730 likes | 1.2k Views
Bottom-Up Parser. Reference. Andrew W. Appel, Modern Compiler Implementation in Java, 2 nd edition, Cambridge Press, 2002. Section 3.2 and 3.3. Weakness of LL(k) parsing. They must predict which production to use, having seen only the first k tokens of the right-hand side. LR(k) Parsing.
E N D
Reference • Andrew W. Appel, Modern Compiler Implementation in Java, 2nd edition, Cambridge Press, 2002. Section 3.2 and 3.3.
Weakness of LL(k) parsing • They must predict which production to use, having seen only the first k tokens of the right-hand side.
LR(k) Parsing • Postpone the decision until it has seen input tokens corresponding to the entire right-hand side of the production in question • And, k more input tokens beyond Input String a b c considering k more tokens if needed Right-hand side of some rule ex) for rule A := a b c a b c is reduced into A
LR(k) • L: left-to-right parse • R: Rightmost derivation • k: k-token lookahead
LR Parse Example (1) • Grammar 0. S’ S$ 1. S S; S 2. S id := E 3. S print ( L ) 4. E id 5. E num 6. E E + E 7. E (S, E) 8. L E 9. L L, E • Source Program a := 7; b := c + ( d := 5 + 6, d )
LR Parse Example (2) Figure 3.18
LR Parse Example (3) • Parser • Stack • Input • lookahead: the first k tokens of the input • Action • Shift: Move the first input token to the top of the stack • Reduce • Choose a grammar rule X A B C • Pop C, B, A from the top of the stack • Push X onto the stack • Accept • Shifting the end-of-file marker $ • Parser stops successfully
LR Parse Example (4) • How does the LR parser know when to shift and when to reduce? • Deterministic Finite Automaton (DFA) • DFA is applied to the stack
LR Parse Example (5) • DFA Table 3.19
LR Parse Example (6) • Actions in the transition table • sn: shift into state n; • gn: Goto state n; • rk: Reduce by rule k; • a: accept • (blank): error • ex) id := E • DFA goes from state 1 to 4 to 6 to 11 • if the next token is “;” reduce to S (by rule 2)
LR Parse Example (7) • See the following Steps with the table in Slide 9. • Stack ( ) Input(a:=7; b:=c$) • State = 1, Action = Shift (because stack is empty) • Stack ( ) NT (id) Input(:=7; b:=c$) • State = 1, Action(1,id) = Shift • Stack(id) NT(:=) Input (7; b:=c$) • State = 14, Action(4,:=) = Shift • Stack(id :=) NT(num) Input (; b:=c$) • State = 146, Action(6,num) = Shift • Stack(id := num) NT(;) Input( b:=c$) • State = 14610, Action(10,;) = Reduce with rule 5 Note: NT means Next Token
LR Parse Example (8) • Stack(id := E) NT(;) Input(b:=c$) • State = 14611, Action(11,;) = Reduce with rule 2 • Stack(S) NT(;) Input(b:=c$) • State = 12, Action(2,;) = Shift • Stack(S;) NT(id) Input(:=c$) • State = 123, Action(3,id) = Shift • Stack(S;id) NT(:=) Input(c$) • State = 1234, Action(4,:=) = Shift • Stack(S;id:=) NT(id) Input($) • State = 12346, Action(6,id) = Shift • Stack(S;id:=id) NT($) Input( ) • State = 1234620, Action(20,$) = Reduce with 4
LR Parse Example (9) • Stack(S;id:=id) NT($) Input( ) • State = 1234620, Action(20,$) = Reduce with 4 • Stack(S;id:=E) NT($) Input( ) • State = 1234611, Action(11,$) = Reduce with 2 • Stack(S;S) NT($) Input( ) • State = 1235, Action(5,$) = Reduce with 1 • Stack(S) NT($) Input( ) • State = 12, Action(2,$) = Accept ** Instead of rescanning the stack for each token, the parser can remember the state reached for each stack element.
Parsing Tree from LR Parsing • “Reduce” actions construct the tree • ex) • Stack(id := num) Reduce to Stack(id:=E) E id := num • Stack(id := E) Reduce to Stack(S) S E id := num
LR(k) Parser • Uses • Contents of stack + next k tokens • to decide the action
LR(0) Parser • Looking only at the stack • Making shift/reduce decisions without any lookahead • Too week but good introduction
LR(0) Parser Example (1) • Grammar 0. S’ S$ • S ( L ) • S x • L S • L L , S • Initially, • empty stack • input: complete S-sentence followed by $ • The right-hand side of the S’ rule (rule 0) will be on the input • We indicate this as: S’ .S$ • Period “.” represents the current position of the parser
LR(0) Parser Example (2) • State 1 • Input begins with S • i.e., input begins with any possible right-hand side of an S-production. • State is a set of items. 1 S’ .S$ S .x S .( L ) • Grammar 0. S’ S$ • S ( L ) • S x • L S • L L , S items
LR(0) Parser Example (3) • Shift Action: State 2 • If we shift an x • We have only one item: S x. • The rules S’ .S$ and S .(L) are irrelevant to this shift action. 2 S x. • Grammar 0. S’ S$ • S ( L ) • S x • L S • L L , S
LR(0) Parser Example (4) • Shift Action: State 3 • In State 1, if we shift “(“ • The 3rd item in State 1 yields “S(.L)” • All productions for “L” are included. • Because of “L.S”, all productions for “S” are also included. 3 S (.L) L .L , S L .S S .(L) S .x • Grammar 0. S’ S$ • S ( L ) • S x • L S • L L , S
LR(0) Parser Example (5) • Goto Action: State 4 • In State 1, assume the parsing past some string of tokens derived by the S nonterminal. • This will happen when an x or left parenthesis is shifted, followed (eventually) by a reduction of an S-production • Move dot past the S. 4 S’ S.$ • Grammar 0. S’ S$ • S ( L ) • S x • L S • L L , S
LR(0) Parser Example (6) • Reduce Action • In State 2, we have an item “S x.” which means the top of the stack is ready to reduce using the production “Sx” • In such a state, the parser could perform a reduce action • Grammar 0. S’ S$ • S ( L ) • S x • L S • L L , S
LR(0) Parser Example (7) • Closure(I) • add more items to a set of items when there is a dot to the left of a nonterminal. Closer(I) = repeat for any item Aa.Xb in I for any production Xg add an item “X.g” into I until I does not change return I
LR(0) Parser Example (8) • Goto(I,X) • Moves the dot past the symbol X in all items. Goto(I,X) = set J to the empty set for any item Aa.Xb in I add AaX.b to J return Closure(J) “When X is a terminal, Goto is considered as Shift action” “When X is a nonterminal, Goto is considered as Goto action”
LR(0) Parser Example (9) • LR(0) Parser Construction Algorithm • First, augment the grammar with “S’S$” Initialize T to {Closure({S’.S$})} set of states found Initialize E to empty set of edges found repeat for each state I in T for each item Aa.Xb in I J Goto(I,X) T T + {J} E E + new edge from I to J until E and T did not change in this iteration
LR(0) Parser Example (10) Figure 3.21
LR(0) Parser Example (11) • Compute set “R” of LR(0) reduce action: R { } for each state I in T for each item Aa. in I R R + {(I, Aa)}
LR(0) Parser Example (12) Table 3.22 • For each edge I –(X) J, where X is a terminal, we put action “shift to J” at • (I,X), that is, (I,X) = sJ • For each edge I -(X)J, where X is a nonterminal, we put action “goto J” at • (I,X), that is, (I,X) = gJ • For a state containing an item Aa. , we put a reduce to (I, Y) for every token Y.
LR(0) Parser Example (13) • Parsing “(x, (x))$” • Stack [ ] Input [(x,(x))$] • State = 1, Action = Shift • Stack [ ( ] Input[ x, (x))$] • State = 1, Action{1,( } = Shift to 3 • Stack [ ( x ] Input[ , (x))$ ] • State = 13, Action{3, x} = Shift to 2 • Stack [ ( x , ] Input[ (x))$ ] • State = 132, Action{2, ,} = Reduce with rule 2 (Sx) • Stack [ ( S , ] Input[ (x))$ ] • State = 137, Action{7, ,} = Reduce with rule 3 (LS)
LR(0) Parser Example (14) • Stack [ ( L , ] Input[ (x))$ ] • State = 135, Action{5, ,} = Shift to 8 • Stack [ ( L , ( ] Input[ x))$ ] • State = 1358, Action{8, ( } = Shift to 3 • Stack [ ( L , ( x ] Input[ ))$] • State = 13583, Action{3, x} = Shift to 2 • Stack [ ( L , ( x ) ] Input[ )$ ] • State = 135832, Action{2, ) } = Reduce 2 (Sx) • Stack [ ( L , ( S ) ] Input[ )$] • State = 13583 7, Action{7,)} = Reduce 3 (LS) • Stack [ ( L , ( L ) ] Input[ )$] • State = 135835, Action{5,)} = Shift to 6
LR(0) Parser Example (15) • Stack [ ( L , ( L )) ] Input[$] • State = 135835->6, Action{6,)} =Reduce 1 (S->(L)) • Stack [ ( L, S ) ] Input[$] • State = 13589, Action{9,)} = Reduce 4 (L L , S) • Stack [ ( L ) ] Input[$] • State = 1356, Action{6,)} = Reduce 1 (S (L)) • Stack [ S ] Input[$] • State = 14, Action{4, $} = Accept
LR(0) Feature • No lookahead • A state will shift or reduce, but not both (see the table in the previous slide) • There is no state including multiple items when a reduce action is performed. (see State 2, 6, 7, 9)
Non-LR(0) Grammar • In state 3, on symbol +, there is a duplicate entry. • The parser must shift into state 4 and also reduce by production 2.
FIRST, FOLLOW Set (1) • FIRST(s) • the set of terminals that can begin strings derived from ‘s’ • FOLLOW(X) • set of terminals that can immediately follow X (by the grammar) • If t is in FOLLOW(X), there is any derivation containing Xt. • This can occur if the derivation contains X Y Z t where Y and Z both derive empty. • nullable(X) = true • if X can derive the empty string
FIRST, FOLLOW Set (2) • Example Grammar Z d Y empty X Y Z X Y Z Y c X a • First • FIRST[X] = {a, c} (X is nullable because of XY) • FIRST[Y] = {c} (Y is nullable) • FIRST[Z] = {a, c, d} (because X, Y are nullable, d) • Follow • FOLLOW[X] = {a, c, d} • FOLLOW[Y] = {a, c, d} • FOLLOW[Z] = empty
SLR Parser • SLR: Simple LR • SLR is almost identical to LR(0) • We put reduce action into the table only where indicated by the FOLLOW set.
SLR Parser Reduce Algorithm R { } for each state I in T for each item A a. in I for each token X in FOLLOW(A) R R + {(I, X, Aa)}
SLR Class Feature • No conflict parsing table entry
LR(1) Items • LR(1) Items • grammar production + dot + lookahead symbol • ex) A a.b, x • a: on top of the stack • the string at the head of the input is derivable from “bx” input stack some string ... a derivable from “bx”
LR(1) Closure Closure(I) = repeat for any item (A a.Xb, z) in I for any production Xg for any w in FIRST(bz) I I + {(X.g, w)} until I does not change return I
LR(1) Goto Goto(I,X) = J { } for any item (A a.Xb, z) in I add (A aX.b, z) to J return Closure(J)
LR(1) Start State • Closure of the item (S’ .S$, ?) • ? means, we don’t care any lookahead. • Because $ (eof marker) will never be shifted.
LR(1) Reduce R { } for each state I in T for each item (A a., z) in I R R + {(I, z, Aa)} (I, z, Aa): in state I, on lookahead symbol z, the parser will reduce by rule Aa
LR(1) Example #1 • Grammar 0. S’ S$ • S V = E • S E • E V • V x • V *E • This is not SLR grammar nor LR(0) grammar • Try to construct parsing tables! • There are conflicts in SLR or LR(0) parsing tables.
LR(1) Example #2 • Start State • Closure of (S’.S$, ?) Closure(I) = repeat for any item (A a.Xb, z) in I for any production Xg for any w in FIRST(bz) I I + {(X.g, w)} until I does not change return I 1 S’ .S$ ? S .V=E $ S .E $ E .V $ V .x $,= V .*E $,= FIRST($?) = {$} FIRST($) = {$} FIRST(=E$) = {=} • Grammar 0. S’ S$ • S V = E • S E • E V • V x • V *E
LR(1) Example #3 • State 2 • Goto by ‘S’ from State 1 2 S’ S.$ ? 1 S’ .S$ ? S .V=E $ S .E $ E .V $ V .x $,= V .*E $,= S • Grammar 0. S’ S$ • S V = E • S E • E V • V x • V *E
LR(1) Example #4 • State 3 • Goto by ‘V’ from State 1 2 S’ S.$ ? 1 S’ .S$ ? S .V=E $ S .E $ E .V $ V .x $,= V .*E $,= S 3 S V.=E $ E .V $ • Grammar 0. S’ S$ • S V = E • S E • E V • V x • V *E V
LR(1) Example #5 • States Diagram
LR(1) Example #6 • LR(1) Parsing Table