CSE401: LL(1) Parsing Example - University of...
Transcript of CSE401: LL(1) Parsing Example - University of...
![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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/1.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/2.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/3.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/4.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/5.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/6.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/7.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/8.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/9.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/10.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/11.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/12.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/13.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/14.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/15.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/16.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/17.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/18.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/19.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/20.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/21.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/22.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/23.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/24.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/25.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/26.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/27.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/28.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/29.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/30.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/31.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/32.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/33.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/34.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/35.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/36.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/37.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/38.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043004/5f85f4345b25535a095f7c38/html5/thumbnails/39.jpg)
39
S
E
ACCEPTS
Ss
Ss
; end $
S
X:a:Stack:
while doid begin begin end
Ss
Top of loop
ε ε