LL(1) Parsing
description
Transcript of LL(1) Parsing
![Page 1: LL(1) Parsing](https://reader035.fdocuments.net/reader035/viewer/2022070419/56815acb550346895dc89bc2/html5/thumbnails/1.jpg)
LL(1) Parsing
Prepared byManuel E. Bermúdez, Ph.D.
Associate ProfessorUniversity of Florida
Programming Language PrinciplesLecture 4
![Page 2: LL(1) Parsing](https://reader035.fdocuments.net/reader035/viewer/2022070419/56815acb550346895dc89bc2/html5/thumbnails/2.jpg)
Example• Build the LL(1) parse table for the
following grammar.
S → begin SL end {begin} → id := E; {id}SL → SL S {begin,id} → S {begin,id}E → E+T {(, id}→ T {(, id}T → P*T {(, id}
→ P {(, id}P → (E) {(}→ id {id}
*
*
*
* - not LL(1)
![Page 3: LL(1) Parsing](https://reader035.fdocuments.net/reader035/viewer/2022070419/56815acb550346895dc89bc2/html5/thumbnails/3.jpg)
![Page 4: LL(1) Parsing](https://reader035.fdocuments.net/reader035/viewer/2022070419/56815acb550346895dc89bc2/html5/thumbnails/4.jpg)
Example (cont’d)
• Lemma: Left recursion always produces a non-LL(1) grammar (e.g., SL, E above)
• Proof: Consider A → A First () or
Follow (A) → First () Follow (A)
![Page 5: LL(1) Parsing](https://reader035.fdocuments.net/reader035/viewer/2022070419/56815acb550346895dc89bc2/html5/thumbnails/5.jpg)
Problems with our Grammar
1. SL is left recursive.
2. E is left recursive.
3. T → P * T both begin with the same → P sequence of symbols (P).
![Page 6: LL(1) Parsing](https://reader035.fdocuments.net/reader035/viewer/2022070419/56815acb550346895dc89bc2/html5/thumbnails/6.jpg)
Solution to Problem 3
• Change: T → P * T { (, id } → P { (, id }
• to: T → P X { (, id }X → * T { * } → { +, ; , ) }
Follow(X) Follow(T) due to T → P X Follow(E) due to E → E+T , E → T= { +, ;, ) } due to E → E+T, S → id := E
; and P → (E)
Disjoint!
![Page 7: LL(1) Parsing](https://reader035.fdocuments.net/reader035/viewer/2022070419/56815acb550346895dc89bc2/html5/thumbnails/7.jpg)
Solution to Problem 3 (cont’d)
• In general, change A → 1
→ 2 . . . → n
to A → X X → 1 . . . → n
Hopefully all the ’s begin with different symbols
![Page 8: LL(1) Parsing](https://reader035.fdocuments.net/reader035/viewer/2022070419/56815acb550346895dc89bc2/html5/thumbnails/8.jpg)
Solution to Problems 1 and 2
• We want (…((( T + T) + T) + T)…)• Instead, (T) (+T) (+T) … (+T)
Change: E → E + T { (, id } → T { (, id }
To: E → T Y { (, id }Y → + T Y { + } → { ; , ) }
• Follow(Y) Follow(E)• = { ; , ) }
No longer contains ‘+’, because we eliminated the production E → E + T
![Page 9: LL(1) Parsing](https://reader035.fdocuments.net/reader035/viewer/2022070419/56815acb550346895dc89bc2/html5/thumbnails/9.jpg)
Solution to Problems 1 and 2 (cont’d)• In general,
Change: A → A1 A → 1 . . . . . . → An → m
to: A → 1 X X → 1 X . . . . . . → m X → n X →
![Page 10: LL(1) Parsing](https://reader035.fdocuments.net/reader035/viewer/2022070419/56815acb550346895dc89bc2/html5/thumbnails/10.jpg)
Solution to Problems 1 and 2 (cont’d)• In our example,
Change: SL → SL S { begin, id } → S { begin, id }
To: SL → S Z { begin, id } Z → S Z { begin, id }
→ { end }
![Page 11: LL(1) Parsing](https://reader035.fdocuments.net/reader035/viewer/2022070419/56815acb550346895dc89bc2/html5/thumbnails/11.jpg)
Modified GrammarS → begin SL end {begin} → id := E ; {id}SL → S Z {begin,id} Z → S Z {begin,id}
→ {end}E → T Y (,id}Y → + T Y {+}
→ {;,)}T → P X {(,id}X → * T {*}
→ {;,+,)}P → (E) {(}
→ id {id}
Disjoint.Grammar is LL(1)
![Page 12: LL(1) Parsing](https://reader035.fdocuments.net/reader035/viewer/2022070419/56815acb550346895dc89bc2/html5/thumbnails/12.jpg)
![Page 13: LL(1) Parsing](https://reader035.fdocuments.net/reader035/viewer/2022070419/56815acb550346895dc89bc2/html5/thumbnails/13.jpg)
![Page 14: LL(1) Parsing](https://reader035.fdocuments.net/reader035/viewer/2022070419/56815acb550346895dc89bc2/html5/thumbnails/14.jpg)
Recursive Descent Parsing
• Top-down parsing strategy, suitable for LL(1) grammars.
• One procedure per nonterminal.• Contents of stack embedded in recursive
call sequence.• Each procedure “commits” to one
production, based on the next input symbol, and the select sets.
• Good technique for hand-written parsers.
![Page 15: LL(1) Parsing](https://reader035.fdocuments.net/reader035/viewer/2022070419/56815acb550346895dc89bc2/html5/thumbnails/15.jpg)
For our Modified, LL(1) Grammarproc S; {S → begin SL end
→ id := E; }case Next_Token of
T_begin : Read(T_begin);SL;Read (T_end);
T_id : Read(T_id);Read (T_:=);E;Read (T_;);
otherwise Error;end
end;
“Read (T_X)” verifies that the upcoming token is X, and consumes it.
“Next_Token” is the upcoming token.
![Page 16: LL(1) Parsing](https://reader035.fdocuments.net/reader035/viewer/2022070419/56815acb550346895dc89bc2/html5/thumbnails/16.jpg)
For our Modified, LL(1) Grammar (cont’d)proc SL; {SL → SZ}
S;Z;
end;
proc E; {E → TY}T;Y;
end;
Technically, should have insisted that Next Token be either T_begin or T_id, but S will do that anyway. Checking early would aid error recovery.
// Ditto for T_( and T_id.
![Page 17: LL(1) Parsing](https://reader035.fdocuments.net/reader035/viewer/2022070419/56815acb550346895dc89bc2/html5/thumbnails/17.jpg)
For our Modified, LL(1) Grammar (cont’d)
proc Z;{Z → SZ→ }
case Next Token ofT_begin, T_id: S;Z;
T_end: ;otherwise Error;
endend;
![Page 18: LL(1) Parsing](https://reader035.fdocuments.net/reader035/viewer/2022070419/56815acb550346895dc89bc2/html5/thumbnails/18.jpg)
For our Modified, LL(1) Grammar (cont’d)proc Y; {Y → +TY
→ }if Next Token = T_+ then
Read (T_+)T;Y;
end;
proc T; {T → PX}P;X
end;
Could have used a case statement
Could have checked for T_( and T_id.
![Page 19: LL(1) Parsing](https://reader035.fdocuments.net/reader035/viewer/2022070419/56815acb550346895dc89bc2/html5/thumbnails/19.jpg)
For our Modified, LL(1) Grammar (cont’d)
proc X;{X → *T→ }
if Next Token = T_* thenRead (T_*);T;
end;
![Page 20: LL(1) Parsing](https://reader035.fdocuments.net/reader035/viewer/2022070419/56815acb550346895dc89bc2/html5/thumbnails/20.jpg)
For our Modified, LL(1) Grammar (cont’d)proc P; {P →(E)
→ id }case Next Token of
T_(: Read (T_();E;Read (T_));
T_id: Read (T_id);otherwise Error;
endend;
![Page 21: LL(1) Parsing](https://reader035.fdocuments.net/reader035/viewer/2022070419/56815acb550346895dc89bc2/html5/thumbnails/21.jpg)
LL(1) Parsing
Prepared byManuel E. Bermúdez, Ph.D.
Associate ProfessorUniversity of Florida
Programming Language PrinciplesLecture 4