Bottom-Up Parsing

‡ A bottom-up parser creates the parse tree of the given input starting from leaves towards the root. ‡ A bottom-up parser tries to find the right-most derivation of the given input in the reverse order.
S   ...   [ (the right-most derivation of [) n (the bottom-up parser finds the right-most derivation in the reverse order)

‡ Bottom-up parsing is also known as shift-reduce parsing because its two main actions are shift and reduce.
± At each shift action, the current symbol in the input string is pushed to a stack. ± At each reduction step, the symbols at the top of the stack (this symbol sequence is the right side of a production) will replaced by the non-terminal at the left side of that production. ± There are also two more actions: accept and error.

CS416 Compiler Design

1

Shift-Reduce Parsing
‡ A shift-reduce parser tries to reduce the given input string into the starting symbol. a string the starting symbol
reduced to

‡ 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: Shift-Reduce Parser finds:
* S [ rm

[ ž ... ž S rm rm

CS416 Compiler Design

2

Shift-Reduce Parsing -- Example
S p aABb A p aA | a B p bB | b input string: aaabb aaAbb aAbb aABb S

¡ reduction

S rm aABb   aAbb rm aaAbb   aaabb     rm rm Right Sentential Forms ‡ How do we know which substring to be replaced at each reduction step?
CS416 Compiler Design 3

Handle
‡ Informally, a handle of a string is a substring that matches the right side of a production rule.
± But not every substring matches the right side of a production rule is handle

‡ A handle of a right sentential form K (| EF[) is a production rule A p F and a position of K where the string F may be found and replaced by A to produce the previous right-sentential form in a rightmost derivation of K.
* S rm EA[   EF[   rm

‡ If the grammar is unambiguous, then every right-sentential form of the grammar has exactly one handle. ‡ We will see that [ is a string of terminals.
CS416 Compiler Design 4

Handle Pruning
‡ A right-most derivation in reverse can be obtained by handle-pruning. S=K0   K1 rm K2 rm ... rm Kn-1   Kn= [       rm rm input string ‡ Start from Kn, find a handle AnpFn in Kn, and replace Fn in by An to get Kn-1. ‡ Then find a handle An-1pFn-1 in Kn-1, and replace Fn-1 in by An-1 to get Kn-2. ‡ Repeat this, until we reach S.

CS416 Compiler Design

5

A Shift-Reduce Parser E p E+T | T T p T*F | F F p (E) | id Right-Most Derivation of id+id*id E   E+T   E+T*F   E+T*id   E+F*id   E+id*id   T+id*id   F+id*id   id+id*id Right-Most Sentential Form Reducing Production id+id*id F p id F+id*id TpF T+id*id EpT F p id E+id*id TpF E+F*id E+T*id F p id T p T*F E+T*F E+T E p E+T E Handles are red and underlined in the right-sentential forms. CS416 Compiler Design 6 .

Reduce: Replace the handle on the top of the stack by the nonterminal.A Stack Implementation of A Shift-Reduce Parser ‡ There are four possible actions of a shift-parser action: 1. Accept: Successful completion of parsing. The end of the input string is marked by the end-marker $. ‡ ‡ Initial stack just contains only the end-marker $. and calls an error recovery routine. CS416 Compiler Design 7 . 3. 4. 2. Shift : The next input symbol is shifted onto the top of the stack. Error: Parser discovers a syntax error.

A Stack Implementation of A Shift-Reduce Parser Stack $ $id $F $T $E $E+ $E+id $E+F $E+T $E+T* $E+T*id $E+T*F $E+T $E Input id+id*id$ shift +id*id$ +id*id$ +id*id$ +id*id$ id*id$ *id$ *id$ *id$ id$ $ $ $ $ Action reduce by F p id reduce by T p F reduce by E p T shift shift reduce by F p id reduce by T p F shift shift reduce by F p id reduce by T p T*F reduce by E p E+T accept CS416 Compiler Design Parse Tree E 8 E 3 T 2 F 1 id + T 5 F 4 id T 7 * F6 id 8 .

Conflicts During Shift-Reduce Parsing ‡ There are context-free grammars for which shift-reduce parsers cannot be used. left to right scanning right-most derivation k lookhead ‡ An ambiguous grammar can never be a LR grammar. CS416 Compiler Design 9 . ± reduce/reduce conflict: The parser cannot decide which of several reductions to make. ‡ If a shift-reduce parser cannot be used for a grammar. ‡ Stack contents and the next input symbol may not decide action: ± shift/reduce conflict: Whether make a shift operation or a reduction. that grammar is called as non-LR(k) grammar.

LR-Parsers SLR ± covers wide range of grammars. ‡ SLR ± simple LR parser ‡ LR ± most general LR parser ‡ LALR ± intermediate LR parser (lookhead LR parser) ± SLR. but only a small class of grammars.Shift-Reduce Parsers ‡ There are two main categories of shift-reduce parsers 1. LR and LALR work same. CS416 Compiler Design 10 . Operator-Precedence Parser ± simple. CFG LR LALR 2. only their parsing tables are different.

LL(1)-Grammars Ž LR(1)-Grammars ± An LR-parser can detect a syntactic error as soon as it is possible to do so a left-to-right scan of the input. left to right scanning right-most derivation k lookhead (k is omitted it is 1) ‡ LR parsing is attractive because: ± LR parsing is most general non-backtracking shift-reduce parsing. CS416 Compiler Design 11 . ± The class of grammars that can be parsed using LR methods is a proper superset of the class of grammars that can be parsed with predictive parsers.LR Parsers ‡ The most powerful shift-reduce parsing (yet efficient) is: LR(k) parsing. yet it is still efficient.

± SLR ± simple LR parser ± LR ± most general LR parser ± LALR ± intermediate LR parser (look-head LR parser) ± SLR. only their parsing tables are different.LR Parsers ‡ LR-Parsers ± covers wide range of grammars. CS416 Compiler Design 12 . LR and LALR work same (they used the same algorithm).

S1 X1 S0 s t a t e s .... . ai .LR Parsing Algorithm input a1 stack Sm Xm Sm-1 Xm-1 . an $ LR Parsing Algorithm output Action Table terminals and $ four different actions s t a t e s Goto Table non-terminal each item is a state number CS416 Compiler Design 13 ..

... ai ai+1 . (Initial Stack contains just So ) ‡ A configuration of a LR parsing represents the right sentential form: X1 ...A Configuration of LR Parsing Algorithm ‡ A configuration of a LR parsing is: ( So X1 S1 . an $ CS416 Compiler Design 14 ... an $ ) Stack Rest of Input ‡ Sm and ai decides the parser action by consulting the parsing action table. Xm Sm.. Xm ai ai+1 .

. Xm Sm ai s.. reduce ApF (or rn where n is a production number) ± pop 2|F| (=r) items from the stack. ai+1 . Xm Sm. Xm-r Sm-r A s. Xm Sm......shifts the next input symbol and the state s onto the stack ( So X1 S1 . shift s -... Accept ± Parsing successfully completed 4.. an $ ) ± Output is the reducing production reduce ApF 3. ai ai+1 . ai ai+1 . ± then push A and s where s=goto[sm-r.A] ( So X1 S1 . ai . an $ ) 2..Actions of A LR-Parser 1. Error -...Parser detected an error (an empty entry in the action table) CS416 Compiler Design 15 .. an $ ) ( So X1 S1 . an $ ) ( So X1 S1 ...

. an $   X1 ...Yr ai ai+1 .. Xm-r Sm-r A s.....A] ( So X1 S1 ..Yr is a handle. an $ CS416 Compiler Design 16 .....Yr Sm. an $ ) ‡ In fact. ai .Reduce Action ‡ pop 2|F| (=r) items from the stack......Yr ‡ then push A and s where s=goto[sm-r.... ai ai+1 ... let us assume that F = Y1Y2. Y1Y2. Xm Y1. Xm-r Sm-r Y1 Sm-r . Xm-r A ai .. X1 . an $ ) ( So X1 S1 .

(SLR) Parsing Tables for Expression Grammar Action Table 1) 2) 3) 4) 5) 6) E p E+T EpT T p T*F TpF F p (E) F p id state 0 1 2 3 4 5 6 7 8 9 10 11 s5 s5 s6 r1 r3 r5 s7 r3 r5 s5 r6 r6 s4 s4 s11 r1 r3 r5 r1 r3 r5 17 Goto Table ) $ acc E 1 T 2 F 3 id s5 + s6 r2 r4 * ( s4 s7 r4 s4 r2 r4 r6 r2 r4 8 r6 9 3 10 2 3 CS416 Compiler Design .

Actions of A (S)LR-Parser -.Example stack 0 0id5 0F3 0T2 0T2*7 0T2*7id5 0T2*7F10 0T2 0E1 0E1+6 0E1+6id5 0E1+6F3 0E1+6T9 0E1 input id*id+id$ *id+id$ *id+id$ *id+id$ id+id$ +id$ +id$ +id$ +id$ id$ $ $ $ $ action shift 5 reduce by Fpid reduce by TpF shift 7 shift 5 reduce by Fpid reduce by TpT*F reduce by EpT shift 6 shift 5 reduce by Fpid reduce by TpF reduce by EpE+T accept CS416 Compiler Design output Fpid TpF Fpid TpT*F EpT Fpid TpF EpE+T 18 .

. ‡ Ex: A p aBb Possible LR(0) Items: A p aBb (four different possibility) A p a Bb A p aB b A p aBb ‡ Sets of LR(0) items will be the states of action and goto table of the SLR parser. ‡ Augmented Grammar: G¶ is G with a new production rule S¶pS where S¶ is the new starting symbol. ‡ A collection of sets of LR(0) items (the canonical LR(0) collection) is the basis for constructing SLR parsers.. .Constructing SLR Parsing Tables ± LR(0) Item ‡ An LR(0) item of a grammar G is a production of G a dot at the some position of the right side. CS416 Compiler Design 19 . .

every LR(0) item in I is added to closure(I). . then closure(I) is the set of LR(0) items constructed from I by the two rules: 1. then Bp K will be in the closure(I).The Closure Operation ‡ If I is a set of LR(0) items for a grammar G. 2. CS416 Compiler Design 20 . Initially. We will apply this rule until no more new LR(0) items can be added to closure(I). If A p E BF is in closure(I) and BpK is a production rule of G. .

. . . kernel items CS416 Compiler Design 21 . .Example E¶ p E E p E+T EpT T p T*F TpF F p (E) F p id closure({E¶ p E}) = { E¶ p E E p E+T Ep T T p T*F Tp F F p (E) F p id } .The Closure Operation -. . . .

then goto(I. .. E p F p (E). .E) = { E¶ p E . T p T *F } goto(I.X). T. . F p (E).F) = {T p F } goto(I. . . . . . E p E+T. . . Example: I ={ E¶ p E.T) = { E p T . .X) is defined as follows: ± If A p E XF in I then every item in closure({A p EX F}) will be in goto(I. . T p . F p id } goto(I. .Goto Operation ‡ If I is a set of LR(0) items and X is a grammar symbol (terminal or nonterminal)..() = { F p ( E). 22 . E p T. . T p CS416 Compiler Design T*F. T p T*F. E p E+T. .id) = { F p id } . F. F p id } goto(I. . . . T p F. E p E +T } goto(I.

CS416 Compiler Design 23 .X) is not empty and not in C add goto(I. ‡ Algorithm: C is { closure({S¶p S}) } repeat the followings until no more set of LR(0) items can be added to C.Construction of The Canonical LR(0) Collection ‡ To create the SLR parsing tables for a grammar G. ‡ goto function is a DFA on the sets in C.X) to C . we will create the canonical LR(0) collection of the grammar G¶. for each I in C and each grammar symbol X if goto(I.

T p .T T p .I6: E p E+.+T T p T.The Canonical LR(0) Collection -.E) E p .id I7: T p T*.id I4: F p (.(E) F p .(E) F p .(E) I3: T p F.(E) F p .F F p . T p .EI1: E¶ p E.id I8: F p (E.id I5: F p id.T T p .F F p .T*F T p .T*F T p .F T p T.T*F I2: E p T.T E p .F F p . .*F F p .+T E p . I11: F p (E).Example I0: E¶ p . CS416 Compiler Design 24 I9: E p E+T.*F I10: T p T*F.) E p E.E+T E p .E+T E p E. F p .

Transition Diagram (DFA) of Goto Function I0 E I1 T F + I6 T F ( id I2 ( * I9 to I3 to I4 to I5 I10 to I4 to I5 I11 to I6 * to I7 I7 F ( id I3 I4 I5 E T F ( id id I8 to I2 to I3 to I4 ) + CS416 Compiler Design 25 .

.a] is reduce ApE for all a in FOLLOW(A) where A{S¶.. All entries not defined by (2) and (3) are errors. ApE. 3.$] is accept. ‡ If any conflicting actions generated by these rules. the grammar is not SLR(1)..In} 2. then action[i. Create the parsing action table as follows ‡ If a is a terminal. Construct the canonical collection of sets of LR(0) items for G¶.a] is shift j.aF in Ii and goto(Ii. 5. Initial state of the parser contains S¶p.A]=j 4.A)=Ij then goto[i. ‡ If S¶pS. if goto(Ii. is in Ii . ‡ If ApE. then action[i. is in Ii .a)=Ij then action[i.. Cn{I0.S CS416 Compiler Design 26 . Create the parsing goto table ‡ for all non-terminals A.Constructing SLR Parsing Table (of an augumented grammar G¶) 1.

Parsing Tables of Expression Grammar Action Table state 0 1 2 3 4 5 6 7 8 9 10 11 s5 s5 s6 r1 r3 r5 s7 r3 r5 s5 r6 r6 s4 s4 s11 r1 r3 r5 r1 r3 r5 27 Goto Table ) $ acc E 1 T 2 F 3 id s5 + s6 r2 r4 * ( s4 s7 r4 s4 r2 r4 r6 r2 r4 8 r6 9 3 10 2 3 CS416 Compiler Design .

‡ If a grammar G has an SLR(1) parsing table. ‡ Every SLR grammar is unambiguous. it is called SLR(1) grammar (or SLR grammar in short). CS416 Compiler Design 28 . but every unambiguous grammar is not a SLR grammar.SLR(1) Grammar ‡ An LR parser using SLR(1) parsing tables for a grammar G is called as the SLR(1) parser for G.

shift/reduce and reduce/reduce conflicts ‡ If a state does not know whether it will make a shift operation or reduction for a terminal. CS416 Compiler Design 29 . ‡ If a state does not know whether it will make a reduction operation using the production rule i or j for a terminal. ‡ If the SLR parsing table of a grammar G has a conflict. we say that there is a shift/reduce conflict. we say that that grammar is not SLR grammar. we say that there is a reduce/reduce conflict.

I4:L p *.*R L p . Problem FOLLOW(R)={=.*R L p .R R p .L=R S p .Conflict Example S p L=R SpR Lp *R L p id RpL I0: S¶ p . I6:S p L=.*R L p .L Lp .$} = shift 6 reduce by R p L shift/reduce conflict CS416 Compiler Design 30 . I7:L p *R.L I1:S¶ p S.R L p .=R R p L.S S p . I2:S p L. I8:R p L.id R p .R R p .id I5:L p id.L Lp . I3:S p R.id I9: S p L=R.

Conflict Example2 S p AaAb S p BbBa ApI BpI I0: S¶ p . Bp.BbBa Ap.S S p .AaAb S p .b} a reduce by A p I reduce by B p I reduce/reduce conflict reduce by A p I reduce by B p I reduce/reduce conflict b CS416 Compiler Design 31 . Problem FOLLOW(A)={a.b} FOLLOW(B)={a.

This means that making reduction in this case is not correct. FA cannot be followed by the terminal a in a right-sentential form when FE and the state i are on the top stack. the state i makes a reduction by ApE when the current token is a: ± if the ApE. in the Ii and a is FOLLOW(A) ‡ In some situations.Constructing Canonical LR(1) Parsing Tables ‡ In SLR method. S p AaAb S p BbBa ApI BpI S AaAb Aab ab Aab   I ab AaAb   Aa I b S BbBa Bba ba Bba   I ba BbBa   Bb I a CS416 Compiler Design 32 .

LR(1) Item ‡ To avoid some of invalid reductions. ‡ A LR(1) item is: A p E F. where a is the look-head of the LR(1) item (a is a terminal or end-marker. ‡ Extra information is put into a state by including a terminal symbol as a second component in an item.) CS416 Compiler Design 33 .a . the states need to carry more information.

a1 where {a1. ‡ A state will contain A p E ..an . we do the reduction by ApE only if the next input symbol is a (not for any terminal in FOLLOW(A)). . CS416 Compiler Design 34 . ‡ When F is empty (A p E .) ‡ When F ( in the LR(1) item A p E F.an}  FOLLOW(A) . ... . the look-head does not have any affect.a )....a ) is not empty.LR(1) Item (cont. A p E .

Canonical Collection of Sets of LR(1) Items ‡ The construction of the canonical collection of the sets of LR(1) items are similar to the construction of the canonical collection of the sets of LR(0) items. CS416 Compiler Design 35 . except that closure and goto operations work a little bit different.K. . then Bp.b will be in the closure(I) for each terminal b in FIRST(Fa) . closure(I) is: ( where I is a set of LR(1) items) ± every LR(1) item in I is in closure(I) ± if ApE BF.a in closure(I) and BpK is a production rule of G.

XF.goto operation ‡ If I is a set of LR(1) items and X is a grammar symbol (terminal or non-terminal).a in I then every item in closure({A p EX.F.X).a}) will be in goto(I. CS416 Compiler Design 36 . then goto(I.X) is defined as follows: ± If A p E.

for each I in C and each grammar symbol X if goto(I.$}) } repeat the followings until no more set of LR(1) items can be added to C. CS416 Compiler Design 37 .X) to C ‡ goto function is a DFA on the sets in C.S.X) is not empty and not in C add goto(I.Construction of The Canonical LR(1) Collection ‡ Algorithm: C is { closure({S¶p.

A Short Notation for The Sets of LR(1) Items ‡ A set of LR(1) items containing the following items A p E F..a1/a2/.../an . .an can be written as A p E F. A p E F. CS416 Compiler Design 38 . .a1 ..

a B p .b .$ CS416 Compiler Design 39 .a B I7: S p BbB.Example S p AaAb S p BbBa ApI BpI I0: S¶ p .$ S A I2: S p A.$ A p .$ a to I4 I4: S p Aa.Ab .b A I6: S p AaA.S .$ B I3: S p B. . . .Ba .a .$ a I8: S p AaAb. . .bBa .$ S p .aAb .$ b I9: S p BbBa.$ S p .AaAb . .BbBa . .b I5: S p Bb.$ A p .$ b to I5 I1: S¶ p S.$ B p .Canonical LR(1) Collection -.

L.id.$ R I3:S p R..*R.$ L p .$ to I9 to I10 to I11 to I12 I10:R p L.L=R.R..$/= R p .R.$ R p L.$ L p .$ CS416 Compiler Design I4:L p *.*R.$ I12:L p id.L.$ R L * id I1:S¶ p S.S.id.$/= I8: R p L..$ I7:L p *R..$ S * L I2:S p L.*R.$/= I13:L p *R.Canonical LR(1) Collection ± Example2 S¶ p S 1) S p L=R 2) S p R 3) Lp *R 4) L p id 5) R p L I0:S¶ p .$/= R p .$/= R L * id to I7 to I8 to I4 to I5 I6:S p L=.$ L p ..=R.$/= id I5:L p id.$ I9:S p L=R.L..$ R p .$/= L p .$ S p .L.$ S p ..R..*R.$ Lp .$/= L p .$ I11:L p *..$ R L * id I4 and I11 to I13 to I10 to I11 to I12 I5 and I12 I7 and I13 I8 and I10 40 .id.R.id.$ R p ..$/= to I6 Lp .$ L p .

if goto(Ii. .a is in Ii . Create the parsing action table as follows ‡ ‡ ‡ ‡ If a is a terminal. Construct the canonical collection of sets of LR(1) items for G¶. If S¶pS .b in Ii and goto(Ii. Cn{I0. . then action[i..In} 2.a] is shift j. 5. Initial state of the parser contains S¶p.Construction of LR(1) Parsing Tables 1...$ is in Ii .a)=Ij then action[i. Create the parsing goto table ‡ for all non-terminals A. ApE aF. All entries not defined by (2) and (3) are errors. If any conflicting actions generated by these rules.S.A]=j 4. then action[i..a] is reduce ApE where A{S¶. the grammar is not LR(1). 3. If ApE . .$] is accept.$ CS416 Compiler Design 41 .A)=Ij then goto[i.

it is a LR(1) grammar 13 s12 s11 .LR(1) Parsing Tables ± (for Example2) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 id s5 * s4 = $ acc r5 r2 8 r4 s12 s11 r3 r5 r3 r5 r1 r5 10 r4 r3 CS416 Compiler Design 42 S 1 L 2 R 3 s6 s5 s4 7 9 r4 10 no shift/reduce or no reduce/reduce conflict ¡ so.

‡ LALR parsers are often used in practice because LALR parsing tables are smaller than LR(1) parsing tables.LALR Parsing Tables ‡ LALR stands for LookAhead LR. ‡ The number of states in SLR and LALR parsing tables for a grammar G are equal. ‡ But LALR parsers recognize more grammars than SLR parsers. CS416 Compiler Design 43 . ‡ yacc creates a LALR parser for the given grammar. ‡ A state of LALR parser will be again a set of LR(1) items.

this shrink process does not produce a shift/reduce conflict.Creating LALR Parsing Tables Canonical LR(1) Parser shrink # of states ‡ This shrink process may introduce a reduce/reduce conflict in the resulting LALR parser (so the grammar is NOT LALR) ‡ But. LALR Parser CS416 Compiler Design 44 .

$ . merge them I12: L p id .$ R p L . CS416 Compiler Design 45 . I1:L p id . Core ‡ We will find the states (sets of LR(1) items) in a canonical LR(1) parser with same cores. S p L =R RpL . .= I2:L p id . Then we will merge them as a single state.= L p id . . .The Core of A Set of LR(1) Items ‡ The core of a set of LR(1) items is the set of its first component. .$ . ‡ In fact. A new state: have same core. Ex: S p L =R. ‡ We will do this for all states of a canonical LR(1) parser to get the states of the LALR parser.$ . the number of the states of the LALR parser for a grammar will be equal to the number of states of the SLR parser for that grammar.

Š Ik since I1... the grammar is LALR(1) grammar.X)=K where K is the union of all sets of items having same cores as goto(I1...... ‡ Find each core. ‡ If no conflict is introduced.. we cannot introduce a shift/reduce conflict) CS416 Compiler Design 46 .Jm} where m e n ‡ Create the parsing tables (action and goto tables) same as the construction of the parsing tables of LR(1) parser.goto(I2...Creation of LALR Parsing Tables ‡ Create the canonical LR(1) collection of the sets of LR(1) items for the given grammar. C={I0.. goto(J. (We may only introduce reduce/reduce conflicts.In} C¶={J1.. ± Note that: If J=I1 Š ..X). ± So.X).X) must be same... replace those sets having same cores with a single set which is their union.. find all sets having that same core..Ik have same cores cores of goto(I1..

The original canonical LR(1) parser has a conflict.aK. the shift operation does not depend on lookaheads) .a .e. A p E. ‡ Assume that we can introduce a shift/reduce conflict. B p F. (Reason for this.b ‡ This means that a state of the canonical LR(1) parser must have: and But.a and B p F aK. In this case. a state of LALR parser must have: A p E .Shift/Reduce Conflict ‡ We say that we cannot introduce a shift/reduce conflict during the shrink process for the creation of the states of a LALR parser.. i.c CS416 Compiler Design 47 . this state has also a shift/reduce conflict.

B p F.Reduce/Reduce Conflict ‡ But.b/c ¡ ..a ..b I2: A p E . I1 : A p E . B p F. B p F.c reduce/reduce conflict CS416 Compiler Design 48 . we may introduce a reduce/reduce conflict during the shrink process for the creation of the states of a LALR parser.a/b .b I12: A p E ..

. .$ R.$/= id.$ L p id.$ I :L p id . .$/= R p L . .$ I411:L p * R.$ R p L.$/= L I2:S p L =R.Canonical LALR(1) Collection ± Example2 S¶ p S 1) S p L=R 2) S p R 3) Lp *R 4) L p id 5) R p L I0:S¶ p Sp Sp Lp Lp Rp R L * id .$/= * S R p L.$ I713:L p *R .$ . . .$/= I810: R p L . .$ I1:S¶ p S . Same Cores I4 and I11 I5 and I12 I7 and I13 I8 and I10 CS416 Compiler Design 49 . . . S.$/= L. to I9 to I810 to I411 to I512 I9:S p L=R .$ *R. . .$ to I6 Lp *R. R L * id to I713 to I810 to I411 to I512 .$/= R id I3:S p R .$ L p id. . .$ L p *R. ..$/= 512 . .$/= .$ L=R. I6:S p L= R. .

