Professional Documents
Culture Documents
Parsing Sequences
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()
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()
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()
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 +
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
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)
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()
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
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
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
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!
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!
LR Parsing Table
1.E -> E + T 2.E -> T 3.T -> T * F 4.T -> F 5.F -> (E) 6.F -> id
14
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
+ id * id$
id * id$ * id$ * id$ * id$
id$
$ $ $ $
15
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
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$
Stack
id5 0
Action: Reduce 6, pops id5, state is now 0, so GOTO [State0, F] places in state 3 (push F3)
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
F3 0
Action: Reduce 4, pops F3 off stack, state is now 0, so 18 GOTO [State0, T] places in state 2 (push T2)
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 19 GOTO [State0, E] places in state 1 (push E1)
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
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
+6 E1 0
Action: Shift 5, gets the next input token and moves to state 5
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$
Stack
id5 +6 E1 0
Action: Reduce 6, pops id5 off stack, state is now 6, so 22 GOTO [State6, F] places in state 3 (pushes F3)
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 23 GOTO [State6, T] places in state 9 (pushes T9)
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
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
*7 T9 +6 E1 0
Action: Shift 5, gets the next input token and moves to state 5
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
$
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) 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
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) 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
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) 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
E1 0
Action: Accept
29
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:
30