Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 461 – Oct. 12 Parsing Running a parse machine –“Goto” (or shift) actions –Reduce actions: backtrack to earlier state –Maintain stack of visited states.

Similar presentations


Presentation on theme: "CS 461 – Oct. 12 Parsing Running a parse machine –“Goto” (or shift) actions –Reduce actions: backtrack to earlier state –Maintain stack of visited states."— Presentation transcript:

1 CS 461 – Oct. 12 Parsing Running a parse machine –“Goto” (or shift) actions –Reduce actions: backtrack to earlier state –Maintain stack of visited states Creating a parse machine –Find the states: sets of items –Find transitions between states, including reduce. –If many states, write table instead of drawing

2 Parsing CYK algorithm still too slow Better technique: bottom-up parsing Basic idea S  AB A  aaa B  bb At any point in time, think about where we could be while parsing the string “aaabb”. When we arrive at aaa  bb. We can reduce the “aaa” to A. When we arrive at Abb , we can reduce the “bb” to B. Knowing that we’ve just read AB , we can reduce this to S. See handouts for details.

3 Sets of items We’re creating states. We start with a grammar. First step is to augment it with the rule S’  S. The first state I 0 will contain S’   S Important rule: Any time you write  before a variable, you must “expand” that variable. So, we add items from the rules of S to I 0. Example: { 0 n 1 n+1 } S  1 | 0S1 We add new start rule S’  S State 0 has these 3 items: I 0 :S’   S S   1 S   0S1 Expand S

4 continued Next, determine transitions out of state 0. δ(0, S) = 1 δ(0, 1) = 2 δ(0, 0) = 3 I’ve written destinations along the right side. Now we’re ready for state 1. Move cursor to right to become S’  S  State 0 has these 3 items: I 0 :S’   S1 S   12 S   0S13 I 1 :S’  S 

5 continued Any time an item ends with , this represents a reduce, not a goto. Now, we’re ready for state 2. The item S   1 moves its cursor to the right: S  1  This also become a reduce. I 0 :S’   S1 S   12 S   0S13 I 1 :S’  S  r I 2 :S  1  r

6 continued Next is state 3. From S   0S1, move cursor. Notice that now the  is in front of a variable, so we need to expand. Once we’ve written the items, fill in the transitions. Create new state only if needed. δ(3, S) = 4 (a new state) δ(3, 1) = 2 (as before) δ(3, 0) = 3 (as before) I 0 :S’   S1 S   12 S   0S13 I 1 :S’  S  r I 2 :S  1  r I 3 :S  0  S14 S   12 S   0S13

7 continued Next is state 4. From item S  0  S1, move cursor. Determine transition. δ(4, 1) = 5 Notice we need new state since we’ve never seen “0 S  1” before. I 0 :S’   S1 S   12 S   0S13 I 1 :S’  S  r I 2 :S  1  r I 3 :S  0  S14 S   12 S   0S13 I 4 :S  0S  15

8 Last state! Our last state is #5. Since the cursor is at the end of the item, our transition is a reduce. Now, we are done finding states and transitions! One question remains, concerning the reduce transitions: On what input should we reduce? I 0 :S’   S1 S   12 S   0S13 I 1 :S’  S  r I 2 :S  1  r I 3 :S  0  S14 S   12 S   0S13 I 4 :S  0S  15 I 5 :S  0S1  r


Download ppt "CS 461 – Oct. 12 Parsing Running a parse machine –“Goto” (or shift) actions –Reduce actions: backtrack to earlier state –Maintain stack of visited states."

Similar presentations


Ads by Google