CSE401: LL(1) Parsing Example - University of Washington · Parser Example Following slides trace...

Post on 23-Jul-2020

3 views 0 download

Transcript of CSE401: LL(1) Parsing Example - University of Washington · Parser Example Following slides trace...

CSE401:LL(1) Parsing Example

Larry RuzzoSpring 2004

Slides by Chambers, Eggers, Notkin, Ruzzo, and others© W.L.Ruzzo & UW CSE 1994-2004

2

LL(1) Parsing TheoryGoal: Formal, rigorous description of those

grammars for which “I can figure out how todo a top-down parse by looking ahead justone token”, plus corresponding algorithms.

Notation:T = Set of Terminals (Tokens)N = Set of Nonterminals$ = End-of-file character (T-like, but not in N ∪ T)

3

Table-driven predictive parser Automatically compute PREDICT table

from grammar PREDICT(nonterminal,input-symbol)

action, e.g. which rhs or error

4

Example 1Stmt ::= 1 if expr then Stmt else Stmt | 2 while Expr do Stmt | 3 begin Stmts end

Stmts ::= 4 Stmt ; Stmts | 5 ε

Expr ::= 6 id

;

6Expr

5444Stmts

321Stmt

$idendbegindowhileelsethenif

empty = error

5

LL(1) Parsing Algorithmpush S$ /* S is start symbol */while Stack not empty

X := pop(Stack)a := peek at next input “token” /* EOF => $ */if X is terminal or $

If X==a, read token a else abort;else look at PREDICT(X, a) /* X is nonterminal*/

Empty : abortrule X → α : push α

If not at end of input, Abort else Accept

6

Parser Example Following slides trace execution of the parser

(slide 5) on a token string according to thegrammar from slide 4 and the correspondingparse tree

Snapshots show parser state at the top of thewhile loop and just before the “if” statement ateach iteration, together with a summary ofthe action taken in the “if”

Notice how the leaves on the right side of theparse tree correspond to the stack contents

7

S$

S

while id do begin begin end ; end $

X: a: Stack:

At top of loop

8

$

S

while id do begin begin end ; end $

X: Sa: whileStack:

Mid loopAction: 2 S ::= while E do S

9

whileEdoS$

S

E Swhile

id

do

begin begin end ; end $

X:a:Stack:

At top of loop

dowhile

10

EdoS$

S

E S

while id do begin begin end ; end $

X: whilea: whileStack:

Mid loop

while do

Action: Match

11

EdoS$

S

E S

whileid do begin begin end ; end $

X:a:Stack:

At top of loop

do

12

doS$

S

E S

whileid do begin begin end ; end $

X: Ea: idStack:

Mid loop

do

Action: 6 E ::= id

13

iddoS$

S

E S

id

do begin begin end ; end $

X:a:Stack:do

idwhile

At top of loop

14

doS$

S

E S

id do begin begin end ; end $

X: ida: idStack:

Mid loop

do

while

id

Action: Match

15

doS$

S

E S

iddo begin begin end ; end $

X:a:Stack:do

while

At top of loop

16

S$

S

E S

do begin begin end ; end $

X: doa: doStack:

Mid loop

do

while id

Action: Match

17

S$

S

E S

begin begin end ; end $

X: a: Stack:

dowhile id

At top of loop

18

$

S

E S

begin begin end ; end $

X: Sa: beginStack:

Mid loop

while doid

Action: 3 S ::= begin Ss end

19

beginSsend$

S

E S

Ss

begin begin end ; end $

X:a:Stack:

while doid

begin end

At top of loop

20

Ssend$

S

E S

Ss

begin begin end ; end $

X: begina: beginStack:

Mid loop

while doid

begin end

Action: Match

21

Ssend$

S

E S

Ss

begin end ; end $

X:a:Stack:

while doid begin

end

At top of loop

22

end$

S

E S

Ss

begin end ; end $

X: Ssa: beginStack:

Mid loop

while doid

end

begin

Action: 4 Ss ::= S ; Ss

23

S;Ssend$

S

E S

Ss

Ss

begin end

;

end $

S

X:a:Stack:

while doid

end

begin ;

At top of loop

24

;Ssend$

S

E S

Ss

Ss

begin end

;

end $

S

X: Sa: beginStack:

Mid loop

while doid

end

begin;

Action: 3 S ::= begin Ss end

25

beginSsend;Ssend$

S

E S

Ss

Ss

begin end

;

end $

S

X:a:Stack:

Top of loop

while doid

end

begin;

begin endSs

26

Ssend;Ssend$

S

E S

Ss

Ss

begin end

;

end $

S

X: begina: beginStack:

while doid

end

begin;

begin endSs

Mid loopAction: Match

27

Ssend;Ssend$

S

E S

Ss

Ss

end

;

end $

S

X:a:Stack:

while doid

end

begin;begin

endSs

Top of loop

28

end;Ssend$

S

E S

Ss

Ss

end

;

end $

S

X: Ssa: endStack:

while doid

end

begin;begin

endSs

Mid loopAction: 5 Ss ::= ε

29

end;Ssend$

S

E S

Ss

Ss

end

;

end $

S

X:a:Stack:

while doid

end

begin;begin

endSs

Top of loop

ε

30

;Ssend$

S

E S

Ss

Ss

end

;

end $

S

X: enda: endStack:

while doid

end

begin;begin

endSs

Mid loop

ε

Action: Match

31

;Ssend$

S

E S

Ss

Ss;

end $

S

X:a:Stack:

while doid

end

begin;begin end

Ss

Top of loop

ε

32

Ssend$

S

E S

Ss

Ss;

end $

S

X: ;a: ;Stack:

while doid

end

begin;begin end

Ss

Mid loop

ε

Action: Match

33

Ssend$

S

E S

Ss

Ss

;end $

S

X:a:Stack:

while doid

end

begin begin end

Ss

Top of loop

ε

34

end$

S

E S

Ss

Ss

;end $

S

X: Ssa: endStack:

while doid

end

begin begin end

Ss

Mid loop

ε

Action: 5 Ss ::= ε

35

end$

S

E S

Ss

Ss

;end $

S

X:a:Stack:

while doid

end

begin begin end

Ss

Top of loop

ε ε

36

$

S

E S

Ss

Ss

;end $

S

X: enda: endStack:

while doid

end

begin begin end

Ss

Mid loop

ε ε

Action: Match

37

$

S

E S

Ss

Ss

; end$

S

X:a:Stack:

while doid begin begin end

Ss

Top of loop

ε ε

38

S

E S

Ss

Ss

; end$

S

X: $a: $Stack:

while doid begin begin end

Ss

Mid loop

ε ε

Action: Match

39

S

E

ACCEPTS

Ss

Ss

; end $

S

X:a:Stack:

while doid begin begin end

Ss

Top of loop

ε ε