Chapter 4 Parsing Sequences. Recursive Descent Parsing expr() – term() lex() +/- lex() term()...

30
Chapter 4 Parsing Sequences
  • 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()...

Page 1: Chapter 4 Parsing Sequences. Recursive Descent Parsing expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term()

Chapter 4

Parsing Sequences

Page 2: Chapter 4 Parsing Sequences. Recursive Descent Parsing expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), 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 (a) = IDexpr()

Page 3: Chapter 4 Parsing Sequences. Recursive Descent Parsing expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), 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()

Page 4: Chapter 4 Parsing Sequences. Recursive Descent Parsing expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), 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()

Page 5: Chapter 4 Parsing Sequences. Recursive Descent Parsing expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), 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()nextToken is ID, so call lex() and return from factor, nextToken is +

Page 6: Chapter 4 Parsing Sequences. Recursive Descent Parsing expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), 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()nextToken is ID, so call lex() and return from factor, nextToken is +not * or / so return from term

Page 7: Chapter 4 Parsing Sequences. Recursive Descent Parsing expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), 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)

Page 8: Chapter 4 Parsing Sequences. Recursive Descent Parsing expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), 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) now call term()

Page 9: Chapter 4 Parsing Sequences. Recursive Descent Parsing expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), 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

Page 10: Chapter 4 Parsing Sequences. Recursive Descent Parsing expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), 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 end

Page 11: Chapter 4 Parsing Sequences. Recursive Descent Parsing expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), 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 term

Page 12: Chapter 4 Parsing Sequences. Recursive Descent Parsing expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), 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!

Page 13: Chapter 4 Parsing Sequences. Recursive Descent Parsing expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), 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!

Page 14: Chapter 4 Parsing Sequences. Recursive Descent Parsing expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term()

14

LR Parsing Table

1.E -> E + T2.E -> T3.T -> T * F4.T -> F5.F -> (E)6.F -> id

Page 15: Chapter 4 Parsing Sequences. Recursive Descent Parsing expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term()

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$

$

$

$

$

Page 16: Chapter 4 Parsing Sequences. Recursive Descent Parsing expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term()

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

Page 17: Chapter 4 Parsing Sequences. Recursive Descent Parsing expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term()

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)

Page 18: Chapter 4 Parsing Sequences. Recursive Descent Parsing expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term()

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)

Page 19: Chapter 4 Parsing Sequences. Recursive Descent Parsing expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term()

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)

Page 20: Chapter 4 Parsing Sequences. Recursive Descent Parsing expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term()

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

Page 21: Chapter 4 Parsing Sequences. Recursive Descent Parsing expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term()

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

Page 22: Chapter 4 Parsing Sequences. Recursive Descent Parsing expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term()

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)

Page 23: Chapter 4 Parsing Sequences. Recursive Descent Parsing expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term()

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)

Page 24: Chapter 4 Parsing Sequences. Recursive Descent Parsing expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term()

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

Page 25: Chapter 4 Parsing Sequences. Recursive Descent Parsing expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term()

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

Page 26: Chapter 4 Parsing Sequences. Recursive Descent Parsing expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term()

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)

Page 27: Chapter 4 Parsing Sequences. Recursive Descent Parsing expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term()

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)

Page 28: Chapter 4 Parsing Sequences. Recursive Descent Parsing expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term()

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)

Page 29: Chapter 4 Parsing Sequences. Recursive Descent Parsing expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term()

29

LR Parsing Table1.E -> E + T2.E -> T3.T -> T * F4.T -> F5.F -> (E)6.F -> id

Input$

StackE1

0

Action: Accept

Page 30: Chapter 4 Parsing Sequences. Recursive Descent Parsing expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term()

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: