objectives
play

Objectives You should be able to ... LR Parsing Explain the - PowerPoint PPT Presentation

Introduction The Automata Introduction The Automata Objectives You should be able to ... LR Parsing Explain the difference between an LL and LR parser. Dr. Mattox Beckman Generate the fjnite state machine from an LR grammar. Use


  1. Introduction The Automata Introduction The Automata Objectives You should be able to ... LR Parsing ◮ Explain the difference between an LL and LR parser. Dr. Mattox Beckman ◮ Generate the fjnite state machine from an LR grammar. ◮ Use the state machine to detect ambiguity. University of Illinois at Urbana-Champaign Department of Computer Science Further reading: See Dragon Book §4.x. Introduction The Automata Introduction The Automata What Is LR Parsing? Shifting Shifting involves three steps. 1. Consume a token from the input. ◮ What is an LR parser? 2. Push the token and the current state to the stack. ◮ An LR parser uses a L eft-to-right scan and produces a R ightmost derivation. 3. Go to the next state. ◮ A.k.a. bottom-up parsing ◮ Uses a push-down automata to do the work. Example: ◮ There are four actions. a b I 2 S → a b • I 0 S → • a b I 1 S → a • b r 1 Shift Consume a token from the input. { $ } Reduce Build a tree from components. Grammar S → a b Goto Jump to a different state, after a reduce. Input • a b $ Accept Signal that we’re done. Stack (empty) Current State 0 We will shift the a and then we go to state 1.

  2. Introduction The Automata Introduction The Automata Shifting Shifting Shifting involves three steps. Shifting involves three steps. 1. Consume a token from the input. 1. Consume a token from the input. 2. Push the token and the current state to the stack. 2. Push the token and the current state to the stack. 3. Go to the next state. 3. Go to the next state. Example: Example: a b a b I 2 S → a b • I 2 S → a b • I 0 S → • a b I 1 S → a • b I 0 S → • a b I 1 S → a • b r 1 r 1 { $ } { $ } Grammar S → a b Grammar S → a b Input a • b $ Input a b • $ Stack 0, a Stack 0, a , 1, b Current State 1 Current State 2 We will shift the b and then we go to state 2. What should happen now? Introduction The Automata Introduction The Automata Reducing Reducing Reducing involves three steps. Reducing involves three steps. 1. Pop the tokens and states from the stack. (How many?) 1. Pop the tokens and states from the stack. (How many?) 2. Return to the last state popped. 2. Return to the last state popped. 3. Construct a new tree from the popped tokens. 3. Construct a new tree from the popped tokens. Example: Example: a b a b I 2 S → a b • I 2 S → a b • I 0 S → • a b I 1 S → a • b I 0 S → • a b I 1 S → a • b r 1 r 1 { $ } { $ } Grammar S → a b Grammar S → a b Input a b • $ Input a b • $ Stack 0, a , 1, b Stack Current State 2 Current State 0 We are ready to reduce. Now we have an S tree. Go To or Accept could happen here.

  3. Introduction The Automata Introduction The Automata A More Complex Example A More Complex Example a E b a E b I 1 S → a • E b I 4 S → 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 I 0 S → • a E b I 2 S → a E • b E → • x r 1 E → • x r 1 { $ } { $ } x x I 3 E → x • I 3 E → x • { b } r 2 { b } r 2 Grammar Grammar S a E b S a E b → → E x E x → → Stack (Empty) Stack 0, a Input • a x b $ Input a • x b $ Current State 0 Current State 1 Introduction The Automata Introduction The Automata A More Complex Example A More Complex Example a b a b E E I 1 S → a • E b I 4 S → 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 I 0 S → • a E b I 2 S → a E • b E → • x r 1 E → • x r 1 { $ } { $ } x x I 3 E → x • I 3 E → x • { b } r 2 { b } r 2 Grammar Grammar S a E b S a E b → → E x E x → → Stack 0, a ,1, x Stack 0, a Input a x • b $ Input a x • b $ Current State 4 Current State 1

  4. b x a x Introduction The Automata Introduction The Automata A More Complex Example A More Complex Example a E b a E b I 1 S → a • E b I 4 S → 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 I 0 S → • a E b I 2 S → a E • b E → • x r 1 E → • x r 1 { $ } { $ } x x I 3 E → x • I 3 E → x • { b } r 2 { b } r 2 Grammar Grammar S a E b E S a E b E → → E x E x → → x , 2, b Input a x • b $ Input a x b • $ Stack 0, a , 1, Stack 0, a , 1, Current State 2 Current State 3 Introduction The Automata Introduction The Automata A More Complex Example Representing the 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 We will represent the automata using two tables. I 3 E → x • { b } r 2 Action Table Shift, Reduce n , Accept Stack (Empty) Goto Table Destination State Grammar Now we have the result: The rows are the state numbers, the columns are the symbols. S a E b → S E x → Input a x b • $ E Current State 0

  5. Introduction The Automata Introduction The Automata The Algorithm The Algorithm, ctd ◮ Let x be an arbitrary terminal, A be an aribtrary nonterminal, and α and β be arbitrary (possibly empty) strings of symbols. ◮ To create the start state, add the transitive closure of the start symbol. ◮ In an item set i , take every production of the form E → α • x β and produce a new state j Example 1 Start Example 2 Start containing the transitive closure of E → α x • β . Add a shift in the action table for column S → x S e S → • x S e S → x S e x and state i , and destination state j in the goto table for column x and state i . E x • E x F x S → • x S e | | | ◮ In an item set i , take every production of the form E → α • A β and produce a new state j E → a E E → • a E E → a E • F x | containing the transitive closure of E → α A • β . Add j to the goto table in column A and F x • F x F x F → • q | | | state i . F → q F → • q F → q ◮ 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. Introduction The Automata Introduction The Automata Automata Example 1 Automata Example 2 Automata a b E Automata 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 a b { $ } I 2 S → a b • x I 0 S → • a b I 1 S → a • b r 1 $ Tabular Representation I 3 E → x • { b } r 2 Grammar Action Goto Grammar Action Goto a b $ a b $ S a b x $ a b x $ S E 0 s 0 1 S → a b 0 s 0 1 1 s 1 2 S a E b 1 s 1 3 2 → 2 r1 2 E x → 2 s 2 4 3 r2 3 4 r1 4

  6. Introduction The Automata Introduction The Automata Automata Example 3.1 Automata Example 3.2 I 0 S → • a E b • a b S ◮ Let’s build the table for this automata. Grammar Action Goto a b x $ a b x $ S E S a E b → 0 0 a b S | 1 1 E E x 2 2 → S a E b → b 3 3 | a b S | 4 4 E E x → 5 5 b | 6 6 Introduction The Automata Introduction The Automata Automata Example 3.2 Automata Example 3.3 a I 1 S → a • E b I 0 S → • a E b ⇐ I 0 S → • a E b a • b S • a b S ⇐ • a b S E → • E x • b Grammar Action Goto S a E b → a b x $ a b x $ S E a b x $ a b x $ S E a b S | 0 0 0 s 0 1 E E x → 1 1 1 1 b | 2 2 2 2 S a E b → 3 3 3 3 a b S | 4 4 E E x 4 4 → 5 5 b 5 5 | 6 6 6 6

  7. Introduction The Automata Introduction The Automata Automata Example 3.3 Automata Example 3.4 a I 1 S → a • E b ⇐ a I 1 S → a • E b E I 0 S → • a E b a • b S I 0 S → • a E b a • b S I 2 S → a E • b • a b S E → • E x ⇐ • a b S E → • E x E → E • x • b • b S a E b S a E b → → a b x $ a b x $ S E a b x $ a b x $ S E a b S a b S | | 0 s 0 1 0 s 0 1 E E x E E x → → 1 1 1 1 2 b b | | 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 Introduction The Automata Introduction The Automata Automata Example 3.4 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 a I 1 S → a • E b E • b 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 E b a b x $ a b x $ S E S → • a b S a b S | 0 s 0 1 S a E b E E x → → a b x $ a b x $ S E 1 1 2 a b S b | | 0 s 0 1 E E x 2 2 → 1 s 1 3 2 b 3 3 | 2 2 4 4 3 3 5 5 4 4 6 6 5 5 6 6

Recommend


More recommend