Top Banner
CSE401: LL(1) Parsing Example Larry Ruzzo Spring 2004 Slides by Chambers, Eggers, Notkin, Ruzzo, and others © W.L.Ruzzo & UW CSE 1994-2004
39

CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

Aug 01, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

CSE401:LL(1) Parsing Example

Larry RuzzoSpring 2004

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

Page 2: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

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)

Page 3: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

3

Table-driven predictive parser Automatically compute PREDICT table

from grammar PREDICT(nonterminal,input-symbol)

action, e.g. which rhs or error

Page 4: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

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

Page 5: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

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

Page 6: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

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

Page 7: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

7

S$

S

while id do begin begin end ; end $

X: a: Stack:

At top of loop

Page 8: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

8

$

S

while id do begin begin end ; end $

X: Sa: whileStack:

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

Page 9: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

9

whileEdoS$

S

E Swhile

id

do

begin begin end ; end $

X:a:Stack:

At top of loop

dowhile

Page 10: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

10

EdoS$

S

E S

while id do begin begin end ; end $

X: whilea: whileStack:

Mid loop

while do

Action: Match

Page 11: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

11

EdoS$

S

E S

whileid do begin begin end ; end $

X:a:Stack:

At top of loop

do

Page 12: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

12

doS$

S

E S

whileid do begin begin end ; end $

X: Ea: idStack:

Mid loop

do

Action: 6 E ::= id

Page 13: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

13

iddoS$

S

E S

id

do begin begin end ; end $

X:a:Stack:do

idwhile

At top of loop

Page 14: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

14

doS$

S

E S

id do begin begin end ; end $

X: ida: idStack:

Mid loop

do

while

id

Action: Match

Page 15: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

15

doS$

S

E S

iddo begin begin end ; end $

X:a:Stack:do

while

At top of loop

Page 16: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

16

S$

S

E S

do begin begin end ; end $

X: doa: doStack:

Mid loop

do

while id

Action: Match

Page 17: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

17

S$

S

E S

begin begin end ; end $

X: a: Stack:

dowhile id

At top of loop

Page 18: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

18

$

S

E S

begin begin end ; end $

X: Sa: beginStack:

Mid loop

while doid

Action: 3 S ::= begin Ss end

Page 19: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

19

beginSsend$

S

E S

Ss

begin begin end ; end $

X:a:Stack:

while doid

begin end

At top of loop

Page 20: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

20

Ssend$

S

E S

Ss

begin begin end ; end $

X: begina: beginStack:

Mid loop

while doid

begin end

Action: Match

Page 21: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

21

Ssend$

S

E S

Ss

begin end ; end $

X:a:Stack:

while doid begin

end

At top of loop

Page 22: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

22

end$

S

E S

Ss

begin end ; end $

X: Ssa: beginStack:

Mid loop

while doid

end

begin

Action: 4 Ss ::= S ; Ss

Page 23: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

23

S;Ssend$

S

E S

Ss

Ss

begin end

;

end $

S

X:a:Stack:

while doid

end

begin ;

At top of loop

Page 24: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

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

Page 25: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

25

beginSsend;Ssend$

S

E S

Ss

Ss

begin end

;

end $

S

X:a:Stack:

Top of loop

while doid

end

begin;

begin endSs

Page 26: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

26

Ssend;Ssend$

S

E S

Ss

Ss

begin end

;

end $

S

X: begina: beginStack:

while doid

end

begin;

begin endSs

Mid loopAction: Match

Page 27: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

27

Ssend;Ssend$

S

E S

Ss

Ss

end

;

end $

S

X:a:Stack:

while doid

end

begin;begin

endSs

Top of loop

Page 28: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

28

end;Ssend$

S

E S

Ss

Ss

end

;

end $

S

X: Ssa: endStack:

while doid

end

begin;begin

endSs

Mid loopAction: 5 Ss ::= ε

Page 29: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

29

end;Ssend$

S

E S

Ss

Ss

end

;

end $

S

X:a:Stack:

while doid

end

begin;begin

endSs

Top of loop

ε

Page 30: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

30

;Ssend$

S

E S

Ss

Ss

end

;

end $

S

X: enda: endStack:

while doid

end

begin;begin

endSs

Mid loop

ε

Action: Match

Page 31: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

31

;Ssend$

S

E S

Ss

Ss;

end $

S

X:a:Stack:

while doid

end

begin;begin end

Ss

Top of loop

ε

Page 32: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

32

Ssend$

S

E S

Ss

Ss;

end $

S

X: ;a: ;Stack:

while doid

end

begin;begin end

Ss

Mid loop

ε

Action: Match

Page 33: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

33

Ssend$

S

E S

Ss

Ss

;end $

S

X:a:Stack:

while doid

end

begin begin end

Ss

Top of loop

ε

Page 34: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

34

end$

S

E S

Ss

Ss

;end $

S

X: Ssa: endStack:

while doid

end

begin begin end

Ss

Mid loop

ε

Action: 5 Ss ::= ε

Page 35: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

35

end$

S

E S

Ss

Ss

;end $

S

X:a:Stack:

while doid

end

begin begin end

Ss

Top of loop

ε ε

Page 36: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

36

$

S

E S

Ss

Ss

;end $

S

X: enda: endStack:

while doid

end

begin begin end

Ss

Mid loop

ε ε

Action: Match

Page 37: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

37

$

S

E S

Ss

Ss

; end$

S

X:a:Stack:

while doid begin begin end

Ss

Top of loop

ε ε

Page 38: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

38

S

E S

Ss

Ss

; end$

S

X: $a: $Stack:

while doid begin begin end

Ss

Mid loop

ε ε

Action: Match

Page 39: CSE401: LL(1) Parsing Example - University of Washingtoncourses.cs.washington.edu/courses/cse401/04sp/slides/03b-LL1-exa… · LL(1) Parsing Theory Goal: Formal, rigorous description

39

S

E

ACCEPTS

Ss

Ss

; end $

S

X:a:Stack:

while doid begin begin end

Ss

Top of loop

ε ε