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 .

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

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 .

‡ Stack contents and the next input symbol may not decide action: ± shift/reduce conflict: Whether make a shift operation or a reduction. ‡ If a shift-reduce parser cannot be used for a grammar. that grammar is called as non-LR(k) grammar. CS416 Compiler Design 9 . ± reduce/reduce conflict: The parser cannot decide which of several reductions to make.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.

only their parsing tables are different. Operator-Precedence Parser ± simple. LR and LALR work same. CS416 Compiler Design 10 . 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.Shift-Reduce Parsers ‡ There are two main categories of shift-reduce parsers 1. but only a small class of grammars. CFG LR LALR 2.

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. ± 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. 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.LR Parsers ‡ The most powerful shift-reduce parsing (yet efficient) is: LR(k) parsing. yet it is still efficient. CS416 Compiler Design 11 .

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

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 .. S1 X1 S0 s t a t e s .LR Parsing Algorithm input a1 stack Sm Xm Sm-1 Xm-1 ... .. ai .

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

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

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

(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 .

CS416 Compiler Design 19 .. . ‡ A collection of sets of LR(0) items (the canonical LR(0) collection) is the basis for constructing SLR parsers. ‡ 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.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..

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

. . . .The Closure Operation -.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 } . . kernel items CS416 Compiler Design 21 . . .

E p F p (E). .F) = {T p F } goto(I. 22 . . F. ..E) = { E¶ p E .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 T*F.Goto Operation ‡ If I is a set of LR(0) items and X is a grammar symbol (terminal or nonterminal). .id) = { F p id } . E p E+T. . . . T p F. T p T *F } goto(I. . . T p CS416 Compiler Design T*F. . F p (E).. .X). . F p id } goto(I. then goto(I. T. Example: I ={ E¶ p E. .() = { F p ( E). E p T. . F p id } goto(I. . E p E+T. T p . E p E +T } goto(I. . . .T) = { E p T .

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

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

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

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 .

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

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

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

AaAb S p .S S p . Bp.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 .b} FOLLOW(B)={a.BbBa Ap.Conflict Example2 S p AaAb S p BbBa ApI BpI I0: S¶ p . Problem FOLLOW(A)={a.

in the Ii and a is FOLLOW(A) ‡ In some situations. 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.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 . the state i makes a reduction by ApE when the current token is a: ± if the ApE.

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

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

.K. CS416 Compiler Design 35 .a in closure(I) and BpK is a production rule of G. then Bp. 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.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.b will be in the closure(I) for each terminal b in FIRST(Fa) . except that closure and goto operations work a little bit different.

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

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

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

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

L.$/= L p .$/= I8: R p L.$ I7:L p *R.$/= L p ...$/= to I6 Lp .$/= R p .$ S * L I2:S p L.$/= R L * id to I7 to I8 to I4 to I5 I6:S p L=.R.id.$ CS416 Compiler Design I4:L p *.id..$ I11:L p *.S.id.$ L p .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 .$ I9:S p L=R.$ Lp .$ R L * id I4 and I11 to I13 to I10 to I11 to I12 I5 and I12 I7 and I13 I8 and I10 40 .$ L p .*R.R.$/= I13:L p *R.$ I12:L p id.L.$/= R p .*R.$ R I3:S p R.id.$ R p .$ S p ..$ S p .$ R L * id I1:S¶ p S..L.=R.$/= id I5:L p id.R.L.$ R p L..L=R..$ to I9 to I10 to I11 to I12 I10:R p L..R.*R.*R.$ L p .$ L p ..

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

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. it is a LR(1) grammar 13 s12 s11 .

‡ The number of states in SLR and LALR parsing tables for a grammar G are equal.LALR Parsing Tables ‡ LALR stands for LookAhead LR. ‡ But LALR parsers recognize more grammars than SLR parsers. CS416 Compiler Design 43 . ‡ yacc creates a LALR parser for the given grammar. ‡ LALR parsers are often used in practice because LALR parsing tables are smaller than LR(1) parsing tables. ‡ 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 .

Then we will merge them as a single state. Core ‡ We will find the states (sets of LR(1) items) in a canonical LR(1) parser with same cores. ‡ We will do this for all states of a canonical LR(1) parser to get the states of the LALR parser. . I1:L p id . Ex: S p L =R.$ R p L . S p L =R RpL . .$ .= 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. A new state: have same core. CS416 Compiler Design 45 .$ . 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. .= I2:L p id . .$ . ‡ In fact. merge them I12: L p id .

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

a state of LALR parser must have: A p E .c CS416 Compiler Design 47 . B p F.aK. In this case.a and B p F aK. The original canonical LR(1) parser has a conflict.b ‡ This means that a state of the canonical LR(1) parser must have: and But. (Reason for this. this state has also a shift/reduce conflict.e.a .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. ‡ Assume that we can introduce a shift/reduce conflict. the shift operation does not depend on lookaheads) .. A p E. i.

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

.$ I1:S¶ p S .$/= id.$/= * S R p L. .$ I713:L p *R .$ L=R.$ R. R L * id to I713 to I810 to I411 to I512 .$ R p L.$/= I810: R p L . .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 .$ L p id. . . .$ L p *R. I6:S p L= R.$ . .. . S. Same Cores I4 and I11 I5 and I12 I7 and I13 I8 and I10 CS416 Compiler Design 49 .$/= L I2:S p L =R.$ *R. to I9 to I810 to I411 to I512 I9:S p L=R .$/= . . .$ I :L p id .$/= L.$ to I6 Lp *R. .$/= 512 . . .$ L p id.$/= R id I3:S p R . .$ I411:L p * R.$/= R p L . . .

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. it is a LALR(1) grammar CS416 Compiler Design 50 .

‡ Why we want to use an ambiguous grammar? ± Some of the ambiguous grammars are much natural. ± Usage of an ambiguous grammar may eliminate unnecessary reductions.Using Ambiguous Grammars ‡ All grammars used in the construction of LR-parsing tables must be un-ambiguous. 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 . but they will have conflicts. ‡ Can we create LR-parsing tables for ambiguous grammars ? ± Yes. and a corresponding unambiguous grammar can be very complex. we will have again an unambiguous grammar. ± We can resolve these conflicts in favor of one of them to disambiguate the grammar. ± At the end. ‡ Ex.

CS416 Compiler Design 52 .E*E .E E p .E*E . . I : E p E *. .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 . . .id id I : E p id. .E+E E p . .Sets of LR(0) Items for Ambiguous Grammar I0: E¶ p Ep Ep Ep Ep .E+E E) . . . 3 E . .(E) .(E) .E*E E p . ) + * I4 I5 I9: E p (E) .E+E E . . + 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 . I8: E p E*E + I4 E p E +E * I 5 E p E *E . . E I1: E¶ p E E p E +E E p E *E ( ( .(E) E p . .id . . .

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 .) } State I7 has shift/reduce conflicts for symbols + and *.*.+.

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 .

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

± stmt. ‡ Discard zero or more input symbols until a symbol a is found that can legitimately follow A. ± 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). . and it resumes the normal parsing.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.. but this may not work for all situations. block. expr. (Get rid of everything from the stack before this state s)..A]. CS416 Compiler Design 57 . ‡ The parser stacks the nonterminal A and the state goto[s.

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

Sign up to vote on this title
UsefulNot useful