80 likes | 174 Views
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 .
E N D
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
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 aaabb. 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.
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 Sets of items Expand S
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 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 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 S 1 2 S 0S1 3 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 S 1 2 S 0S1 3 I4: S 0S 1 5 continued
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 S 1 2 S 0S1 3 I4: S 0S 1 5 I5: S 0S1 r Last state!