it is a LALR(1) grammar CS416 Compiler Design 50 .LALR(1) Parsing Tables ± (for Example2) 0 1 2 3 4 5 6 7 8 9 id s5 * s4 = $ acc r5 r2 8 r4 s12 s11 r3 r5 r3 r5 r1 r4 10 9 7 S 1 L 2 R 3 s6 s5 s4 no shift/reduce or no reduce/reduce conflict ¡ so.

± At the end. and a corresponding unambiguous grammar can be very complex. ± We can resolve these conflicts in favor of one of them to disambiguate the grammar. E p E+T | T E p E+E | E*E | (E) | id T p T*F | F F p (E) | id CS416 Compiler Design 51 .Using Ambiguous Grammars ‡ All grammars used in the construction of LR-parsing tables must be un-ambiguous. but they will have conflicts. ± Usage of an ambiguous grammar may eliminate unnecessary reductions. ‡ Why we want to use an ambiguous grammar? ± Some of the ambiguous grammars are much natural. we will have again an unambiguous grammar. ‡ Can we create LR-parsing tables for ambiguous grammars ? ± Yes. ‡ Ex.

id I6: E p (E ) E p E +E E p E *E E ( id I3 E ( id I3 I2 I2 I7: E p E+E + I4 E p E +E * I 5 E p E *E . .(E) . .E E p .E+E E p .Sets of LR(0) Items for Ambiguous Grammar I0: E¶ p Ep Ep Ep Ep . . I : E p E *.E+E E . I8: E p E*E + I4 E p E +E * I 5 E p E *E .id id I : E p id. 3 E .E*E . ) + * I4 I5 I9: E p (E) .(E) .E*E . .E+E E) .E*E E p . . + I4: E p E + E E p E+E E p E*E * E p (E) E p id 5 I2: E p ( Ep Ep Ep id Ep . .id . . . E I1: E¶ p E E p E +E E p E *E ( ( .(E) E p . . . . . . . CS416 Compiler Design 52 .

) } State I7 has shift/reduce conflicts for symbols + and *.SLR-Parsing Tables for Ambiguous Grammar FOLLOW(E) = { $.+.*. I0 E I1 + I4 E I7 when current token is + shift + is right-associative reduce + is left-associative when current token is * shift * has higher precedence than + reduce + has higher precedence than * CS416 Compiler Design 53 .

I0 E I1 * I5 E I7 when current token is * shift * is right-associative reduce * is left-associative when current token is + shift + has higher precedence than * reduce * has higher precedence than + CS416 Compiler Design 54 .+.*.SLR-Parsing Tables for Ambiguous Grammar FOLLOW(E) = { $.) } State I8 has shift/reduce conflicts for symbols + and *.

SLR-Parsing Tables for Ambiguous Grammar id s3 s3 r4 s3 s3 s4 r1 r2 r3 s5 s5 r2 r3 CS416 Compiler Design Action + * s4 s5 0 1 2 3 4 5 6 7 8 9 ( s2 s2 ) $ acc Goto E 1 6 r4 s2 s2 r4 r4 7 8 s9 r1 r2 r3 r1 r2 r3 55 .

‡ But. ‡ Errors are never detected by consulting the goto table. ‡ An LR parser will announce error as soon as there is no valid continuation for the scanned portion of the input. ‡ A canonical LR parser (LR(1) parser) will never make even a single reduction before announcing an error. CS416 Compiler Design 56 . All empty entries in the action table are error entries.Error Recovery in LR Parsing ‡ An LR parser will detect an error when it consults the parsing action table and finds an error entry. ‡ The SLR and LALR parsers may make several reductions before announcing an error. LALR and SLR parsers) will never shift an erroneous input symbol onto the stack. all LR parsers (LR(1).

.Panic Mode Error Recovery in LR Parsing ‡ Scan down the stack until a state s with a goto on a particular nonterminal A is found. CS416 Compiler Design 57 . expr.A]. ‡ Discard zero or more input symbols until a symbol a is found that can legitimately follow A.. but this may not work for all situations.. and it resumes the normal parsing. (Get rid of everything from the stack before this state s). block. ‡ The parser stacks the nonterminal A and the state goto[s. ± stmt. ± The symbol a is simply in FOLLOW(A). ‡ This nonterminal A is normally is a basic programming block (there can be more than one choice for A).

± missing operand ± unbalanced right parenthesis CS416 Compiler Design 58 . or it can do both insertion and deletion). ‡ An error routine inserts the symbols into the stack or the input (or it deletes the symbols from the stack and the input. ‡ An error routine reflects the error that the user most likely will make in that case.Phrase-Level Error Recovery in LR Parsing ‡ Each empty entry in the action table is marked with a specific error routine.

Sign up to vote on this title
UsefulNot useful