Compiler Construction Sohail Aslam Lecture 19 LL1 Table
Compiler Construction Sohail Aslam Lecture 19
LL(1) Table Construction For each production A → a 1. for each terminal a in FIRST(a), add A → a to M[A, a]. 2. If e is in FIRST(a), add A → a to M[A, b] for each terminal b in FOLLOW(A). 2
LL(1) Table Construction For each production A → a 1. for each terminal a in FIRST(a), add A → a to M[A, a]. 2. If e is in FIRST(a), add A → a to M[A, b] for each terminal b in FOLLOW(A). 3
LL(1) Table Construction For each production A → a 1. for each terminal a in FIRST(a), add A → a to M[A, a]. 2. If e is in FIRST(a), add A → a to M[A, b] for each terminal b in FOLLOW(A). 4
LL(1) Table Construction (2 cont. ) If e is in FIRST(a), and $ is in FOLLOW(A), add A → a to M[A, $]. 5
LL(1) Table Construction Make each undefined entry of M be error. 6
LL(1) Table Construction Let us apply the algorithm to the expression grammar. 7
LL(1) Table Construction Since FIRST(TE ') = FIRST(T ) = { (, id }, production E →TE' cause M[E, (] and M[E, id] to get E →TE' 8
LL(1) Table Construction Production E' → +TE' causes M[E', +] to get E' →+TE' 10
LL(1) Table Construction Production E' → e causes M[E', )] and M[E', $] to get E' → e since FOLLOW (E' ) = { ), $ } 12
LL(1) Table Construction And so on. The final parsing table produced is: 14
LL(1) Parsing Table id + * E E →TE' E' T T →FT' $ E' →e T →FT' T' →e T →*FT' F → id ) E →TE' E' → +TE' T' F ( T' →e F →(E ) 15
Left Factoring Consider the grammar E→T+E | T T → int | int T | (E) 16
Left Factoring E→T+E | T T → int | int T | (E) Impossible to predict because for T, two productions start with int 17
Left Factoring E→T+E | T T → int | int T | (E) For E, it is not clear how to predict 18
Left Factoring A grammar must be left factored before use for predictive parsing 19
Left Factoring If a e, replace all productions A → ab 1 | ab 2 |. . | abn | g with A→a. Z|g Z → b 1| b 2|. . | bn where Z is new nonterminal 20
Left Factoring If a e, replace all productions A → ab 1 | ab 2 |. . | abn | g with A→a. Z|g Z → b 1| b 2|. . | bn where Z is new nonterminal 21
Left Factoring A graphical explanation ab 1 A ab 2 ab 3 22
Left Factoring becomes b 1 A a. Z b 2 b 3 23
Left Factoring Consider following fragment of expression grammar Factor → id | id [ Expr. List ] | id ( Expr. List ) 24
Left Factoring Factor → id | id [ Expr. List ] | id ( Expr. List ) FIRST(rhs 1) = {id} FIRST(rhs 2) = {id} FIRST(rhs 3) = {id} 25
Left Factoring after left factoring Factor → id Args → [ Expr. List ] | ( Expr. List ) | e 26
Left Factoring Factor Args → → | | id Args [ Expr. List ] ( Expr. List ) e FIRST(rhs 1) = {id} FIRST(rhs 2) = { [ } FIRST(rhs 3) = { ( } FIRST(rhs 4) = FOLLOW(Factor) 27
Left Factoring id Factor id [ Expr. List ] id ( Expr. List ) No basis for choice 28
Left Factoring e Factor id [ Expr. List ] ( Expr. List ) correct choice possible 29
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? ) 30
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 31
- Slides: 31