Chapter 4 Parsing Sequences. Recursive Descent Parsing expr() – term() lex() +/- lex() term()...
-
date post
21-Dec-2015 -
Category
Documents
-
view
251 -
download
6
Transcript of Chapter 4 Parsing Sequences. Recursive Descent Parsing expr() – term() lex() +/- lex() term()...
Chapter 4
Parsing Sequences
Recursive Descent Parsing
expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term() – factor * or / lex() factor()
a + blex() -> nextToken (a) = IDexpr()
Recursive Descent Parsing
expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term() – factor * or / lex() factor()
a + blex() -> nextToken = IDexpr()term()
Recursive Descent Parsing
expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term() – factor * or / lex() factor()
a + blex() -> nextToken = IDexpr()term()factor()
Recursive Descent Parsing
expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term() – factor * or / lex() factor()
a + blex() -> nextToken = IDexpr()term()factor()nextToken is ID, so call lex() and return from factor, nextToken is +
Recursive Descent Parsing
expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term() – factor * or / lex() factor()
a + blex() -> nextToken = IDexpr()term()factor()nextToken is ID, so call lex() and return from factor, nextToken is +not * or / so return from term
Recursive Descent Parsing
expr() – term() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term() – factor * or / lex() factor()
a + blex() -> nextToken = IDexpr()term()factor()nextToken is ID, so call lex() and return from factor, nextToken is +not * or / so return from termnextToken is + so call lex() (nextToken is b)
Recursive Descent Parsing
expr() – term() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term() – factor * or / lex() factor()
a + blex() -> nextToken = IDexpr()term()factor()nextToken is ID, so call lex() and return from factor, nextToken is +not * or / so return from termnextToken is + so call lex() (nextToken is b) now call term()
Recursive Descent Parsing
expr() – term() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term() – factor * or / lex() factor()
a + blex() -> nextToken = IDexpr()term()factor()nextToken is ID, so call lex() and return from factor, nextToken is +not * or / so return from termnextToken is + so lex() (sets nextToken to b) now call term()call factor
Recursive Descent Parsing
expr() – term() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term() – factor * or / lex() factor()
a + blex() -> nextToken = IDexpr()term()factor()nextToken is ID, so call lex() and return from factor, nextToken is +not * or / so return from termnextToken is + so lex() (sets nextToken to b) and call term()call factornextToken (b) is ID, so call lex() and return from factor, nextToken is end
Recursive Descent Parsing
expr() – term() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term() – factor * or / lex() factor()
a + blex() -> nextToken = IDexpr()term()factor()nextToken is ID, so call lex() and return from factor, nextToken is +not * or / so return from termnextToken is + so lex() (sets nextToken to b) and call term()call factornextToken (b) is ID, so call lex() and return from factor, nextToken is endnextToken is not * or / so return from term
Recursive Descent Parsing
expr() – term() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term() – factor * or / lex() factor()
a + blex() -> nextToken = IDexpr()term()factor()nextToken is ID, so call lex() and return from factor, nextToken is +not * or / so return from termnextToken is + so lex() (sets nextToken to b) and call term()call factornextToken (b) is ID, so call lex() and return from factor, nextToken is endnextToken is not * or / so return from termreturn from expr() – have just recognized a + b as an expression!
Recursive Descent Parsing
expr() – term() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term() – factor * or / lex() factor()
a + blex() -> nextToken = IDexpr()term()factor()nextToken is ID, so call lex() and return from factor, nextToken is +not * or / so return from termnextToken is + so lex() (sets nextToken to b) and call term()call factornextToken (b) is ID, so call lex() and return from factor, nextToken is endnextToken is not * or / so return from termreturn from expr() – have just recognized a + b as an expression!
14
LR Parsing Table
1.E -> E + T2.E -> T3.T -> T * F4.T -> F5.F -> (E)6.F -> id
15
Bottom-up Parsing/LR example
Stack00id50F30T20E10E1+60E1+6id50E1+6F30E1+6T90E1+6T9*70E1+6T9*7id50E1+6T9*7F100E1+6T90E1
ActionS5R6 (use GOTO[0.F])R4 (use GOTO[0,T])R2 (use GOTO[0,E])S6S5R6 (use GOTO[6,F])R4 (use GOTO[6,T]S7S5R6 (use GOTO[7.F])R3 (use GOTO[6,T])R1 (use GOTO[0.E])accept
Parse id + id * idInputid + id * id$
+ id * id$
+ id * id$
+ id * id$
+ id * id$
id * id$
* id$
* id$
* id$
id$
$
$
$
$
16
LR Parsing Table1.E -> E + T2.E -> T3.T -> T * F4.T -> F5.F -> (E)6.F -> id
Inputid + id * id$
Stack0
Action: Shift 5
17
LR Parsing Table1.E -> E + T2.E -> T3.T -> T * F4.T -> F5.F -> (E)6.F -> id
Input+ id * id$
Stackid5
0
Action: Reduce 6, pops id5, state is now 0, so GOTO [State0, F] places in state 3 (push F3)
18
LR Parsing Table1.E -> E + T2.E -> T3.T -> T * F4.T -> F5.F -> (E)6.F -> id
Input+ id * id$
StackF3
0
Action: Reduce 4, pops F3 off stack, state is now 0, so GOTO [State0, T] places in state 2 (push T2)
19
LR Parsing Table1.E -> E + T2.E -> T3.T -> T * F4.T -> F5.F -> (E)6.F -> id
Input+ id * id$
StackT2
0
Action: Reduce 2, pops T2 off the stack, state is now 0, so GOTO [State0, E] places in state 1 (push E1)
20
LR Parsing Table1.E -> E + T2.E -> T3.T -> T * F4.T -> F5.F -> (E)6.F -> id
Input+ id * id$
StackE1
0
Action: Shift 6, gets the next input token and movesto state 6
21
LR Parsing Table1.E -> E + T2.E -> T3.T -> T * F4.T -> F5.F -> (E)6.F -> id
Inputid * id$
Stack+6
E1
0
Action: Shift 5, gets the next input token and movesto state 5
22
LR Parsing Table1.E -> E + T2.E -> T3.T -> T * F4.T -> F5.F -> (E)6.F -> id
Input* id$
Stackid5
+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
LR Parsing Table1.E -> E + T2.E -> T3.T -> T * F4.T -> F5.F -> (E)6.F -> id
Input* id$
StackF3
+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
LR Parsing Table1.E -> E + T2.E -> T3.T -> T * F4.T -> F5.F -> (E)6.F -> id
Input* id$
StackT9
+6
E1
0
Action: Shift 7, gets the next input token and movesto state 7
25
LR Parsing Table1.E -> E + T2.E -> T3.T -> T * F4.T -> F5.F -> (E)6.F -> id
Inputid $
Stack*7
T9
+6
E1
0
Action: Shift 5, gets the next input token and movesto state 5
26
LR Parsing Table1.E -> E + T2.E -> T3.T -> T * F4.T -> F5.F -> (E)6.F -> id
Input$
Stackid5
*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
LR Parsing Table1.E -> E + T2.E -> T3.T -> T * F4.T -> F5.F -> (E)6.F -> id
Input$
StackF10
*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
LR Parsing Table1.E -> E + T2.E -> T3.T -> T * F4.T -> F5.F -> (E)6.F -> id
Input$
StackT9
+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
LR Parsing Table1.E -> E + T2.E -> T3.T -> T * F4.T -> F5.F -> (E)6.F -> id
Input$
StackE1
0
Action: Accept
30
LR Parsing Table Quick Exercise1.E -> E + T2.E -> T3.T -> T * F4.T -> F5.F -> (E)6.F -> id
Input(id + id) $
Stack0
Action: