Download presentation
Presentation is loading. Please wait.
1
Chapter 4 Parsing Sequences
2
Recursive Descent Parsing expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term() – factor * or / lex() factor() a + b lex() -> nextToken (a) = ID expr()
3
Recursive Descent Parsing expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term() – factor * or / lex() factor() a + b lex() -> nextToken = ID expr() term()
4
Recursive Descent Parsing expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term() – factor * or / lex() factor() a + b lex() -> nextToken = ID expr() term() factor()
5
Recursive Descent Parsing expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term() – factor * or / lex() factor() a + b lex() -> nextToken = ID expr() term() factor() nextToken is ID, so call lex() and return from factor, nextToken is +
6
Recursive Descent Parsing expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term() – factor * or / lex() factor() a + b lex() -> nextToken = ID expr() term() factor() nextToken is ID, so call lex() and return from factor, nextToken is + not * or / so return from term
7
Recursive Descent Parsing expr() – term() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term() – factor * or / lex() factor() a + b lex() -> nextToken = ID expr() term() factor() nextToken is ID, so call lex() and return from factor, nextToken is + not * or / so return from term nextToken is + so call lex() (nextToken is b)
8
Recursive Descent Parsing expr() – term() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term() – factor * or / lex() factor() a + b lex() -> nextToken = ID expr() term() factor() nextToken is ID, so call lex() and return from factor, nextToken is + not * or / so return from term nextToken is + so call lex() (nextToken is b) now call term()
9
Recursive Descent Parsing expr() – term() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term() – factor * or / lex() factor() a + b lex() -> nextToken = ID expr() term() factor() nextToken is ID, so call lex() and return from factor, nextToken is + not * or / so return from term nextToken is + so lex() (sets nextToken to b) now call term() call factor
10
Recursive Descent Parsing expr() – term() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term() – factor * or / lex() factor() a + b lex() -> nextToken = ID expr() term() factor() nextToken is ID, so call lex() and return from factor, nextToken is + not * or / so return from term nextToken is + so lex() (sets nextToken to b) and call term() call factor nextToken (b) is ID, so call lex() and return from factor, nextToken is end
11
Recursive Descent Parsing expr() – term() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term() – factor * or / lex() factor() a + b lex() -> nextToken = ID expr() term() factor() nextToken is ID, so call lex() and return from factor, nextToken is + not * or / so return from term nextToken is + so lex() (sets nextToken to b) and call term() call factor nextToken (b) is ID, so call lex() and return from factor, nextToken is end nextToken is not * or / so return from term
12
Recursive Descent Parsing expr() – term() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term() – factor * or / lex() factor() a + b lex() -> nextToken = ID expr() term() factor() nextToken is ID, so call lex() and return from factor, nextToken is + not * or / so return from term nextToken is + so lex() (sets nextToken to b) and call term() call factor nextToken (b) is ID, so call lex() and return from factor, nextToken is end nextToken is not * or / so return from term return from expr() – have just recognized a + b as an expression!
13
Recursive Descent Parsing expr() – term() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term() – factor * or / lex() factor() a + b lex() -> nextToken = ID expr() term() factor() nextToken is ID, so call lex() and return from factor, nextToken is + not * or / so return from term nextToken is + so lex() (sets nextToken to b) and call term() call factor nextToken (b) is ID, so call lex() and return from factor, nextToken is end nextToken is not * or / so return from term return from expr() – have just recognized a + b as an expression!
14
14 LR Parsing Table 1.E -> E + T 2.E -> T 3.T -> T * F 4.T -> F 5.F -> (E) 6.F -> id
15
15 Bottom-up Parsing/LR example Stack 0 0id5 0F3 0T2 0E1 0E1+6 0E1+6id5 0E1+6F3 0E1+6T9 0E1+6T9*7 0E1+6T9*7id5 0E1+6T9*7F10 0E1+6T9 0E1 Action S5 R6 (use GOTO[0.F]) R4 (use GOTO[0,T]) R2 (use GOTO[0,E]) S6 S5 R6 (use GOTO[6,F]) R4 (use GOTO[6,T] S7 S5 R6 (use GOTO[7.F]) R3 (use GOTO[6,T]) R1 (use GOTO[0.E]) accept Parse id + id * id Input id + id * id$ + id * id$ id * id$ * id$ id$ $
16
16 LR Parsing Table 1.E -> E + T 2.E -> T 3.T -> T * F 4.T -> F 5.F -> (E) 6.F -> id Input id + id * id$ Stack 0 Action: Shift 5
17
17 LR Parsing Table 1.E -> E + T 2.E -> T 3.T -> T * F 4.T -> F 5.F -> (E) 6.F -> id Input + id * id$ Stack id5 0 Action: Reduce 6, pops id5, state is now 0, so GOTO [State0, F] places in state 3 (push F3)
18
18 LR Parsing Table 1.E -> E + T 2.E -> T 3.T -> T * F 4.T -> F 5.F -> (E) 6.F -> id Input + id * id$ Stack F3 0 Action: Reduce 4, pops F3 off stack, state is now 0, so GOTO [State0, T] places in state 2 (push T2)
19
19 LR Parsing Table 1.E -> E + T 2.E -> T 3.T -> T * F 4.T -> F 5.F -> (E) 6.F -> id Input + id * id$ Stack T2 0 Action: Reduce 2, pops T2 off the stack, state is now 0, so GOTO [State0, E] places in state 1 (push E1)
20
20 LR Parsing Table 1.E -> E + T 2.E -> T 3.T -> T * F 4.T -> F 5.F -> (E) 6.F -> id Input + id * id$ Stack E1 0 Action: Shift 6, gets the next input token and moves to state 6
21
21 LR Parsing Table 1.E -> E + T 2.E -> T 3.T -> T * F 4.T -> F 5.F -> (E) 6.F -> id Input id * id$ Stack +6 E1 0 Action: Shift 5, gets the next input token and moves to state 5
22
22 LR Parsing Table 1.E -> E + T 2.E -> T 3.T -> T * F 4.T -> F 5.F -> (E) 6.F -> id Input * id$ Stack id5 +6 E1 0 Action: Reduce 6, pops id5 off stack, state is now 6, so GOTO [State6, F] places in state 3 (pushes F3)
23
23 LR Parsing Table 1.E -> E + T 2.E -> T 3.T -> T * F 4.T -> F 5.F -> (E) 6.F -> id Input * id$ Stack F3 +6 E1 0 Action: Reduce 4, pops F3 off stack, state is now 6, so GOTO [State6, T] places in state 9 (pushes T9)
24
24 LR Parsing Table 1.E -> E + T 2.E -> T 3.T -> T * F 4.T -> F 5.F -> (E) 6.F -> id Input * id$ Stack T9 +6 E1 0 Action: Shift 7, gets the next input token and moves to state 7
25
25 LR Parsing Table 1.E -> E + T 2.E -> T 3.T -> T * F 4.T -> F 5.F -> (E) 6.F -> id Input id $ Stack *7 T9 +6 E1 0 Action: Shift 5, gets the next input token and moves to state 5
26
26 LR Parsing Table 1.E -> E + T 2.E -> T 3.T -> T * F 4.T -> F 5.F -> (E) 6.F -> id Input $ Stack id5 *7 T9 +6 E1 0 Action: Reduce 6, pops id5 off stack, state is now 7, so GOTO [State7, F] places in state 10 (pushes F10)
27
27 LR Parsing Table 1.E -> E + T 2.E -> T 3.T -> T * F 4.T -> F 5.F -> (E) 6.F -> id Input $ Stack F10 *7 T9 +6 E1 0 Action: Reduce 3, pops F10*7T9 off stack, state is now 6, so GOTO [State6, T] places in state 9 (pushes T9)
28
28 LR Parsing Table 1.E -> E + T 2.E -> T 3.T -> T * F 4.T -> F 5.F -> (E) 6.F -> id Input $ Stack T9 +6 E1 0 Action: Reduce 1, pops T9+6E1 off stack, state is now 0, so GOTO [State0, E] places in state 1 (pushes E1)
29
29 LR Parsing Table 1.E -> E + T 2.E -> T 3.T -> T * F 4.T -> F 5.F -> (E) 6.F -> id Input $ Stack E1 0 Action: Accept
30
30 LR Parsing Table Quick Exercise 1.E -> E + T 2.E -> T 3.T -> T * F 4.T -> F 5.F -> (E) 6.F -> id Input (id + id) $ Stack 0 Action:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.