chapter 3 chang chi-chung 2015.6.8
DESCRIPTION
LL(1) Grammar Predictive parsers, that is, recursive-descent parsers needing no backtracking, can be constructed for a class of grammars called LL(1) First “L” means the input from left to right. Second “L” means leftmost derivation. “1” for using one input symbol of lookahead at each step tp make parsing action decisions. No left-recursive. No ambiguous.TRANSCRIPT
![Page 1: Chapter 3 Chang Chi-Chung 2015.6.8](https://reader034.vdocuments.us/reader034/viewer/2022052308/5a4d1af77f8b9ab059981ed7/html5/thumbnails/1.jpg)
Chapter 3
Chang Chi-Chung2015.6.8
![Page 2: Chapter 3 Chang Chi-Chung 2015.6.8](https://reader034.vdocuments.us/reader034/viewer/2022052308/5a4d1af77f8b9ab059981ed7/html5/thumbnails/2.jpg)
LL(1) Grammar Predictive parsers, that is, recursive-descent parsers
needing no backtracking, can be constructed for a class of grammars called LL(1)
First “L” means the input from left to right. Second “L” means leftmost derivation. “1” for using one input symbol of lookahead at each
step tp make parsing action decisions. No left-recursive. No ambiguous.
![Page 3: Chapter 3 Chang Chi-Chung 2015.6.8](https://reader034.vdocuments.us/reader034/viewer/2022052308/5a4d1af77f8b9ab059981ed7/html5/thumbnails/3.jpg)
LL(1) 文法 明確性文法 (No Ambiguity) 不可以有左遞迴 (No Left Recursion) 不可以有左因子 (No Left Factor)
![Page 4: Chapter 3 Chang Chi-Chung 2015.6.8](https://reader034.vdocuments.us/reader034/viewer/2022052308/5a4d1af77f8b9ab059981ed7/html5/thumbnails/4.jpg)
Top-Down Parsing LL methods and recursive-descent parsing
Left-to-right, Leftmost derivation Creating the nodes of the parse tree in preorder ( depth-
first )
GrammarE T + TT ( E )T - ET id
Leftmost derivationE lm T + T lm id + T lm id + id
E E
T
+
T
idid
E
TT
+
E
T
+
T
id
![Page 5: Chapter 3 Chang Chi-Chung 2015.6.8](https://reader034.vdocuments.us/reader034/viewer/2022052308/5a4d1af77f8b9ab059981ed7/html5/thumbnails/5.jpg)
Top-down Parsing
Give a Grammar G
E → T E’ E’ → + T E’ | εT → F T’T’ → * F T’ | εF → ( E ) | id
E E + T | TT T * F | FF ( E ) | id
![Page 6: Chapter 3 Chang Chi-Chung 2015.6.8](https://reader034.vdocuments.us/reader034/viewer/2022052308/5a4d1af77f8b9ab059981ed7/html5/thumbnails/6.jpg)
Elimination of Left Recursion Productions of the form
A A |
are left recursive Non-left-recursions
A A’ A’ A’ | ε
When one of the productions in a grammar is left recursive then a predictive parser loops forever on certain inputs
![Page 7: Chapter 3 Chang Chi-Chung 2015.6.8](https://reader034.vdocuments.us/reader034/viewer/2022052308/5a4d1af77f8b9ab059981ed7/html5/thumbnails/7.jpg)
Immediate Left-Recursion Elimination Group the Productions as
A A1 | A2 | … | Am | 1 | 2 | … | n
Where no i begins with an A Replace the A-Productions by
A 1 A’ | 2 A’ | … | n A’
A’ 1 A’ | 2 A’ | … | m A’ | ε
![Page 8: Chapter 3 Chang Chi-Chung 2015.6.8](https://reader034.vdocuments.us/reader034/viewer/2022052308/5a4d1af77f8b9ab059981ed7/html5/thumbnails/8.jpg)
Example Left-recursive grammar
A A | | | A
Into a right-recursive production A AR
| AR
AR AR
| AR |
![Page 9: Chapter 3 Chang Chi-Chung 2015.6.8](https://reader034.vdocuments.us/reader034/viewer/2022052308/5a4d1af77f8b9ab059981ed7/html5/thumbnails/9.jpg)
Non-Immediate Left-Recursion The Grammar
S A a | b A A c | S d | ε
The nonterminal S is left recursive, because S A a Sda But S is not immediately left recursive.
![Page 10: Chapter 3 Chang Chi-Chung 2015.6.8](https://reader034.vdocuments.us/reader034/viewer/2022052308/5a4d1af77f8b9ab059981ed7/html5/thumbnails/10.jpg)
Elimination of Left Recursion Eliminating left recursion algorithm
Arrange the nonterminals in some order A1, A2, …, Anfor (each i from 1 to n) {
for (each j from 1 to i-1){ replace each production
Ai Ajwith
Ai 1 | 2 | … | kwhere
Aj 1 | 2 | … | k}
eliminate the immediate left recursion in Ai}
![Page 11: Chapter 3 Chang Chi-Chung 2015.6.8](https://reader034.vdocuments.us/reader034/viewer/2022052308/5a4d1af77f8b9ab059981ed7/html5/thumbnails/11.jpg)
Example A B C | aB C A | A bC A B | C C | a
i = 1 nothing to doi = 2, j = 1 B C A | A b
B C A | B C b | a b(imm) B C A BR | a b BR
BR C b BR |
i = 3, j = 1 C A B | C C | a C B C B | a B | C C | a
i = 3, j = 2 C B C B | a B | C C | aC C A BR C B | a b BR C B | a B | C C | a(imm)C a b BR C B CR | a B CR | a CR
CR A BR C B CR | C CR |
![Page 12: Chapter 3 Chang Chi-Chung 2015.6.8](https://reader034.vdocuments.us/reader034/viewer/2022052308/5a4d1af77f8b9ab059981ed7/html5/thumbnails/12.jpg)
Exercise
The grammarS A a | b A A c | S d | ε
Answer A A c | A a d | b d |
![Page 13: Chapter 3 Chang Chi-Chung 2015.6.8](https://reader034.vdocuments.us/reader034/viewer/2022052308/5a4d1af77f8b9ab059981ed7/html5/thumbnails/13.jpg)
Left Factoring
Left Factoring is a grammar transformation. Predictive Parsing Top-down Parsing
Replace productionsA 1 | 2 | … | n |
withA AR | AR 1 | 2 | … | n
![Page 14: Chapter 3 Chang Chi-Chung 2015.6.8](https://reader034.vdocuments.us/reader034/viewer/2022052308/5a4d1af77f8b9ab059981ed7/html5/thumbnails/14.jpg)
Example
The Grammar stmt if expr then stmt
| if expr then stmt else stmt Replace with
stmt if expr then stmt stmtsstmts else stmt | ε
![Page 15: Chapter 3 Chang Chi-Chung 2015.6.8](https://reader034.vdocuments.us/reader034/viewer/2022052308/5a4d1af77f8b9ab059981ed7/html5/thumbnails/15.jpg)
Exercise
The following grammarS i E t S | i E t S e S | aE b
AnswerS i E t S S’ | aS’ e S | εE b
![Page 16: Chapter 3 Chang Chi-Chung 2015.6.8](https://reader034.vdocuments.us/reader034/viewer/2022052308/5a4d1af77f8b9ab059981ed7/html5/thumbnails/16.jpg)
LL(1)
![Page 17: Chapter 3 Chang Chi-Chung 2015.6.8](https://reader034.vdocuments.us/reader034/viewer/2022052308/5a4d1af77f8b9ab059981ed7/html5/thumbnails/17.jpg)
LL(1) A grammar G is LL(1) if it is not left recursive
and for each collection of productionsA 1 | 2 | … | n
for nonterminal A the following holds: FIRST(i) FIRST(j) = for all i j如果交集不是空集合,會如何? if i * then
j * for all i j FIRST(j) FOLLOW(A) = for all i j
![Page 18: Chapter 3 Chang Chi-Chung 2015.6.8](https://reader034.vdocuments.us/reader034/viewer/2022052308/5a4d1af77f8b9ab059981ed7/html5/thumbnails/18.jpg)
ExampleGrammar Not LL(1) because:
S S a | a Left recursive
S a S | a FIRST(a S) FIRST(a) ={a}
S a R | R S |
For R: S * and *
S a R aR S |
For R:FIRST(S) FOLLOW(R)
![Page 19: Chapter 3 Chang Chi-Chung 2015.6.8](https://reader034.vdocuments.us/reader034/viewer/2022052308/5a4d1af77f8b9ab059981ed7/html5/thumbnails/19.jpg)
Top-down Parsing
Give a Grammar G
E → T E’ E’ → + T E’ | εT → F T’T’ → * F T’ | εF → ( E ) | id
E E + T | TT T * F | FF ( E ) | id
![Page 20: Chapter 3 Chang Chi-Chung 2015.6.8](https://reader034.vdocuments.us/reader034/viewer/2022052308/5a4d1af77f8b9ab059981ed7/html5/thumbnails/20.jpg)
Example
Give a Grammar GE → T E’ E’ → + T E’ | εT → F T’T’ → * F T’ | εF → ( E ) | id
FIRSTE ( idE’ + T ( idT’ * F ( id
FOLLOW
E $ )E’ $ )T + $ )T’ + $ )F * + $ )
![Page 21: Chapter 3 Chang Chi-Chung 2015.6.8](https://reader034.vdocuments.us/reader034/viewer/2022052308/5a4d1af77f8b9ab059981ed7/html5/thumbnails/21.jpg)
Non-Recursive Predictive Parsing Table-Driven Parsing Given an LL(1) grammar G = <N, T, P, S>
construct a table M[A,a] for A N, a T and use a driver program with a stack
Predictive parsingprogram (driver)
Parsing tableM
a + b $
XYZ$
stackinput
output
![Page 22: Chapter 3 Chang Chi-Chung 2015.6.8](https://reader034.vdocuments.us/reader034/viewer/2022052308/5a4d1af77f8b9ab059981ed7/html5/thumbnails/22.jpg)
Predictive Parsing Table Algorithm
![Page 23: Chapter 3 Chang Chi-Chung 2015.6.8](https://reader034.vdocuments.us/reader034/viewer/2022052308/5a4d1af77f8b9ab059981ed7/html5/thumbnails/23.jpg)
ExampleE T E’E’ + T E’ | T F T ’ T ’ * F T ’ | F ( E ) | id
id + * ( ) $E E T E’ E T E’E’ E’ + T E’ E’ E’ T T F T’ T F T’T’ T’ T’ * F T’ T’ T’ F F id F ( E )
A FIRST() FOLLOW(A)E T E’ ( id $ )
E’ + T E’ + $ )E’ $ )
T F T’ ( id + $ )T’ * F T’ * + $ )
T’ + $ )F ( E ) ( * + $ )F id id * + $ )
![Page 24: Chapter 3 Chang Chi-Chung 2015.6.8](https://reader034.vdocuments.us/reader034/viewer/2022052308/5a4d1af77f8b9ab059981ed7/html5/thumbnails/24.jpg)
Example MATCHED STACK INPUT ACTIONE$ id + id * id$
TE’$ id + id * id$ E T E’
FT’E’$ id + id * id$ T F T ’
id T’E’$ id + id * id$ F id
id T’E’$ + id * id$ match id
id E’$ + id * id$ T ’
id +TE’$ + id * id$ E’ + T E ’
id + TE’$ id * id$ match +
id + FT’E’$ id * id$ T F T’
id + id T’E’$ id * id$ F id
id + id T’E’$ * id$ match id
id + id * FT’E’$ * id$ T’ * F T’
id + id * FT’E’$ id$ match *
id + id * id T’E’$ id$ F id
id + id * id T’E’$ $ Match id
id + id * id E’$ $ T’
id + id * id $ $ E’
Table-Driven Parsing
E T E’E’ + T E’ | T F T ’ T ’ * F T ’ | F ( E ) | id
![Page 25: Chapter 3 Chang Chi-Chung 2015.6.8](https://reader034.vdocuments.us/reader034/viewer/2022052308/5a4d1af77f8b9ab059981ed7/html5/thumbnails/25.jpg)
Exercise Give a Grammar G as below
S i E t S S’S’ e S | εE b
Calculate the FIRST and FOLLOW Create a predictive parsing table
![Page 26: Chapter 3 Chang Chi-Chung 2015.6.8](https://reader034.vdocuments.us/reader034/viewer/2022052308/5a4d1af77f8b9ab059981ed7/html5/thumbnails/26.jpg)
AnswerAmbiguous grammar
S i E t S S’ | aS’ e S | E b
a b e i t $S S a S i E t S S’
S’ S’ S’ e S S’
E E b
A FIRST() FOLLOW(A)S i E t S S’ i e $
S a a e $S’ e S e e $S’ e $E b b t
Error: duplicate table entry