1 / 18

LL(k) and LR(k) Parsers

LL(k) and LR(k) Parsers. CS 6800 12/11/12 Matthew Rodgers. LL(k) and LR(k). What are LL and LR parsers? What grammars do they parse? What is the difference between LL and LR? Why do we care?. Top-Down vs. Bottom-Up Parsers. Top-Down. Bottom-Up. LR(k) Parsers are Bottom-Up Parsers

alima
Download Presentation

LL(k) and LR(k) Parsers

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. LL(k) and LR(k) Parsers CS 6800 12/11/12 Matthew Rodgers

  2. LL(k) and LR(k) • What are LL and LR parsers? • What grammars do they parse? • What is the difference between LL and LR? • Why do we care?

  3. Top-Down vs. Bottom-Up Parsers Top-Down Bottom-Up LR(k) Parsers are Bottom-Up Parsers LR(k) Grammars is exactly the set of Deterministic Context-Free Grammars LR(k), for some k, is also LR(1) • LL(k) parsers are Top-Down Parsers • LL(1) is Deterministic • The way you are most likely familiar with how to parsing grammars

  4. Generalized LL Parser • Consider the following grammar: • S → F • S→ (S+F) • F→ a • Input: (a+a) • The parsing table for this grammar is shown

  5. LL Parser Cont. • The stack initializes with the start symbol, S and is compared to the first symbol in the input • Since it does not find an ( on the stack, it looks at the table to see which rule to apply • After applying the rule, it attempts again • It finds the ( in both the input string and the top of the stack so it removes both • The parser continues to do this until it reaches the end symbol, $, or rejects the string

  6. LR(k) • Given an LR(1) grammar, we can produce a shift-reduce parser table • Shift– “Shifts” an input symbol onto the parser’s stack and builds a node in the parse tree labeled by that symbol • Reduce– “Reduces” a string of symbols from the top of the stack to a non-terminal symbol using a grammar rule • When it does this it builds the piece of the parse tree • However, many LR(1) languages have too large of a parse table to be practical • Instead we use LALR parsing

  7. Shift-Reduce Example S→S + E | E E →num | (S)

  8. Building an LR(0) Parser

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

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

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

  12. 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•

  13. 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•

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

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

  16. Final Parse Table

  17. LALR Parsing • “Lookahead” LR Parsing– Deterministic, shift-reduce parser • Most practical (non-Natural) languages can be described by an LALR • LALR Parser tables are fairly small • Yacc is a Parser-Generation tool that creates LALR parsers

  18. References • "LL Parser." Wikipedia. Wikimedia Foundation, 11 Sept. 2012. Web. 12 Nov. 2012. <http://en.wikipedia.org/wiki/LL_parser>. • "LR Parser." Wikipedia. Wikimedia Foundation, 11 July 2012. Web. 12 Nov. 2012. <http://en.wikipedia.org/wiki/LR_parser>. • Rich, Elaine. "Context-Free Parsing." Automata, Computability and Complexity: Theory and Applications. Upper Saddle River, NJ: Pearson Prentice Hall, 2008. 323-50. Print.

More Related