CS 461 Oct 12 Parsing Running a parse

  • Slides: 8
Download presentation
CS 461 – Oct. 12 Parsing • Running a parse machine – “Goto” (or

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

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.

Sets of items • We’re creating states. • We start with a grammar. First

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 | 0 S 1 We add new start rule S’ S State 0 has these 3 items: I 0: S’ S S 1 Expand S S 0 S 1

continued • Next, determine transitions out of state 0. δ(0, S) = 1 δ(0,

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. State 0 has these 3 items: I 0: S’ S 1 S 1 2 S 0 S 1 3 • Now we’re ready for state 1. Move cursor to right to become S’ S I 1: S’ S

continued • Any time an item ends with , this represents a reduce, not

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

continued • Next is state 3. From S 0 S 1, move cursor. Notice

continued • Next is state 3. From S 0 S 1, 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: I 1: I 2: I 3: S’ S S 1 S 0 S 1 S’ S S 1 1 2 3 r r S 0 S 1 S 0 S 1 4 2 3

continued • Next is state 4. From item S 0 S 1, move cursor.

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

Last state! • Our last state is #5. Since the cursor is at the

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 4: S’ S S 1 S 0 S 1 S’ S S 1 S 0 S 1 S 0 S 1 S 0 S 1 1 2 3 r r 4 2 3 5 I 5: S 0 S 1 r I 0: I 1: I 2: I 3: