Date post: | 24-Dec-2015 |
Category: |
Documents |
Upload: | mariah-ramsey |
View: | 215 times |
Download: | 1 times |
Compiler Compiler ConstructionConstruction
Compiler Compiler ConstructionConstruction
Sohail Aslam
Lecture 19
2
LL(1) Table ConstructionLL(1) Table ConstructionLL(1) Table ConstructionLL(1) Table ConstructionFor each production A →
1. for each terminal a in FIRST(), add A → to M[A,a].
2. If is in FIRST(), add A → to M[A,b] for each terminal b in FOLLOW(A).
3
LL(1) Table ConstructionLL(1) Table ConstructionLL(1) Table ConstructionLL(1) Table ConstructionFor each production A →
1. for each terminal a in FIRST(), add A → to M[A,a].
2. If is in FIRST(), add A → to M[A,b] for each terminal b in FOLLOW(A).
4
LL(1) Table ConstructionLL(1) Table ConstructionLL(1) Table ConstructionLL(1) Table ConstructionFor each production A →
1. for each terminal a in FIRST(), add A → to M[A,a].
2. If is in FIRST(), add A → to M[A,b] for each terminal b in FOLLOW(A).
5
LL(1) Table ConstructionLL(1) Table ConstructionLL(1) Table ConstructionLL(1) Table Construction
(2 cont.)If is in FIRST(), and $ is in FOLLOW(A), add A → to M[A,$].
6
LL(1) Table ConstructionLL(1) Table ConstructionLL(1) Table ConstructionLL(1) Table Construction
Make each undefined entry of M be error.
7
LL(1) Table ConstructionLL(1) Table ConstructionLL(1) Table ConstructionLL(1) Table Construction
Let us apply the algorithm to the expression grammar.
8
LL(1) Table ConstructionLL(1) Table ConstructionLL(1) Table ConstructionLL(1) Table Construction
Since FIRST(TE ') = FIRST(T ) = { (, id },production E →TE' cause M[E,(] and
M[E,id] to get E →TE'
9
id + * ( ) $
E E →TE' E →TE'
E' E' → +TE'
E' → E' →
T T →FT' T →FT'T' T' → T
→*FT'T' → T' →
F F → id F →(E )
10
LL(1) Table ConstructionLL(1) Table ConstructionLL(1) Table ConstructionLL(1) Table Construction
Production E' → +TE' causes
M[E',+] to get E' →+TE'
11
id + * ( ) $
E E →TE' E →TE'
E' E' → +TE'
E' → E' →
T T →FT' T →FT'T' T' → T
→*FT'T' → T' →
F F → id F →(E )
12
LL(1) Table ConstructionLL(1) Table ConstructionLL(1) Table ConstructionLL(1) Table Construction
Production E' → causes M[E',)] and M[E',$] to get E' → since
FOLLOW (E' ) = { ), $ }
13
id + * ( ) $
E E →TE' E →TE'
E' E' → +TE'
E' → E' →
T T →FT' T →FT'T' T' → T
→*FT'T' → T' →
F F → id F →(E )
14
LL(1) Table ConstructionLL(1) Table ConstructionLL(1) Table ConstructionLL(1) Table Construction
And so on.
The final parsing table produced is:
15
LL(1) Parsing TableLL(1) Parsing TableLL(1) Parsing TableLL(1) Parsing Tableid + * ( ) $
E E →TE' E →TE'
E' E' → +TE'
E' → E' →
T T →FT' T →FT'T' T' → T
→*FT'T' → T' →
F F → id F →(E )
16
Left FactoringLeft FactoringLeft FactoringLeft Factoring
Consider the grammar
E → T + E | TT → int | int T | (E)
17
Left FactoringLeft FactoringLeft FactoringLeft Factoring
E → T + E | TT → int | int T | (E)
Impossible to predict because for T, two productions start with int
18
Left FactoringLeft FactoringLeft FactoringLeft Factoring
E → T + E | TT → int | int T | (E)
For E, it is not clear how to predict
19
Left FactoringLeft FactoringLeft FactoringLeft Factoring
A grammar must be left factored before use for predictive parsing
20
Left FactoringLeft FactoringLeft FactoringLeft Factoring
If , replace all productionsA → 1 | 2 | .... | n |
withA → Z |
Z → 1| 2| .... | n
where Z is new nonterminal
21
Left FactoringLeft FactoringLeft FactoringLeft Factoring
If , replace all productionsA → 1 | 2 | .... | n |
withA → Z |
Z → 1| 2| .... | n
where Z is new nonterminal
22
Left FactoringLeft FactoringLeft FactoringLeft FactoringA graphical explanation
A 2
1
3
23
Left FactoringLeft FactoringLeft FactoringLeft Factoring
becomes
Z 2
1
3
A
24
Left FactoringLeft FactoringLeft FactoringLeft Factoring
Consider following fragment of expression grammarFactor → id
| id [ ExprList ] | id ( ExprList )
25
Left FactoringLeft FactoringLeft FactoringLeft FactoringFactor → id
| id [ ExprList ]| id ( ExprList )
FIRST(rhs1) = {id}FIRST(rhs2) = {id}FIRST(rhs3) = {id}
26
Left FactoringLeft FactoringLeft FactoringLeft Factoring
after left factoring
Factor → id ArgsArgs → [ ExprList ]
| ( ExprList ) |
27
Left FactoringLeft FactoringLeft FactoringLeft FactoringFactor → id ArgsArgs → [ ExprList ]
| ( ExprList ) |
FIRST(rhs1) = {id}FIRST(rhs2) = { [ }FIRST(rhs3) = { ( }
FIRST(rhs4) = FOLLOW(Factor)
28
Left FactoringLeft FactoringLeft FactoringLeft Factoring
[
(
Factor id ExprList
ExprList
]
)id
id
No basis for choice
29
Left FactoringLeft FactoringLeft FactoringLeft Factoring
[
(
Factor id ExprList
ExprList
]
)
correct choice possible
30
Left FactoringLeft FactoringLeft FactoringLeft FactoringQuestion
By eliminating left recursion and left factoring, can we transform an arbitrary CFG to a form where it meets the LL(1) condition (and can be parsed predictively with a single token lookahead?)
31
Left FactoringLeft FactoringLeft FactoringLeft Factoring
AnswerGiven a CFG that does not meet the LL(1) condition, it is undecidable whether or not an equivalent LL(1) grammar exists