1 / 55

Bottom-Up Parser

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.

tangia
Télécharger la présentation

Bottom-Up Parser

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Bottom-Up Parser

  2. Reference • Andrew W. Appel, Modern Compiler Implementation in Java, 2nd edition, Cambridge Press, 2002. Section 3.2 and 3.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.

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

  5. LR(k) • L: left-to-right parse • R: Rightmost derivation • k: k-token lookahead

  6. 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 )

  7. LR Parse Example (2) Figure 3.18

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

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

  10. LR Parse Example (5) • DFA Table 3.19

  11. 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)

  12. 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 = 14, Action(4,:=) = Shift • Stack(id :=) NT(num) Input (; b:=c$) • State = 146, Action(6,num) = Shift • Stack(id := num) NT(;) Input( b:=c$) • State = 14610, Action(10,;) = Reduce with rule 5 Note: NT means Next Token

  13. LR Parse Example (8) • Stack(id := E) NT(;) Input(b:=c$) • State = 14611, Action(11,;) = Reduce with rule 2 • Stack(S) NT(;) Input(b:=c$) • State = 12, Action(2,;) = Shift • Stack(S;) NT(id) Input(:=c$) • State = 123, Action(3,id) = Shift • Stack(S;id) NT(:=) Input(c$) • State = 1234, Action(4,:=) = Shift • Stack(S;id:=) NT(id) Input($) • State = 12346, Action(6,id) = Shift • Stack(S;id:=id) NT($) Input( ) • State = 1234620, Action(20,$) = Reduce with 4

  14. LR Parse Example (9) • Stack(S;id:=id) NT($) Input( ) • State = 1234620, Action(20,$) = Reduce with 4 • Stack(S;id:=E) NT($) Input( ) • State = 1234611, Action(11,$) = Reduce with 2 • Stack(S;S) NT($) Input( ) • State = 1235, Action(5,$) = Reduce with 1 • Stack(S) NT($) Input( ) • State = 12, Action(2,$) = Accept ** Instead of rescanning the stack for each token, the parser can remember the state reached for each stack element.

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

  16. LR(k) Parser • Uses • Contents of stack + next k tokens • to decide the action

  17. LR(0) Parser • Looking only at the stack • Making shift/reduce decisions without any lookahead • Too week but good introduction

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

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

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

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

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

  23. 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 “Sx” • In such a state, the parser could perform a reduce action • Grammar 0. S’  S$ • S  ( L ) • S  x • L  S • L  L , S

  24. 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 Aa.Xb in I for any production Xg add an item “X.g” into I until I does not change return I

  25. 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 Aa.Xb in I add AaX.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”

  26. 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 Aa.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

  27. LR(0) Parser Example (10) Figure 3.21

  28. LR(0) Parser Example (11) • Compute set “R” of LR(0) reduce action: R  { } for each state I in T for each item Aa. in I R  R + {(I, Aa)}

  29. 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 Aa. , we put a reduce to (I, Y) for every token Y.

  30. 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 = 13, Action{3, x} = Shift to 2 • Stack [ ( x , ] Input[ (x))$ ] • State = 132, Action{2, ,} = Reduce with rule 2 (Sx) • Stack [ ( S , ] Input[ (x))$ ] • State = 137, Action{7, ,} = Reduce with rule 3 (LS)

  31. LR(0) Parser Example (14) • Stack [ ( L , ] Input[ (x))$ ] • State = 135, Action{5, ,} = Shift to 8 • Stack [ ( L , ( ] Input[ x))$ ] • State = 1358, Action{8, ( } = Shift to 3 • Stack [ ( L , ( x ] Input[ ))$] • State = 13583, Action{3, x} = Shift to 2 • Stack [ ( L , ( x ) ] Input[ )$ ] • State = 135832, Action{2, ) } = Reduce 2 (Sx) • Stack [ ( L , ( S ) ] Input[ )$] • State = 13583  7, Action{7,)} = Reduce 3 (LS) • Stack [ ( L , ( L ) ] Input[ )$] • State = 135835, Action{5,)} = Shift to 6

  32. LR(0) Parser Example (15) • Stack [ ( L , ( L )) ] Input[$] • State = 135835->6, Action{6,)} =Reduce 1 (S->(L)) • Stack [ ( L, S ) ] Input[$] • State = 13589, Action{9,)} = Reduce 4 (L  L , S) • Stack [ ( L ) ] Input[$] • State = 1356, Action{6,)} = Reduce 1 (S  (L)) • Stack [ S ] Input[$] • State = 14, Action{4, $} = Accept

  33. 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)

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

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

  36. 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 XY) • 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

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

  38. 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, Aa)}

  39. SLR Class Feature • No conflict parsing table entry

  40. 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”

  41. LR(1) Closure Closure(I) = repeat for any item (A  a.Xb, z) in I for any production Xg for any w in FIRST(bz) I  I + {(X.g, w)} until I does not change return I

  42. 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)

  43. LR(1) Start State • Closure of the item (S’  .S$, ?) • ? means, we don’t care any lookahead. • Because $ (eof marker) will never be shifted.

  44. LR(1) Reduce R  { } for each state I in T for each item (A  a., z) in I R  R + {(I, z, Aa)} (I, z, Aa): in state I, on lookahead symbol z, the parser will reduce by rule Aa

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

  46. 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 Xg 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

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

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

  49. LR(1) Example #5 • States Diagram

  50. LR(1) Example #6 • LR(1) Parsing Table

More Related