Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 44 – Jan. 31 Parsing Running a parse machine √

Similar presentations


Presentation on theme: "CS 44 – Jan. 31 Parsing Running a parse machine √"— Presentation transcript:

1 CS 44 – Jan. 31 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 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 I0 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 I0. Example: { 0n 1n+1 } S  1 | 0S1 We add new start rule S’  S State 0 has these 3 items: I0: S’   S S   1 S   0S1 Expand S

3 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: I0: S’   S 1 S   1 2 S   0S1 3 I1: S’  S 

4 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. I0: S’   S 1 S   1 2 S   0S1 3 I1: S’  S  r I2: S  1  r

5 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) I0: S’   S 1 S   1 2 S   0S1 3 I1: S’  S  r I2: S  1  r I3: S  0  S1 4

6 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. I0: S’   S 1 S   1 2 S   0S1 3 I1: S’  S  r I2: S  1  r I3: S  0  S1 4 I4: S  0S  1 5

7 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? I0: S’   S 1 S   1 2 S   0S1 3 I1: S’  S  r I2: S  1  r I3: S  0  S1 4 I4: S  0S  1 5 I5: S  0S1  r

8 When to reduce If you are at the end of an item such as S  1 , there is no symbol after the  telling us what input to wait for. The next symbol should be whatever “follows” the variable we are reducing. In this case, what follows S. We need to look at the original grammar to find out. For example, if you were reducing A, and you saw a rule S  A1B, you would say that 1 follows A. Since S is start symbol, $ (end of input) follows S. For more info, see parser worksheet. New skill: for each grammar variable, what follows?


Download ppt "CS 44 – Jan. 31 Parsing Running a parse machine √"

Similar presentations


Ads by Google