lr parsing
play

LR Parsing Dr. Mattox Beckman University of Illinois at - PowerPoint PPT Presentation

Introduction The Automata LR Parsing Dr. Mattox Beckman University of Illinois at Urbana-Champaign Department of Computer Science Introduction The Automata Objectives You should be able to ... Explain the difference between an LL and LR


  1. Introduction The Automata LR Parsing Dr. Mattox Beckman University of Illinois at Urbana-Champaign Department of Computer Science

  2. Introduction The Automata Objectives You should be able to ... ◮ Explain the difference between an LL and LR parser. ◮ Generate the fjnite state machine from an LR grammar. ◮ Use the state machine to detect ambiguity. Further reading: See Dragon Book §4.x.

  3. Introduction The Automata What Is LR Parsing? ◮ What is an LR parser? ◮ An LR parser uses a L eft-to-right scan and produces a R ightmost derivation. ◮ A.k.a. bottom-up parsing ◮ Uses a push-down automata to do the work. ◮ There are four actions. Shift Consume a token from the input. Reduce Build a tree from components. Goto Jump to a different state, after a reduce. Accept Signal that we’re done.

  4. Introduction The Automata Shifting Shifting involves three steps. 1. Consume a token from the input. 2. Push the token and the current state to the stack. 3. Go to the next state. Example: a b I 2 S → a b • I 0 S → • a b I 1 S → a • b r 1 { $ } Grammar S → a b Input • a b $ Stack (empty) Current State 0 We will shift the a and then we go to state 1.

  5. Introduction The Automata Shifting Shifting involves three steps. 1. Consume a token from the input. 2. Push the token and the current state to the stack. 3. Go to the next state. Example: a b I 2 S → a b • I 0 S → • a b I 1 S → a • b r 1 { $ } Grammar S → a b Input a • b $ Stack 0, a Current State 1 We will shift the b and then we go to state 2.

  6. Introduction The Automata Shifting Shifting involves three steps. 1. Consume a token from the input. 2. Push the token and the current state to the stack. 3. Go to the next state. Example: a b I 2 S → a b • I 0 S → • a b I 1 S → a • b r 1 { $ } Grammar S → a b Input a b • $ Stack 0, a , 1, b Current State 2 What should happen now?

  7. Introduction The Automata Reducing Reducing involves three steps. 1. Pop the tokens and states from the stack. (How many?) 2. Return to the last state popped. 3. Construct a new tree from the popped tokens. Example: a b I 2 S → a b • I 0 S → • a b I 1 S → a • b r 1 { $ } Grammar S → a b Input a b • $ Stack 0, a , 1, b Current State 2 We are ready to reduce.

  8. Introduction The Automata Reducing Reducing involves three steps. 1. Pop the tokens and states from the stack. (How many?) 2. Return to the last state popped. 3. Construct a new tree from the popped tokens. Example: a b I 2 S → a b • I 0 S → • a b I 1 S → a • b r 1 { $ } Grammar S → a b Input a b • $ Stack Current State 0 Now we have an S tree. Go To or Accept could happen here.

  9. Introduction The Automata A More Complex Example a E b I 1 S → a • E b I 4 S → a E b • I 0 S → • a E b I 2 S → a E • b E → • x r 1 { $ } x I 3 E → x • { b } r 2 Grammar S a E b → E x → Stack (Empty) Input • a x b $ Current State 0

  10. Introduction The Automata A More Complex Example a E b I 1 S → a • E b I 4 S → a E b • I 0 S → • a E b I 2 S → a E • b E → • x r 1 { $ } x I 3 E → x • { b } r 2 Grammar S a E b → E x → Stack 0, a Input a • x b $ Current State 1

  11. Introduction The Automata A More Complex Example a E b I 1 S → a • E b I 4 S → a E b • I 0 S → • a E b I 2 S → a E • b E → • x r 1 { $ } x I 3 E → x • { b } r 2 Grammar S a E b → E x → Stack 0, a ,1, x Input a x • b $ Current State 4

  12. Introduction The Automata A More Complex Example a E b I 1 S → a • E b I 4 S → a E b • I 0 S → • a E b I 2 S → a E • b E → • x r 1 { $ } x I 3 E → x • { b } r 2 Grammar S a E b → E x → Stack 0, a Input a x • b $ Current State 1

  13. x Introduction The Automata A More Complex Example a E b I 1 S → a • E b I 4 S → a E b • I 0 S → • a E b I 2 S → a E • b E → • x r 1 { $ } x I 3 E → x • { b } r 2 Grammar S a E b E → E x → Input a x • b $ Stack 0, a , 1, Current State 2

  14. Introduction The Automata A More Complex Example a E b I 1 S → a • E b I 4 S → a E b • I 0 S → • a E b I 2 S → a E • b E → • x r 1 { $ } x I 3 E → x • { b } r 2 Grammar S a E b E → E x → x , 2, b Input a x b • $ Stack 0, a , 1, Current State 3

  15. b a x Introduction The Automata A More Complex Example a b E I 1 S → a • E b I 4 S → a E b • I 0 S → • a E b I 2 S → a E • b E → • x r 1 { $ } x I 3 E → x • { b } r 2 Stack (Empty) Grammar Now we have the result: S a E b → S E x → Input a x b • $ E Current State 0

  16. Introduction The Automata Representing the Automata We will represent the automata using two tables. Action Table Shift, Reduce n , Accept Goto Table Destination State The rows are the state numbers, the columns are the symbols.

  17. Introduction The Automata The Algorithm ◮ To create the start state, add the transitive closure of the start symbol. Example 1 Start Example 2 Start S → x S e S → • x S e S → x S e E x • E x F x S → • x S e | | | E → a E E → • a E E → a E • F x | F x • F x F x F → • q | | | F → q F → • q F → q

  18. Introduction The Automata The Algorithm, ctd ◮ Let x be an arbitrary terminal, A be an aribtrary nonterminal, and α and β be arbitrary (possibly empty) strings of symbols. ◮ In an item set i , take every production of the form E → α • x β and produce a new state j containing the transitive closure of E → α x • β . Add a shift in the action table for column x and state i , and destination state j in the goto table for column x and state i . ◮ In an item set i , take every production of the form E → α • A β and produce a new state j containing the transitive closure of E → α A • β . Add j to the goto table in column A and state i . ◮ In an item set i , take every rule of the form E → α • and add a reduce actions for state i for each terminal in the follow set of E . ◮ If an item set is recreated, reuse the original; do not create a duplicate.

  19. Introduction The Automata Automata Example 1 Automata a b I 2 S → a b • I 0 S → • a b I 1 S → a • b r 1 $ Tabular Representation Grammar Action Goto a b $ a b $ S 0 s 0 1 S → a b 1 s 1 2 2 r1 2

  20. Introduction The Automata Automata Example 2 Automata a E b I 1 S → a • E b I 4 S → a E b • I 0 S → • a E b I 2 S → a E • b E → • x r 1 { $ } x I 3 E → x • { b } r 2 Grammar Action Goto a b x $ a b x $ S E 0 s 0 1 S a E b 1 s 1 3 2 → E x 2 s 2 4 → 3 r2 3 4 r1 4

  21. Introduction The Automata Automata Example 3.1 ◮ Let’s build the table for this automata. S a E b → a b S | E E x → b |

  22. Introduction The Automata Automata Example 3.2 I 0 S → • a E b • a b S Grammar Action Goto a b x $ a b x $ S E 0 0 1 1 2 2 S a E b → 3 3 a b S | 4 4 E E x → 5 5 b | 6 6

  23. Introduction The Automata Automata Example 3.2 I 0 S → • a E b ⇐ • a b S ⇐ Grammar Action Goto a b x $ a b x $ S E 0 0 1 1 2 2 S a E b → 3 3 a b S | 4 4 E E x → 5 5 b | 6 6

  24. Introduction The Automata Automata Example 3.3 a I 1 S → a • E b I 0 S → • a E b a • b S • a b S E → • E x • b S a E b → a b x $ a b x $ S E a b S | 0 s 0 1 E E x → 1 1 b | 2 2 3 3 4 4 5 5 6 6

  25. Introduction The Automata Automata Example 3.3 a I 1 S → a • E b ⇐ I 0 S → • a E b a • b S • a b S E → • E x ⇐ • b S a E b → a b x $ a b x $ S E a b S | 0 s 0 1 E E x → 1 1 b | 2 2 3 3 4 4 5 5 6 6

  26. Introduction The Automata Automata Example 3.4 a I 1 S → a • E b E I 0 S → • a E b a • b S I 2 S → a E • b • a b S E → • E x E → E • x • b S a E b → a b x $ a b x $ S E a b S | 0 s 0 1 E E x → 1 1 2 b | 2 2 3 3 4 4 5 5 6 6

  27. Introduction The Automata Automata Example 3.4 a I 1 S → a • E b E I 0 S → • a E b a • b S ⇐ I 2 S → a E • b • a b S E → • E x E → E • x • b ⇐ S a E b → a b x $ a b x $ S E a b S | 0 s 0 1 E E x → 1 1 2 b | 2 2 3 3 4 4 5 5 6 6

  28. Introduction The Automata Automata Example 3.5 a I 1 S → a • E b E I 0 S → • a E b a • b S I 2 S → a E • b • a b S E → • E x E → E • x • b b I 3 S → a b • S E → b • S → • a E b S → • a b S S a E b → a b x $ a b x $ S E a b S | 0 s 0 1 E E x → 1 s 1 3 2 b | 2 2 3 3 4 4 5 5 6 6

  29. Introduction The Automata Automata Example 3.5 a I 1 S → a • E b E I 0 S → • a E b a • b S I 2 S → a E • b ⇐ • a b S E → • E x E → E • x • b b I 3 S → a b • S E → b • S → • a E b S → • a b S S a E b → a b x $ a b x $ S E a b S | 0 s 0 1 E E x → 1 s 1 3 2 b | 2 2 3 3 4 4 5 5 6 6

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend