1 / 20

LR(K) Grammars

LR(K) Grammars. Hitesh keelapudi. Bottom-Up Parsing. LR(k) Parsers are Bottom-Up Parsers LR(k) Grammars is exactly the set of Deterministic Context-Free Grammars. Bottom-Up Parsing. Start at the leaves and grow toward root As input is consumed, encode possibilities in an internal state

farrah
Download Presentation

LR(K) Grammars

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. LR(K) Grammars Hitesh keelapudi

  2. Bottom-Up Parsing • LR(k) Parsers are Bottom-Up Parsers • LR(k) Grammars is exactly the set of Deterministic Context-Free Grammars

  3. Bottom-Up Parsing • Start at the leaves and grow toward root • As input is consumed, encode possibilities in an internal state • A powerful parsing technology • LR grammars • Construct right-most derivation of program • Left-recursive grammar, virtually all programming language are left-recursive • Easier to express syntax

  4. Bottom-Up Parsing • Right-most derivation • Start with the tokens • End with the start symbol • Match substring on RHS of production, replace by LHS • Shift-reduce parsers • Parsers for LR grammars • Automatic parser generators (yacc, bison)

  5. Bottom-Up Parsing • Example Bottom-Up Parsing S  S + E | E E  num | (S) (1+2+(3+4))+5  (E+2+(3+4))+5 (S+2+(3+4))+5 (S+E+(3+4))+5 (S+(3+4))+5 (S+(E+4))+5 (S+(S+4))+5 (S+(S+E))+5 (S+(S))+5 (S+E)+5 (S)+5 E+5 S+5 S+E S

  6. Terminology LR(k) • Left-to-right scan of input • Right-most derivation • k symbol lookahead • [Bottom-up or shift-reduce] parsing or LR parser • Perform post-order traversal of parse tree

  7. Shift-Reduce Parsing • Parsing actions: • A sequence of shift and reduce operations • Parser state: • A stack of terminals and non-terminals (grows to the right) • Current derivation step: = stack + input

  8. Shift-Reduce Parsing

  9. Shift-Reduce Actions • Parsing is a sequence of shift and reduces • Shift: move look-ahead token to stack • Reduce: Replace symbols from top of stack with non-terminal symbols X corresponding to the production: X  β (e.g., pop β, push X)

  10. Shift-Reduce Parsing SS + E | E E  num | (S)

  11. Building an LR(0) Parser

  12. Lets Build An LR(0) Parser! • First we shall define a simple grammar • E → E * B • E → E + B • E → B • B → 0 • B → 1 • We also add a new rule, S → E, which is used by the parser as a final accepting rule

  13. Items • To create a parsing table for this grammar we must introduce a special symbol, ∙, which indicates the current position for which the parser has already read symbols on the input and what to expect next • E.g. E → E ∙ + B • This shows that the E has already been processed and the parser is looking for a + symbol next • Each of these above rules is called an item • There is an item for each position the dot symbol can take along the right-hand side of the rule

  14. Item Sets • Since a parser may not know which grammar rule to use in advance, when creating our table we must use sets of items to consider all the possibilities • E.g. • S → • E • E → • E * B • E → • E + B • E → • B • B → • 0 • B → • 1 • The first line is the initial rule for the item set, but since we need to consider all possibilities when we come to a non-terminal, we must create a closure around the non-terminal E, in this case. (By extension, we must do the same for B as shown by the 5th and 6th items.)

  15. Item Sets for Our Example • Set 0 • S → • E • E → • E * B • E → • E + B • E → • B • B → • 0 • B → • 1 • Set 1 • B →  0 • • Set 2 • B →  1 • • Set 3 • S →  E • • E →  E • * B • E →  E • + B • Set 4 • E →  B•

  16. Set 5 • E →  E * • B • B → • 0 • B → • 1 • Set 6 • E →  E + •B • B → • 0 • B → • 1 • Set 7 • E →  E * B• • Set 8 • E →  E + B•

  17. Transition Portion of Parse Table • Each of the transitions can be found by following the item sets to where the new item set is created from • Item Set 7 Spawned as a result of Item Set 5

  18. Constructing the Table • After finishing creating the item sets and the transitions, follow the steps below to finish the table 1) The columns for nonterminals are copied to the goto table. 2) The columns for the terminals are copied to the action table as shift actions. 3) An extra column for '$' (end of input) is added to the action table that contains acc for every item set that contains S → E •. 4) If an item set i contains an item of the form A → w • and A → w is rule m with m > 0 then the row for state i in the action table is completely filled with the reduce action rm.

  19. Final Parse Table

  20. Thank you

More Related