Download presentation
Presentation is loading. Please wait.
Published byMariah Ramsey Modified over 9 years ago
1
Compiler Construction Sohail Aslam Lecture 19
2
2 LL(1) Table Construction For 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
3 LL(1) Table Construction For 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
4 LL(1) Table Construction For 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
5 LL(1) Table Construction (2 cont.) If is in FIRST( ), and $ is in FOLLOW(A), add A → to M [ A,$ ].
6
6 LL(1) Table Construction Make each undefined entry of M be error.
7
7 LL(1) Table Construction Let us apply the algorithm to the expression grammar.
8
8 LL(1) Table Construction Since FIRST(TE ') = FIRST(T ) = { (, id }, production E → TE' cause M [ E,( ] and M[E,id] to get E → TE'
9
9 id +*()$ EE → TE' E'E' → +TE' E' → TT → FT' T' T' → T → *FT' T' → FF → idF → (E )
10
10 LL(1) Table Construction Production E' → +TE' causes M[E',+] to get E' → +TE'
11
11 id +*()$ EE → TE' E'E' → +TE' E' → TT → FT' T' T' → T → *FT' T' → FF → idF → (E )
12
12 LL(1) Table Construction Production E' → causes M[E',)] and M[E',$] to get E' → since FOLLOW (E' ) = { ), $ }
13
13 id +*()$ EE → TE' E'E' → +TE' E' → TT → FT' T' T' → T → *FT' T' → FF → idF → (E )
14
14 LL(1) Table Construction And so on. The final parsing table produced is:
15
15 LL(1) Parsing Table id +*()$ EE → TE' E'E' → +TE' E' → TT → FT' T' T' → T → *FT' T' → FF → idF → (E )
16
16 Left Factoring Consider the grammar E → T + E | T T → int | int T | (E)
17
17 Left Factoring E → T + E | T T → int | int T | (E) Impossible to predict because for T, two productions start with int
18
18 Left Factoring E → T + E | T T → int | int T | (E) For E, it is not clear how to predict
19
19 Left Factoring A grammar must be left factored before use for predictive parsing
20
20 Left Factoring If , replace all productions A → 1 | 2 |.... | n | with A → Z | Z → 1 | 2 |.... | n where Z is new nonterminal
21
21 Left Factoring If , replace all productions A → 1 | 2 |.... | n | with A → Z | Z → 1 | 2 |.... | n where Z is new nonterminal
22
22 Left Factoring A graphical explanation A 2 1 3
23
23 Left Factoring becomes ZZ 22 11 33 A
24
24 Left Factoring Consider following fragment of expression grammar Factor → id | id [ ExprList ] | id ( ExprList )
25
25 Left Factoring Factor → id | id [ ExprList ] | id ( ExprList ) FIRST(rhs 1 ) = {id} FIRST(rhs 2 ) = {id} FIRST(rhs 3 ) = {id}
26
26 Left Factoring after left factoring Factor → id Args Args → [ ExprList ] | ( ExprList ) |
27
27 Left Factoring Factor → id Args Args → [ ExprList ] | ( ExprList ) | FIRST(rhs 1 ) = {id} FIRST(rhs 2 ) = { [ } FIRST(rhs 3 ) = { ( } FIRST(rhs 4 ) = FOLLOW(Factor)
28
28 Left Factoring [ ( Factor id ExprList ] ) id No basis for choice
29
29 Left Factoring [ ( Factor id ExprList ] ) correct choice possible
30
30 Left Factoring Question 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
31 Left Factoring Answer Given a CFG that does not meet the LL(1) condition, it is undecidable whether or not an equivalent LL(1) grammar exists
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.