top-down parsing ii - stanford university · 2012. 8. 13. · top-down parsing leftmost bfs is one...
TRANSCRIPT
![Page 1: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/1.jpg)
Top-Down Parsing II
![Page 2: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/2.jpg)
Announcements
● Written Assignment 1 due this afternoon at 5PM.● Can submit electronically by emailing us at
[email protected] with [WA1] somewhere in the subject line.
● Can submit hard copies to the drop-off box in Gates (details in the problem set).
● C++ review session next Monday, time and place TBA.
![Page 3: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/3.jpg)
Where We Are
Lexical Analysis
Syntax Analysis
Semantic Analysis
IR Generation
IR Optimization
Code Generation
Optimization
SourceCode
MachineCode
![Page 4: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/4.jpg)
Top-Down Parsing
E → TE → T + ET → intT → (E)
int + ( int + int )
T
E
E
T
T
T
E
E
![Page 5: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/5.jpg)
LL(1) Parse Tables
E → intE → (E Op E)Op → +Op → *
int ( ) + *
E
Op
int (E Op E)
*+
![Page 6: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/6.jpg)
FIRST Sets
● We want to tell if a particular nonterminal A derives a string starting with a particular nonterminal t.
● We can formalize this with FIRST sets.
FIRST(A) = { t | A ⇒* tω for some ω }
● We also include ε in FIRST(A) if A can produce the empty string.
● Intuitively, FIRST(A) is the set of terminals that can be at the start of a string produced by A.
● We can generalize FIRST to strings with FIRST*(ω) being the set of all terminals (or ε) that can appear at the start of a string derived from ω.
![Page 7: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/7.jpg)
FIRST Computation with ε
● Initially, for all nonterminals A, set
FIRST(A) = { t | A → tω for some ω }
● For all nonterminals A where A → ε is a production, add ε to FIRST(A).
● Repeat the following until no changes occur:● For each production A → α, set
FIRST(A) = FIRST(A) ∪ FIRST*(α)
![Page 8: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/8.jpg)
LL(1) Tables with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9
![Page 9: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/9.jpg)
LL(1) Tables with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9
Num
Sign
Digits
More
Digit
+ - # $
![Page 10: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/10.jpg)
LL(1) Tables with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9
Num Sign Digit Digits
0 51 62 73 84 9
0 51 62 73 84 9
+ -0 51 62 73 84 9
+ -ε
More
0 51 62 73 84 9ε
Num
Sign
Digits
More
Digit
+ - # $
![Page 11: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/11.jpg)
LL(1) Tables with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9
Num Sign Digit Digits
0 51 62 73 84 9
0 51 62 73 84 9
+ -0 51 62 73 84 9
+ -ε
More
0 51 62 73 84 9ε
Num
Sign
Digits
More
Digit
+ - # $
![Page 12: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/12.jpg)
LL(1) Tables with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9
Num Sign Digit Digits
0 51 62 73 84 9
0 51 62 73 84 9
+ -0 51 62 73 84 9
+ -ε
More
0 51 62 73 84 9ε
Num
Sign
Digits
More
Digit
+ - # $Sign Digits Sign Digits
![Page 13: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/13.jpg)
LL(1) Tables with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9
Num Sign Digit Digits
0 51 62 73 84 9
0 51 62 73 84 9
+ -0 51 62 73 84 9
+ -ε
More
0 51 62 73 84 9ε
Num
Sign
Digits
More
Digit
+ - # $Sign Digits Sign Digits
![Page 14: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/14.jpg)
LL(1) Tables with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9
Num Sign Digit Digits
0 51 62 73 84 9
0 51 62 73 84 9
+ -0 51 62 73 84 9
+ -ε
More
0 51 62 73 84 9ε
Num
Sign
Digits
More
Digit
+ - # $Sign Digits Sign Digits
+ -
![Page 15: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/15.jpg)
LL(1) Tables with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9
Num Sign Digit Digits
0 51 62 73 84 9
0 51 62 73 84 9
+ -0 51 62 73 84 9
+ -ε
More
0 51 62 73 84 9ε
Num
Sign
Digits
More
Digit
+ - # $Sign Digits Sign Digits
+ -
![Page 16: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/16.jpg)
LL(1) Tables with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9
Num Sign Digit Digits
0 51 62 73 84 9
0 51 62 73 84 9
+ -0 51 62 73 84 9
+ -ε
More
0 51 62 73 84 9ε
Num
Sign
Digits
More
Digit
+ - # $Sign Digits Sign Digits
+ -Digits More
![Page 17: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/17.jpg)
LL(1) Tables with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9
Num Sign Digit Digits
0 51 62 73 84 9
0 51 62 73 84 9
+ -0 51 62 73 84 9
+ -ε
More
0 51 62 73 84 9ε
Num
Sign
Digits
More
Digit
+ - # $Sign Digits Sign Digits
+ -Digits More
![Page 18: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/18.jpg)
LL(1) Tables with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9
Num Sign Digit Digits
0 51 62 73 84 9
0 51 62 73 84 9
+ -0 51 62 73 84 9
+ -ε
More
0 51 62 73 84 9ε
Num
Sign
Digits
More
Digit
+ - # $Sign Digits Sign Digits
+ -Digits More
Digits
![Page 19: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/19.jpg)
LL(1) Tables with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9
Num Sign Digit Digits
0 51 62 73 84 9
0 51 62 73 84 9
+ -0 51 62 73 84 9
+ -ε
More
0 51 62 73 84 9ε
Num
Sign
Digits
More
Digit
+ - # $Sign Digits Sign Digits
+ -Digits More
Digits
![Page 20: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/20.jpg)
LL(1) Tables with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9
Num Sign Digit Digits
0 51 62 73 84 9
0 51 62 73 84 9
+ -0 51 62 73 84 9
+ -ε
More
0 51 62 73 84 9ε
Num
Sign
Digits
More
Digit
+ - # $Sign Digits Sign Digits
+ -Digits More
Digits
#
![Page 21: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/21.jpg)
LL(1) Tables with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9
Num Sign Digit Digits
0 51 62 73 84 9
0 51 62 73 84 9
+ -0 51 62 73 84 9
+ -ε
More
0 51 62 73 84 9ε
Num
Sign
Digits
More
Digit
+ - # $Sign Digits Sign Digits
+ -Digits More
Digits
#
![Page 22: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/22.jpg)
LL(1) Tables with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9
Num Sign Digit Digits
0 51 62 73 84 9
0 51 62 73 84 9
+ -0 51 62 73 84 9
+ -ε
More
0 51 62 73 84 9ε
Num
Sign
Digits
More
Digit
+ - # $Sign Digits Sign Digits
+ -Digits More
Digits
#
![Page 23: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/23.jpg)
LL(1) Tables with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9
Num Sign Digit Digits
0 51 62 73 84 9
0 51 62 73 84 9
+ -0 51 62 73 84 9
+ -ε
More
0 51 62 73 84 9ε
Num
Sign
Digits
More
Digit
+ - # $Sign Digits Sign Digits Sign Digits
+ -Digits More
Digits
#
![Page 24: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/24.jpg)
LL(1) Tables with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9
Num Sign Digit Digits
0 51 62 73 84 9
0 51 62 73 84 9
+ -0 51 62 73 84 9
+ -ε
More
0 51 62 73 84 9ε
Num
Sign
Digits
More
Digit
+ - # $Sign Digits Sign Digits Sign Digits
+ -Digits More
Digits
#
![Page 25: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/25.jpg)
LL(1) Tables with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9
Num Sign Digit Digits
0 51 62 73 84 9
0 51 62 73 84 9
+ -0 51 62 73 84 9
+ -ε
More
0 51 62 73 84 9ε
Num
Sign
Digits
More
Digit
+ - # $Sign Digits Sign Digits Sign Digits
+ - εDigits More
Digits
#
![Page 26: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/26.jpg)
LL(1) Tables with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9
Num Sign Digit Digits
0 51 62 73 84 9
0 51 62 73 84 9
+ -0 51 62 73 84 9
+ -ε
More
0 51 62 73 84 9ε
Num
Sign
Digits
More
Digit
+ - # $Sign Digits Sign Digits Sign Digits
+ - εDigits More
Digits
#
![Page 27: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/27.jpg)
LL(1) Tables with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9
Num Sign Digit Digits
0 51 62 73 84 9
0 51 62 73 84 9
+ -0 51 62 73 84 9
+ -ε
More
0 51 62 73 84 9ε
Num
Sign
Digits
More
Digit
+ - # $Sign Digits Sign Digits Sign Digits
+ - εDigits More
Digits
#
![Page 28: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/28.jpg)
LL(1) Tables with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9
Num Sign Digit Digits
0 51 62 73 84 9
0 51 62 73 84 9
+ -0 51 62 73 84 9
+ -ε
More
0 51 62 73 84 9ε
Num
Sign
Digits
More
Digit
+ - # $Sign Digits Sign Digits Sign Digits
+ - εDigits More
Digits ε
#
![Page 29: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/29.jpg)
LL(1) Tables with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9
Num Sign Digit Digits
0 51 62 73 84 9
0 51 62 73 84 9
+ -0 51 62 73 84 9
+ -ε
More
0 51 62 73 84 9ε
Num
Sign
Digits
More
Digit
+ - # $Sign Digits Sign Digits Sign Digits
+ - εDigits More
Digits ε
#
![Page 30: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/30.jpg)
LL(1) Tables with εNum → Sign DigitsSign → + | - | εDigits → Digit MoreMore → Digits | εDigit → 0 | 1 | … | 9
Num Sign Digit Digits
0 51 62 73 84 9
0 51 62 73 84 9
+ -0 51 62 73 84 9
+ -ε
More
0 51 62 73 84 9ε
Num
Sign
Digits
More
Digit
+ - # $Sign Digits Sign Digits Sign Digits
+ - εDigits More
Digits ε
#
![Page 31: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/31.jpg)
FOLLOW Sets
● With ε-productions in the grammar, we may have to “look past” the current nonterminal to what can come after it.
● The FOLLOW set represents the set of terminals that might come after a given nonterminal.
● Formally:
FOLLOW(A) = { t | S ⇒* αAtω for some α, ω }
where S is the start symbol of the grammar.● Informally, every nonterminal that can ever come after
A in a derivation.
![Page 32: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/32.jpg)
Computation of FOLLOW Sets
● Initially, for each nonterminal A, set
FOLLOW(A) = { t | B → αAtω is a production }
● Add $ to FOLLOW(S), where S is the start symbol.
● Repeat the following until no changes occur:● If B → αAω is a production, set
FOLLOW(A) = FOLLOW(A) ∪ FIRST*(ω) - { ε }.● If B → αAω is a production and ε ∈ FIRST*(ω), set
FOLLOW(A) = FOLLOW(A) ∪ FOLLOW(B).
![Page 33: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/33.jpg)
The Final LL(1) Table Algorithm
● Compute FIRST(A) and FOLLOW(A) for all nonterminals A.
● For each rule A → ω, for each terminal t ∈ FIRST*(ω), set T[A, t] = ω.● Note that ε is not a terminal.
● For each rule A → ω, if ε ∈ FIRST*(ω), set T[A, t] = ω for each t ∈ FOLLOW(A).
![Page 34: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/34.jpg)
An Egregious Abuse of Notation
● Compute FIRST(A) and FOLLOW(A) for all nonterminals A.
● For each rule A → ω, for each terminal t ∈ FIRST*(ωFOLLOW(A)), setT[A, t] = ω.
![Page 35: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/35.jpg)
Example LL(1) Construction
![Page 36: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/36.jpg)
The Limits of LL(1)
![Page 37: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/37.jpg)
A Grammar that is Not LL(1)
● Consider the following (left-recursive) grammar:
A → Ab | c
● FIRST(A) = {c}
● However, we cannot build an LL(1) parse table.● Why?
Cannot uniquely predict production!
This is called a FIRST/FIRST conflict.
![Page 38: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/38.jpg)
A Grammar that is Not LL(1)
● Consider the following (left-recursive) grammar:
A → Ab | c
● FIRST(A) = {c}
● However, we cannot build an LL(1) parse table.● Why?
Cannot uniquely predict production!
This is called a FIRST/FIRST conflict.
A
b cA → AbA → c
![Page 39: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/39.jpg)
A Grammar that is Not LL(1)
● Consider the following (left-recursive) grammar:
A → Ab | c
● FIRST(A) = {c}
● However, we cannot build an LL(1) parse table.● Why?
● Cannot uniquely predict production!● This is called a FIRST/FIRST conflict.
A
b cA → AbA → c
![Page 40: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/40.jpg)
Eliminating Left Recursion
● In general, left recursion can be converted into right recursion by a mechanical transformation.
● Consider the grammar
A → Aω | α● This will produce α followed by some number of ω's.
● Can rewrite the grammar as
A → αB
B → ε | ωB
![Page 41: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/41.jpg)
Another Non-LL(1) Grammar
● Consider the following grammar:
E → T
E → T + E
T → int
T → (E)
● FIRST(E) = { int, ( }
● FIRST(T) = { int, ( }● Why is this grammar not LL(1)?
![Page 42: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/42.jpg)
Another Non-LL(1) Grammar
● Consider the following grammar:
E → T
E → T + E
T → int
T → (E)
● FIRST(E) = { int, ( }
● FIRST(T) = { int, ( }● Why is this grammar not LL(1)?
How do you predict which of these to use?
![Page 43: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/43.jpg)
Left-Factoring
E → T E → T + ET → intT → (E)
![Page 44: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/44.jpg)
Left-Factoring
E → TεE → T + ET → intT → (E)
![Page 45: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/45.jpg)
Left-Factoring
E → TY
T → intT → (E)
![Page 46: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/46.jpg)
Left-Factoring
E → TY
T → intT → (E)Y → + EY → ε
![Page 47: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/47.jpg)
Left-Factoring
E → TY 1
T → int 2T → (E) 3Y → + E 4Y → ε 5
![Page 48: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/48.jpg)
Left-Factoring
E → TY 1T → int 2T → (E) 3Y → + E 4Y → ε 5
![Page 49: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/49.jpg)
Left-Factoring
E → TY 1T → int 2T → (E) 3Y → + E 4Y → ε 5
E T YFIRST
E T YFOLLOW
![Page 50: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/50.jpg)
Left-Factoring
E → TY 1T → int 2T → (E) 3Y → + E 4Y → ε 5
E Tint(
YFIRST
E T YFOLLOW
![Page 51: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/51.jpg)
Left-Factoring
E → TY 1T → int 2T → (E) 3Y → + E 4Y → ε 5
E Tint(
Y+ε
FIRST
E T YFOLLOW
![Page 52: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/52.jpg)
Left-Factoring
E → TY 1T → int 2T → (E) 3Y → + E 4Y → ε 5
Eint(
Tint(
Y+ε
FIRST
E T YFOLLOW
![Page 53: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/53.jpg)
Left-Factoring
E → TY 1T → int 2T → (E) 3Y → + E 4Y → ε 5
Eint(
Tint(
Y+ε
FIRST
E$
T YFOLLOW
![Page 54: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/54.jpg)
Left-Factoring
E → TY 1T → int 2T → (E) 3Y → + E 4Y → ε 5
Eint(
Tint(
Y+ε
FIRST
E$)
T YFOLLOW
![Page 55: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/55.jpg)
Left-Factoring
E → TY 1T → int 2T → (E) 3Y → + E 4Y → ε 5
Eint(
Tint(
Y+ε
FIRST
E$)
T+
YFOLLOW
![Page 56: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/56.jpg)
Left-Factoring
E → TY 1T → int 2T → (E) 3Y → + E 4Y → ε 5
Eint(
Tint(
Y+ε
FIRST
E$)
T+
Y$)
FOLLOW
![Page 57: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/57.jpg)
Left-Factoring
E → TY 1T → int 2T → (E) 3Y → + E 4Y → ε 5
Eint(
Tint(
Y+ε
FIRST
E$)
T+$)
Y$)
FOLLOW
![Page 58: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/58.jpg)
Left-Factoring
E → TY 1T → int 2T → (E) 3Y → + E 4Y → ε 5
Eint(
Tint(
Y+ε
FIRST
E$)
T+$)
Y$)
FOLLOW
E
T
Y
int ( ) + $
![Page 59: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/59.jpg)
Left-Factoring
E → TY 1T → int 2T → (E) 3Y → + E 4Y → ε 5
Eint(
Tint(
Y+ε
FIRST
E$)
T+$)
Y$)
FOLLOW
E
T
Y
int ( ) + $
11
![Page 60: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/60.jpg)
Left-Factoring
E → TY 1T → int 2T → (E) 3Y → + E 4Y → ε 5
Eint(
Tint(
Y+ε
FIRST
E$)
T+$)
Y$)
FOLLOW
E
T
Y
int ( ) + $
12 31
![Page 61: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/61.jpg)
Left-Factoring
E → TY 1T → int 2T → (E) 3Y → + E 4Y → ε 5
Eint(
Tint(
Y+ε
FIRST
E$)
T+$)
Y$)
FOLLOW
E
T
Y
int ( ) + $
12 3
4
1
![Page 62: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/62.jpg)
Left-Factoring
E → TY 1T → int 2T → (E) 3Y → + E 4Y → ε 5
Eint(
Tint(
Y+ε
FIRST
E$)
T+$)
Y$)
FOLLOW
E
T
Y
int ( ) + $
12 3
5 4 5
1
![Page 63: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/63.jpg)
A Formal Characterization of LL(1)
● A grammar G is LL(1) iff for any productionsA → ω1 and A → ω2, the sets
FIRST(ω1 FOLLOW(A))
and
FIRST(ω2 FOLLOW(A))
are disjoint.● This condition is equivalent to saying that there
are no conflicts in the table.
![Page 64: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/64.jpg)
The Strengths of LL(1)
![Page 65: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/65.jpg)
LL(1) is Straightforward
● Can be implemented quickly with a table-driven design.
● Can be implemented by recursive descent:● Define a function for each nonterminal.● Have these functions call each other based
on the lookahead token.
● See Handout #09 for more details.
![Page 66: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/66.jpg)
LL(1) is Fast
● Both table-driven LL(1) and recursive-descent-powered LL(1) are fast.
● Can parse in O(n |G|) time, where n is the length of the string and |G| is the size of the grammar.
![Page 67: Top-Down Parsing II - Stanford University · 2012. 8. 13. · Top-down parsing Leftmost BFS is one approach to top-down parsing; it is mostly Leftmost DFS is another approach to top-down](https://reader035.vdocuments.us/reader035/viewer/2022081411/60b2f7b72d765107f93ab016/html5/thumbnails/67.jpg)
Summary
● Top-down parsing tries to derive the user's program from the start symbol.
● Leftmost BFS is one approach to top-down parsing; it is mostly of theoretical interest.
● Leftmost DFS is another approach to top-down parsing that is uncommon in practice.
● LL(1) parsing scans from left-to-right, using one token of lookahead to find a leftmost derivation.
● FIRST sets contain terminals that may be the first symbol of a production.
● FOLLOW sets contain terminals that may follow a nonterminal in a production.
● Left recursion and left factorability cause LL(1) to fail and can be mechanically eliminated in some cases.