Module III
Module III
Bottom up Parsers
Top-down parsers (LL(1), recursive descent)
• Start at the root of the parse tree from the start symbol and grow toward leaves
(similar to a derivation)
• Pick a production and try to match the input
• Bad “pick” may need to backtrack
• Some grammars are backtrack-free (predictive parsing)
• Bottom-up parsers (LR(1), operator precedence)
• Start at the leaves and grow toward root
• We can think of the process as reducing the input string to the start symbol
• At each reduction step a particular substring matching the right-side of a
production is replaced by the symbol on the left-side of the production
• Bottom-up parsers handle a large class of grammars
Bottom up Parsing
• A parse tree is created from the leaf upwards
• The symbols in the input are placed at the leaf nodes of the tree
• Starting from the leaves, the parser fills in the internal nodes of the parse tree
gradually eventually finding the root.
• The creation of an internal node involves replacing symbols from (VUT)* by a
single non terminal repeatedly
• The task is to identify the RHS of a production rule in the tree constructed so far
and replace it with the corresponding LHS of the production rule
• This is called reduction
• The crucial task in bottom up parsing is to find productions that have to be
used for reduction
Shift Reduce Parsing
• A generic name for a family of parsers that employ the strategy of
bottom up parsing.
• At each reduction step, a substring of the input matching to the right side of a
production rule is replaced by the non-terminal at the left side of that production
rule.
• If the substring is chosen correctly, the right most derivation of that string is
created in the reverse order.
Rightmost Derivation: S
Shift-Reduce Parser finds: ... S
Handles
• Handle of a string: Substring that matches the RHS of some production AND whose
reduction to the non-terminal on the LHS represents one step along the reverse of
a rightmost derivation.
1. Shift : The next input symbol is shifted onto the top of the stack.
2. Reduce: The right end of the string to be reduced is on top of the stack.
Locate the left end of the string within the stack. Replace the handle on the
top of the stack by the nonterminal.
4. Error: Parser discovers a syntax error, and calls an error recovery routine.
Stack Implementation of Shift Reduce Parsing
Stack Input Action
$ id+id*id$ shift
$id +id*id$ Reduce F→id
$F +id*id$ Reduce T→F
$T +id*id$ Reduce E→T
$E +id*id$ shift
$E+ id*id$ shift
$E+id *id$ Reduce F→id
$E+F *id$ Reduce T→F
$E+T *id$ shift
$E+T* id$ shift
$E+T*id $ Reduce F→id
$E+T*F $ Reduce T→T*F
$E+T $ Reduce E→E+T
$E $ Accept
Conflicts During Shift Reduce Parsing
• Viable Prefix
• The set of prefixes of right sentential forms that can appear on the stack of a
shift reduce parser
• Stack contents and the next input symbol may not decide action
• shift/reduce conflict: Whether to make a shift operation or a reduction.
• reduce/reduce conflict: The parser cannot decide which of several
reductions to make.
• There are context-free grammars for which shift-reduce parsers cannot be
used.
• If a shift-reduce parser cannot be used for a grammar, that grammar is
called as non-LR(k) grammar.
• L: left to right scanning
• R: rightmost derivation in reverse
• k: lookahead
• An ambiguous grammar can never be a LR grammar.
Shift Reduce Parsers
• Two main categories of shift reduce parsers
1. Operator Precedence Parser
• simple, but only a small class of grammars.
2. LR Parser
• covers wide range of grammars.
• SLR – simple LR parser
• Canonical LR – most general LR parser
• LALR – intermediate LR parser (lookahead LR parser)
• SLR, Canonical LR and LALR work same, only their parsing tables are
different.
Operator Precedence Parser
• Operator grammar
• small, but an important class of grammars
• In an operator grammar, no production rule can have:
• at the right side
• two adjacent non-terminals at the right side.
• Ex:
EAB EEAE EE+E
Aa Eid |E*E
Bb A+|*|/ | E/E | id
not operator grammar not operator grammar operator grammar
Precedence Relations
• In operator-precedence parsing, we define three disjoint precedence
relations between certain pairs of terminals.
5) F (E) 3 r4 r4 r4 r4
4 s5 s4 8 2 3
6) F id
5 r6 r6 r6 r6
6 s5 s4 9 3
• sj: shift current input symbol and
state j onto stack 7 s5 s4 10
• rj: reduce by production number j 8 s6 s11
• acc: accept 9 r1 s7 r1 r1
• Blank entries: error
10 r3 r3 r3 r3
11 r5 r5 r5 r5
A Configuration of LR Parsing Algorithm
• A configuration of a LR parsing is:
( So X1 S1 ... Xm Sm, ai ai+1 ... an $ )
Input * id + id $ Input id + id $
7
*
2 action(2,*) = s7 2
T
T
0
0
Stack
Stack
LR Parser
2. action[Sm, ai] = reduce A
pop 2|| (=2r) items from the stack;
then push A and S where S = goto[sm-r , A]
(So X1 S1 ... Xm Sm, ai ai+1 ... an $) (So X1 S1 ... Xm-r Sm-r A S, ai ... an $)
• Output the reducing production reduce A
Input Input
+ id $ + id $
10
Stack
F 2
2*3 items • Pop 3 grammar
7 Reduce T→T*F T symbols and 3 state
* 0 symbols
2 • Push T
Stack • Push goto(0,T)
T
0
LR Parser
3. If action[Sm , ai ] = accept , Parsing successfully completed
E’ E .
closure(,E’ E}) =
E E+T .
{ E’ E {rule 1}
ET .
E E+T
T T*F .
E T
TF .
T T*F
F (E) .
T F
{rule 2}
F id .
F (E)
.
F id }
Computation of Closure
function closure ( I )
begin
J := I;
repeat
for each item A .B in J and each production
B of G such that B. is not in J do
add B. to J
until no more items can be added to J
return J
End
Goto Operation
• If I is a set of LR(0) items and X is a grammar symbol (terminal or non-terminal),
then goto(I,X) is defined as follows:
• If A .X in I then every item in closure([A X.]) will be in goto(I,X).
I =, E’ E., E E.+T}
F
to I2 + I11
I5 to I3 to I6
(
to I4
Constructing SLR Parsing Table
Input: An augmented grammar G’
Output: The SLR parsing functions action and goto for grammar G’
1. Construct the canonical collection of sets of LR(0) items for G’, C={I0,...,In}
2. State i is constructed from Ii . The parsing actions for state i are determined as follows:
• If [A.a] is in Ii and goto(Ii,a)=Ij , then action[i,a] is shift j. Here a should be a
terminal
• If [A.] is in Ii , then set action[i,a] to reduce A for all a in FOLLOW(A) where
AS’
• If [S’S.] is in Ii , then action[i,$] is accept.
If any conflicting actions generated by these rules, the grammar is not SLR(1)
3. The goto transitions for state I are constructed for all nonterminals A using the rule
If goto(Ii,A)=Ij then goto[i,A]=j
4. All entries not defined by (2) and (3) are errors.
5. Initial state of the parser is the one constructed from the sets of items containing *S’.S]
Canonical Collection of LR(0) Items
I0: E’ .E I4: goto(I0, () I7: goto(I2, *)
E .E+T F (.E) goto(I6, F)=I3
•a T T*.F
E .T E .E+T F .(E) goto(I6,()=I4
T .T*F E .T F .id goto(I6, id)=I5
T .F T .T*F
F .(E) T .F I8: goto(I4, E) I10 :goto(I7, F)
F .id F .(E) F (E.) T T*F.
F .id E E.+T
I1: goto(I0, E) goto(I7,()=I4
E’ E. I5: goto(I0, id) goto(I4, T)=I2 goto(I7, id)=I5
E E.+T F id. goto(I4, F)=I3
goto(I4, ()=I4 I11 :goto(I8, ))
I2: goto(I0,T) I6: goto(I1, +)
F (E).
E T. E E+.T goto(I4, id)=I5
T T.*F T .T*F
T .F I9: goto(I6, T) goto(I8,+) = I6
I3: goto(I0, F) F .(E) E E+T. goto(I9,*) = I7
T F. F .id T T.*F
1) E E+T
2) ET
SLR Parsing Table 3)
4)
T T*F
TF
FOLLOW(E)= {+,),$}
FOLLOW(T)= {*,+,),$}
Action Goto 5) F (E) FOLLOW(F)= {*,+,),$}
6) F id
state id + * ( ) $ E T F
0 s5 s4 1 2 3
goto(I0, E)= I1
1 s6 acc goto(I0,T)= I2 goto(I6, T)= I9
2 r2 s7 r2 r2 goto(I0, F)= I3 goto(I6, F)=I3
3 r4 r4 r4 r4 goto(I0, ()= I4 goto(I6,()=I4
4 s5 s4 8 2 3 goto(I0, id)= I5 goto(I6, id)=I5
goto(I1, +)= I6 goto(I7, F)= I10
5 r6 r6 r6 r6
goto(I2, *)= I7
6 s5 s4 9 3 goto(I7,()=I4
goto(I4, E)= I8
7 s5 s4 10 goto(I7, id)=I5
goto(I4, T)=I2
8 s6 s11
goto(I8, ))= I11
goto(I4, F)=I3
goto(I8,+) = I6
9 r1 s7 r1 r1 goto(I4, ()=I4
goto(I9,*) = I7
10 r3 r3 r3 r3 goto(I4, id)=I5
11 r5 r5 r5 r5
SLR(1) Grammar
• If the SLR parsing table of a grammar G has a conflict, we say that that
grammar is not SLR grammar.
SLR Parser-Example
• S→L=R
• S →R
• L→*R
• L →id
• R →L
Conflict Example-1
I7: goto (I4, R)
S→L=R I5: goto (I0, id)
•S →R
. I1: goto (I0, S)
L →id.
L→*R.
S’ →S.
L→*R I8: goto (I4, L)
L →id I2: goto (I0, L) I6: goto (I2, =)
R →L.
R →L S→L.=R S→L=.R
R →L. R →.L
I9: goto (I6, R)
L→.*R
S→L=R.
I0: I3: goto (I0, R) L →.id
S’ →.S S →R. goto(I6, L) =I8
S→.L=R goto(I6, *) =I4
S →.R I4: goto (I0, *) goto(I4, *) =I4
goto(I6, id) =I5
L→.*R L→*.R goto(I4, *) d=I5
L →.id R →.L Action[2,=] = shift 6
R →.L L→.*R Action[2,=] = reduce by R L
L →.id [ S L=R *R=R] so follow(R) contains, =
Conflict Example2
S AaAb I0: S’ .S
S BbBa S .AaAb
A S .BbBa
B A.
B.
I0: A . I0: B .
FOLLOW(A)={a,b} FOLLOW(B)={a,b}
Problem: reduce/reduce
action(0,a)=reduce by A action(0,a)=reduce by B
conflict
action(0,b)=reduce by A action(0,b)=reduce by B
Canonical LR(1) Parser
• Most general technique for constructing an LR parsing table.
• In SLR method, the state i makes a reduction by A
• if the set of items Ii contains the item [A.] and
• a is FOLLOW(A)
C c C, c/d C I8
c C c C, c/d
C d, c/d C c C , c/d
I3
I4 d
d
C d , c/d
Construction of LR(1) Parsing Tables
Algorithm: Construction of Canonical LR Parsing Tables
Input: An augmented grammar G’
Output: The canonical LR parsing table functions, action and goto for grammar G’
1. Construct C’= {I0,...,In}, the canonical collection of sets of LR(1) items for G’.
2. State i of the parser is constructed from Ii. The parsing action for state i is determined as
follows
• If [A.a,b] in Ii and goto(Ii,a)=Ij then set action[i,a] to shift j. Here a should be a terminal
• If [A.,a] is in Ii and AS’, then set action*i,a] to reduce A
• If [S’S.,$] is in Ii , then action[i,$] to accept.
If any conflicting actions generated by these rules, the grammar is not LR(1).
3. The goto transitions for state i are constructed for all nonterminals A using the rule:
if goto(Ii,A)=Ij then goto[i,A]=j
4. All entries not defined by (2) and (3) are errors.
5. Initial state of the parser is the one constructed from the set of items containing *S’.S, $]
S’ S
Canonical LR(1) Collection- Example 1. S CC
2. C cC
3. C d
I0: S’ S, $
S CC, $ I3: goto(I0, c) = I6: goto(I2, c) = goto(I3, c) = I3
C cC, c/) C cC, c/d C cC, $
C d, c/) C cC, c/d C cC, $ goto(I3, d) = I4
C d, c/d C d, $
I1: goto(I0, S)= I9: goto(I6, C) =
S’ S , $ I4: goto(I0, d) = I7: goto(I2, d) = C cC, $
C d, c/d C d, $
I2: goto(I0, C) = goto(I6, c) = I6
S CC, $ I5: goto(I2, C) = I8: goto(I3, C) =
C cC, $ S CC, $ C cC, c/d goto(I6, d) = I7
C d, $
Canonical LR(1) Parsing Table for Grammar G’
ACTION GOTO goto(I0,S)=I1
State c d $ S C goto(I0,C)=I2
0 s3 s4 1 2 goto(I0,c)=I3
1 acc
goto(I0,d)=I4
goto(I2,C)=I5
2 s6 s7 5
goto(I2,c)=I6
3 s3 s4 8
goto(I2,d)=I7
4 r3 r3
goto(I3,C)=I8
5 r1
goto(I3,c)=I3
6 s6 s7 9
goto(I3,d)=I4
7 r3 goto(I6,C)=I9
8 r2 r2 goto(I6,c)=I6
9 r2 goto(I6,d)=I7
LALR Parsing Tables
1. LALR stands for Lookahead LR.
2. LALR parsers are often used in practice because LALR parsing tables
are smaller than LR(1) parsing tables.
3. The number of states in SLR and LALR parsing tables for a grammar
G are equal.
4. But LALR parsers recognize more grammars than SLR parsers.
5. yacc creates a LALR parser for the given grammar.
6. A state of LALR parser will be again a set of LR(1) items.
The Core of LR(1) Items
• The core of a set of LR(1) Items is the set of their first components (i.e.,
LR(0) items)
• The core of the set of LR(1) items
{ C cC, c/d,
C cC, c/d,
C d, c/d }
is { C cC, C cC, C d }
Creating LALR Parsing Tables
Canonical LR(1) Parser LALR Parser
shrink # of states
• Look for LR(1) items having the same core, i.e. the first set of components
• Merge the sets with common core into one set of items
• Eg; I4 and I7 form such a pair with core {C→d.}
• I3 and I6 with core {C→c.C, C→.cC, C→.d}
• I8 and I9 with core {C→cC.}
• Replace I4 and I7 by I47, [I4 = C d, c/d ; I7 = C d, $]
• I47={C→d., c/d/$}
Creation of LALR Parsing Tables
Algorithm: An easy but space consuming LALR parsing table construction
Input: An augmented grammar G’
Output: The LALR paring table functions action and goto for grammar G’
Method:
1. Construct C={I0,...,In}, the collection LR(1) items for the given grammar.
2. For each core present among the sets of LR(1) items, find all sets having that core and
replace these sets by their union.
3. Let C’=,J1,...,Jm} be the resulting set of LR(1) items. The parsing actions for state i are
constructed from Ji in the same manner as in the algorithm for canonical LR parser. If
there is a parsing action conflict, the algorithm fails to produce a parser and the
grammar is not LALR(1).
4. The goto table is constructed as follows: If J is the union of one or more sets of LR(1)
items, i.e. J=I1 ... Ik, then the cores of goto(I1,X),...,goto(I2,X) are the same since
I1,...,Ik have same cores. Let K be the union of all sets of items of goto(I,X). Then
goto(I,X)=K.
S’ S, $ I1
S C C, $ S’ S , $
C c C, c/d S
C d, c/d
I0 S C C, $ I5
C C
C c C, $ S C C , $
C d, $
I2
c I6
c C c C, $
C c C, $ C
d C d, $ I9
d C cC , $
I7
C d , $
c c
C c C, c/d
C c C, c/d C I8
C d, c/d
C cC , c/d
I3
d d
I4
C d , c/d
S’ S, $ I1
S C C, $ S’ S , $
C c C, c/d S
C d, c/d
I0 S C C, $ I5
C C
C c C, $ S C C , $
C d, $
I2
c I6
c
C c C, $
C c C, $
C
d C d, $
I7 d
C d , $
c c
C c C, c/d
C c C, c/d C I89
C d, c/d
C c C , c/d/$
I3
d
d I4
C d , c/d
S’ S, $ I1
S C C, $ S’ S , $
C c C, c/d S
C d, c/d
I0 S C C, $ I5
C C c C, $ C
S C C , $
C d, $
I2
c I6
c
d C c C, $
C c C, $
C
d C d, $
I47 d
C d , c/d/$
c
c d
C c C, c/d I89
C c C, c/d
C
C d, c/d C c C , c/d/$
I3
S’ S, $ I1
S C C, $ S’ S , $
C c C, c/d S
C d, c/d
I0 S C C, $ I5
C C
C c C, $ S C C , $
C d, $
I2
c I36
c C c C, c/d/$
C c C,c/d/$ C
C d,c/d/$
d
c
I47 d
C d , c/d/$
d
I89
C c C , c/d/$
LALR Parsing Table
• ACTION
States action goto •
•
GOTO
State
• c
• d
• $
c d $ S C
• S
• C
• 0
• s3
0 s36 s47 1 2
• s4
• 1
• 2
• 1
1 acc • acc
• 2
• s6
• s7
• 5
2 s36 s47 5
• 3
• s3
• s4
• 8
36 s36 s47 89
• 4
• r3
• r3
• 5
47 r3 r3 r3 • r1
• 6
• s6
• s7
5 r1 • 9
• 7
• r3
• 8
• r2
89 r2 r2 r2 • r2
• 9
• r2
Canonical LR(1) Parsing Table
LALR Parsing Table
ACTION GOTO
State action goto State c d $ S C
s 0 s3 s4 1 2
c d $ S C 1 acc
0 s36 s47 1 2 2 s6 s7 5
1 acc 3 s3 s4 8
2 s36 s47 5 4 r3 r3
36 s36 s47 89 5 r1
47 r3 r3 r3 6 s6 s7 9
5 r1 7 r3
89 r2 r2 r2 8 r2 r2
9 r2