Professional Documents
Culture Documents
(Lecture 16)
Instructor:
Gul Sher Ali
______________________________________________________
University of Gujrat
1
Non- Recursive predictive
parsing
2
Syntax Error
3
4
5
Outline
Top-down v.s. Bottom-up
Top-down parsing Bottom-up parsing
Shift-reduce parsers
Recursive-descent
parsing LR(0) parsing
LR(0) items
LL(1) parsing
Finite automata of items
LL(1) parsing
LR(0) parsing algorithm
algorithm
LR(0) grammar
First and follow sets
SLR(1) parsing
Constructing LL(1) SLR(1) parsing algorithm
parsing table
SLR(1) grammar
Error recovery Parsing conflict
LALR(1)
Chapter 4 Parsing 2301373 6
Parsing Techniques
Bottom-up parsers
Start in a state valid for
legal first tokens
Bottom-up parsers handle a
large class of grammars
7
Bottom-up Parsing
8
Terminologies
Shifting :
Movement of input symbol to stack is called shifting.
2301373 13
Conflicts
Shift-reduceand reduce-
reduce conflicts are caused by
The limitations of the LR parsing method (even when the
grammar is unambiguous)
Ambiguity of the grammar
14
LR parsing
15
The image given below depicts the bottom-up parsers
available.
16
There are three widely used algorithms available for constructing an
LR parser:
2301373 18
LR(0) Automation Rules
The grammar must be augmented
a new start symbol S’ is added
a production S’ S is added
It-self to closure.
When the next symbol is NT, then add its’
productions
If the added symbol order 2 again have NT,
then add it’s production.
2301373 19
Finite automata of items
Grammar:
S’ .S S S’ S.
S’ S S .(S)S
S (S)S S.
S (S.)S
S ( S
Items: )
S (.S)S
S’ .S S .(S)S
S.
S’ S.
( ( S (S).S
S .(S)S S .(S)S
S (.S)S S.
S (S.)S
S
S (S).S
S (S)S. S (S)S.
S. 20
Example for LR(0)
Grammar:
S AA
S b c
A b a A
A c
21
Example for LR(0)
Grammar:
E E +T | T
T T *F | F
F ( E ) | id
22
Non-LR(0)Grammar
Conflict
S’ .S S S’ S. 1
Shift-reduce conflict S .(S)S
A state contains a S.
0 S (S.)S 3
complete item A x. (
and a shift item A x.By S
S (.S)S )
Reduce-reduce conflict
S .(S)S
A state contains more S .
2
than one complete items.
( ( S (S).S
S .(S)S
A grammar is a LR(0) S .
4
grammar if there is no
conflict in the S
grammar. S (S)S. 5
2301373 23