1 / 60

Bottom-up Parser Table Construction

Bottom-up Parser Table Construction. David Walker COS 320. Programming Language Parsing. LL(k) (top-down) parsing compute nullable, first, follow sets then parsing table use synchronizing tokens (Follow(X)) for error recovery derive ML program

phiala
Download Presentation

Bottom-up Parser Table Construction

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 Table Construction David Walker COS 320

  2. Programming Language Parsing • LL(k) (top-down) parsing • compute nullable, first, follow sets then parsing table • use synchronizing tokens (Follow(X)) for error recovery • derive ML program • LR(k) parsing -- more powerful than LL(k) parsers • according to parser table: • shift tokens from input on to stack • reduce stack symbols using grammar rules • accept or signal errors • Fisher-Burke error repair • global technique (try every possible insertion/replacement/substitution in k-token window) • Now the magic: how to construct the parser table

  3. finally the magic: how to construct an LR parser table • At every point in the parse, the LR parser table tells us what to do next • shift, reduce, error or accept • To do so, the LR parser keeps track of the parse “state” ==> a state in a finite automaton yet to read NUM PLUS ( NUM PLUS NUM ) PLUS NUM input: stack: exp PLUS ( exp PLUS

  4. finally the magic: how to construct an LR parser table yet to read NUM PLUS ( NUM PLUS NUM ) PLUS NUM input: stack: exp PLUS ( exp PLUS 5 minus finite automaton; terminals and non terminals label edges exp plus 2 3 exp ( exp 1 4

  5. finally the magic: how to construct an LR parser table yet to read NUM PLUS ( NUM PLUS NUM ) PLUS NUM input: stack: exp PLUS ( exp PLUS 5 minus finite automaton; terminals and non terminals label edges exp plus 2 3 exp ( exp 1 4 state-annotated stack: 1

  6. finally the magic: how to construct an LR parser table yet to read NUM PLUS ( NUM PLUS NUM ) PLUS NUM input: stack: exp PLUS ( exp PLUS 5 minus finite automaton; terminals and non terminals label edges exp plus 2 3 exp ( exp 1 4 state-annotated stack: 1 exp 2

  7. finally the magic: how to construct an LR parser table yet to read NUM PLUS ( NUM PLUS NUM ) PLUS NUM input: stack: exp PLUS ( exp PLUS 5 minus finite automaton; terminals and non terminals label edges exp plus 2 3 exp ( exp 1 4 state-annotated stack: 1 exp 2 PLUS 3

  8. finally the magic: how to construct an LR parser table yet to read NUM PLUS ( NUM PLUS NUM ) PLUS NUM input: stack: exp PLUS ( exp PLUS 5 minus finite automaton; terminals and non terminals label edges exp plus 2 3 exp ( exp 1 4 this state and input tell us what to do next state-annotated stack: 1 exp 2 PLUS 3 ( 1 exp 2 PLUS 3

  9. The Parse Table • At every point in the parse, the LR parser table tells us what to do next according to the automaton state at the top of the stack • shift, reduce, error or accept

  10. The Parse Table • Reducing by rule k is broken into two steps: • current stack is: A 8 B 3 C ....... 7RHS12 • rewrite the stack according to X ::=RHS: A 8 B 3 C ....... 7X • figure out state on top of stack (ie: goto 13) A 8 B 3 C ....... 7X 13

  11. The Parse Table • Reducing by rule k is broken into two steps: • current stack is: A 8 B 3 C ....... 7RHS12 • rewrite the stack according to X ::=RHS: A 8 B 3 C ....... 7X • figure out state on top of stack (ie: goto 13) A 8 B 3 C ....... 7X 13

  12. LR(0) parsing • each state in the automaton represents a collection of LR(0) items: • an item is a rule from the grammar combined with “@” to indicate where the parser currently is in the input • eg: S’ ::= @ S $ indicates that the parser is just beginning to parse this rule and it expects to be able to parse S then $ next • A whole automaton state looks like this: 1 S’ ::= @ S $ S ::= @ ( L ) S ::= @ x collection of LR(0) items state number • LR(1) states look very similar, it is just that the items contain some look-ahead info

  13. LR(0) parsing • To construct states, we begin with a particular LR(0) item and construct its closure • the closure adds more items to a set when the “@” appears to the left of a non-terminal • if the state includes X ::= s @ Y s’ and Y ::= t is a rule then the state also includes Y ::= @ t Grammar: 1 • 0. S’ ::= S $ • S ::= ( L ) • S ::= x • L ::= S • L ::= L , S S’ ::= @ S $

  14. LR(0) parsing • To construct states, we begin with a particular LR(0) item and construct its closure • the closure adds more items to a set when the “@” appears to the left of a non-terminal • if the state includes X ::= s @ Y s’ and Y ::= t is a rule then the state also includes Y ::= @ t Grammar: 1 • 0. S’ ::= S $ • S ::= ( L ) • S ::= x • L ::= S • L ::= L , S S’ ::= @ S $ S ::= @ ( L )

  15. LR(0) parsing • To construct states, we begin with a particular LR(0) item and construct its closure • the closure adds more items to a set when the “@” appears to the left of a non-terminal • if the state includes X ::= s @ Y s’ and Y ::= t is a rule then the state also includes Y ::= @ t Grammar: 1 • 0. S’ ::= S $ • S ::= ( L ) • S ::= x • L ::= S • L ::= L , S S’ ::= @ S $ S ::= @ ( L ) S ::= @ x Full Closure

  16. LR(0) parsing • To construct an LR(0) automaton: • start with start rule & compute initial state with closure • pick one of the items from the state and move “@” to the right one symbol (as if you have just parsed the symbol) • this creates a new item ... • ... and a new state when you compute the closure of the new item • mark the edge between the two states with: • a terminal T, if you moved “@” over T • a non-terminal X, if you moved “@” over X • continue until there are no further ways to move “@” across items and generate new states or new edges in the automaton

  17. Grammar: • 0. S’ ::= S $ • S ::= ( L ) • S ::= x • L ::= S • L ::= L , S S’ ::= @ S $ S ::= @ ( L ) S ::= @ x

  18. Grammar: • 0. S’ ::= S $ • S ::= ( L ) • S ::= x • L ::= S • L ::= L , S S’ ::= @ S $ S ::= @ ( L ) S ::= @ x S S’ ::= S @ $

  19. Grammar: • 0. S’ ::= S $ • S ::= ( L ) • S ::= x • L ::= S • L ::= L , S ( S ::= ( @ L ) L ::= @ S L ::= @ L , S S ::= @ ( L ) S ::= @ x S’ ::= @ S $ S ::= @ ( L ) S ::= @ x S S’ ::= S @ $

  20. Grammar: • 0. S’ ::= S $ • S ::= ( L ) • S ::= x • L ::= S • L ::= L , S ( S ::= ( @ L ) L ::= @ S L ::= @ L , S S ::= @ ( L ) S ::= @ x S’ ::= @ S $ S ::= @ ( L ) S ::= @ x S S’ ::= S @ $

  21. Grammar: • 0. S’ ::= S $ • S ::= ( L ) • S ::= x • L ::= S • L ::= L , S ( S ::= ( @ L ) L ::= @ S L ::= @ L , S S ::= @ ( L ) S ::= @ x S’ ::= @ S $ S ::= @ ( L ) S ::= @ x S S’ ::= S @ $

  22. Grammar: • 0. S’ ::= S $ • S ::= ( L ) • S ::= x • L ::= S • L ::= L , S ( S ::= ( @ L ) L ::= @ S L ::= @ L , S S ::= @ ( L ) S ::= @ x S’ ::= @ S $ S ::= @ ( L ) S ::= @ x S ::= ( L @ ) L ::= L @ , S L S S’ ::= S @ $

  23. Grammar: • 0. S’ ::= S $ • S ::= ( L ) • S ::= x • L ::= S • L ::= L , S ( S ::= ( @ L ) L ::= @ S L ::= @ L , S S ::= @ ( L ) S ::= @ x S’ ::= @ S $ S ::= @ ( L ) S ::= @ x S ::= ( L @ ) L ::= L @ , S L S S S’ ::= S @ $ L ::= S @

  24. Grammar: • 0. S’ ::= S $ • S ::= ( L ) • S ::= x • L ::= S • L ::= L , S S ::= x @ x ( S ::= ( @ L ) L ::= @ S L ::= @ L , S S ::= @ ( L ) S ::= @ x S’ ::= @ S $ S ::= @ ( L ) S ::= @ x S ::= ( L @ ) L ::= L @ , S L S S S’ ::= S @ $ L ::= S @

  25. Grammar: • 0. S’ ::= S $ • S ::= ( L ) • S ::= x • L ::= S • L ::= L , S S ::= x @ x ( S ::= ( @ L ) L ::= @ S L ::= @ L , S S ::= @ ( L ) S ::= @ x S’ ::= @ S $ S ::= @ ( L ) S ::= @ x S ::= ( L @ ) L ::= L @ , S L S ) S S’ ::= S @ $ S ::= ( L ) @ L ::= S @

  26. Grammar: • 0. S’ ::= S $ • S ::= ( L ) • S ::= x • L ::= S • L ::= L , S S ::= x @ L ::= L , @ S S ::= @ ( L ) S ::= @ x x ( S ::= ( @ L ) L ::= @ S L ::= @ L , S S ::= @ ( L ) S ::= @ x S’ ::= @ S $ S ::= @ ( L ) S ::= @ x , S ::= ( L @ ) L ::= L @ , S L S ) S S’ ::= S @ $ S ::= ( L ) @ L ::= S @

  27. Grammar: • 0. S’ ::= S $ • S ::= ( L ) • S ::= x • L ::= S • L ::= L , S S ::= x @ L ::= L , @ S S ::= @ ( L ) S ::= @ x x ( S ::= ( @ L ) L ::= @ S L ::= @ L , S S ::= @ ( L ) S ::= @ x S’ ::= @ S $ S ::= @ ( L ) S ::= @ x , S ::= ( L @ ) L ::= L @ , S L S ) S S’ ::= S @ $ S ::= ( L ) @ L ::= S @

  28. Grammar: • 0. S’ ::= S $ • S ::= ( L ) • S ::= x • L ::= S • L ::= L , S L ::= L , S @ S S ::= x @ L ::= L , @ S S ::= @ ( L ) S ::= @ x x ( S ::= ( @ L ) L ::= @ S L ::= @ L , S S ::= @ ( L ) S ::= @ x S’ ::= @ S $ S ::= @ ( L ) S ::= @ x , S ::= ( L @ ) L ::= L @ , S L S ) S S’ ::= S @ $ S ::= ( L ) @ L ::= S @

  29. Grammar: • 0. S’ ::= S $ • S ::= ( L ) • S ::= x • L ::= S • L ::= L , S L ::= L , S @ S S ::= x @ L ::= L , @ S S ::= @ ( L ) S ::= @ x x ( ( S ::= ( @ L ) L ::= @ S L ::= @ L , S S ::= @ ( L ) S ::= @ x S’ ::= @ S $ S ::= @ ( L ) S ::= @ x , S ::= ( L @ ) L ::= L @ , S L S ) S S’ ::= S @ $ S ::= ( L ) @ L ::= S @

  30. Grammar: • 0. S’ ::= S $ • S ::= ( L ) • S ::= x • L ::= S • L ::= L , S L ::= L , S @ S x S ::= x @ L ::= L , @ S S ::= @ ( L ) S ::= @ x x x ( ( S ::= ( @ L ) L ::= @ S L ::= @ L , S S ::= @ ( L ) S ::= @ x S’ ::= @ S $ S ::= @ ( L ) S ::= @ x , S ::= ( L @ ) L ::= L @ , S L S ) S S’ ::= S @ $ S ::= ( L ) @ L ::= S @

  31. Grammar: Assigning numbers to states: • 0. S’ ::= S $ • S ::= ( L ) • S ::= x • L ::= S • L ::= L , S L ::= L , S @ 9 S 8 x 2 S ::= x @ L ::= L , @ S S ::= @ ( L ) S ::= @ x x x ( ( 1 S ::= ( @ L ) L ::= @ S L ::= @ L , S S ::= @ ( L ) S ::= @ x S’ ::= @ S $ S ::= @ ( L ) S ::= @ x , ( 5 S ::= ( L @ ) L ::= L @ , S L 3 S ) S 4 S’ ::= S @ $ 6 S ::= ( L ) @ 7 L ::= S @

  32. computing parse table • State i contains X ::= s @ $ ==> table[i,$] = a • State i contains rule k: X ::= s @ ==> table[i,T] = rk for all terminals T • Transition from i to j marked with T ==> table[i,T] = sj • Transition from i to j marked with X ==> table[i,X] = gj

  33. L ::= L , S @ 9 • 0. S’ ::= S $ • S ::= ( L ) • S ::= x • L ::= S • L ::= L , S S 8 x 2 S ::= x @ L ::= L , @ S S ::= @ ( L ) S ::= @ x x x ( ( 1 S ::= ( @ L ) L ::= @ S L ::= @ L , S S ::= @ ( L ) S ::= @ x S’ ::= @ S $ S ::= @ ( L ) S ::= @ x , ( 5 S ::= ( L @ ) L ::= L @ , S L 3 S ) S 7 6 S ::= ( L ) @ 4 S’ ::= S @ $ L ::= S @

  34. L ::= L , S @ 9 • 0. S’ ::= S $ • S ::= ( L ) • S ::= x • L ::= S • L ::= L , S S 8 x 2 2 S ::= x @ L ::= L , @ S S ::= @ ( L ) S ::= @ x x x ( ( 1 S ::= ( @ L ) L ::= @ S L ::= @ L , S S ::= @ ( L ) S ::= @ x S’ ::= @ S $ S ::= @ ( L ) S ::= @ x , ( 5 S ::= ( L @ ) L ::= L @ , S L 3 S ) S 7 6 S ::= ( L ) @ 4 S’ ::= S @ $ L ::= S @

  35. L ::= L , S @ 9 • 0. S’ ::= S $ • S ::= ( L ) • S ::= x • L ::= S • L ::= L , S S 8 x 2 2 S ::= x @ L ::= L , @ S S ::= @ ( L ) S ::= @ x x x ( ( 1 S ::= ( @ L ) L ::= @ S L ::= @ L , S S ::= @ ( L ) S ::= @ x S’ ::= @ S $ S ::= @ ( L ) S ::= @ x , ( 5 S ::= ( L @ ) L ::= L @ , S L 3 S ) S 7 6 S ::= ( L ) @ 4 S’ ::= S @ $ L ::= S @

  36. L ::= L , S @ 9 • 0. S’ ::= S $ • S ::= ( L ) • S ::= x • L ::= S • L ::= L , S S 8 x 2 2 S ::= x @ L ::= L , @ S S ::= @ ( L ) S ::= @ x x x ( ( 1 S ::= ( @ L ) L ::= @ S L ::= @ L , S S ::= @ ( L ) S ::= @ x S’ ::= @ S $ S ::= @ ( L ) S ::= @ x , ( 5 S ::= ( L @ ) L ::= L @ , S L 3 S ) S 7 6 S ::= ( L ) @ 4 S’ ::= S @ $ L ::= S @

  37. L ::= L , S @ 9 • 0. S’ ::= S $ • S ::= ( L ) • S ::= x • L ::= S • L ::= L , S S 8 x 2 2 S ::= x @ L ::= L , @ S S ::= @ ( L ) S ::= @ x x x ( ( 1 S ::= ( @ L ) L ::= @ S L ::= @ L , S S ::= @ ( L ) S ::= @ x S’ ::= @ S $ S ::= @ ( L ) S ::= @ x , ( 5 S ::= ( L @ ) L ::= L @ , S L 3 S ) S 7 6 S ::= ( L ) @ 4 S’ ::= S @ $ L ::= S @

  38. L ::= L , S @ 9 • 0. S’ ::= S $ • S ::= ( L ) • S ::= x • L ::= S • L ::= L , S S 8 x 2 2 S ::= x @ L ::= L , @ S S ::= @ ( L ) S ::= @ x x x ( ( 1 S ::= ( @ L ) L ::= @ S L ::= @ L , S S ::= @ ( L ) S ::= @ x S’ ::= @ S $ S ::= @ ( L ) S ::= @ x , ( 5 S ::= ( L @ ) L ::= L @ , S L 3 S ) S 7 6 S ::= ( L ) @ 4 S’ ::= S @ $ L ::= S @

  39. L ::= L , S @ 9 • 0. S’ ::= S $ • S ::= ( L ) • S ::= x • L ::= S • L ::= L , S S 8 x 2 2 S ::= x @ L ::= L , @ S S ::= @ ( L ) S ::= @ x x x ( ( 1 S ::= ( @ L ) L ::= @ S L ::= @ L , S S ::= @ ( L ) S ::= @ x S’ ::= @ S $ S ::= @ ( L ) S ::= @ x , ( 5 S ::= ( L @ ) L ::= L @ , S L 3 S ) S 7 6 S ::= ( L ) @ 4 S’ ::= S @ $ L ::= S @

  40. L ::= L , S @ 9 • 0. S’ ::= S $ • S ::= ( L ) • S ::= x • L ::= S • L ::= L , S S 8 x 2 2 S ::= x @ L ::= L , @ S S ::= @ ( L ) S ::= @ x x x ( ( 1 S ::= ( @ L ) L ::= @ S L ::= @ L , S S ::= @ ( L ) S ::= @ x S’ ::= @ S $ S ::= @ ( L ) S ::= @ x , ( 5 S ::= ( L @ ) L ::= L @ , S L 3 S ) S 7 6 S ::= ( L ) @ 4 S’ ::= S @ $ L ::= S @

  41. 0. S’ ::= S $ • S ::= ( L ) • S ::= x • L ::= S • L ::= L , S yet to read ( x , x ) $ input: stack: 1

  42. 0. S’ ::= S $ • S ::= ( L ) • S ::= x • L ::= S • L ::= L , S yet to read ( x , x ) $ input: stack: 1 ( 3

  43. S’ ::= S $ • S ::= ( L ) • S ::= x • L ::= S • L ::= L , S yet to read ( x , x ) $ input: stack: 1 ( 3 x 2

  44. 0. S’ ::= S $ • S ::= ( L ) • S ::= x • L ::= S • L ::= L , S yet to read ( x , x ) $ input: stack: 1 ( 3 S

  45. 0. S’ ::= S $ • S ::= ( L ) • S ::= x • L ::= S • L ::= L , S yet to read ( x , x ) $ input: stack: 1 ( 3 S 7

  46. 0. S’ ::= S $ • S ::= ( L ) • S ::= x • L ::= S • L ::= L , S yet to read ( x , x ) $ input: stack: 1 ( 3 L

  47. 0. S’ ::= S $ • S ::= ( L ) • S ::= x • L ::= S • L ::= L , S yet to read ( x , x ) $ input: stack: 1 ( 3 L 5

  48. 0. S’ ::= S $ • S ::= ( L ) • S ::= x • L ::= S • L ::= L , S yet to read ( x , x ) $ input: stack: 1 ( 3 L 5 , 8

  49. 0. S’ ::= S $ • S ::= ( L ) • S ::= x • L ::= S • L ::= L , S yet to read ( x , x ) $ input: stack: 1 ( 3 L 5 , 8 x 2

  50. 0. S’ ::= S $ • S ::= ( L ) • S ::= x • L ::= S • L ::= L , S yet to read ( x , x ) $ input: stack: 1 ( 3 L 5 , 8 S

More Related