+ All Categories
Home > Documents > Compiler Construction Sohail Aslam Lecture 19. 2 LL(1) Table Construction For each production A → ...

Compiler Construction Sohail Aslam Lecture 19. 2 LL(1) Table Construction For each production A → ...

Date post: 24-Dec-2015
Category:
Upload: mariah-ramsey
View: 215 times
Download: 1 times
Share this document with a friend
31
Compiler Compiler Construction Construction Sohail Aslam Lecture 19
Transcript
Page 1: Compiler Construction Sohail Aslam Lecture 19. 2 LL(1) Table Construction For each production A →  1.for each terminal a in FIRST(  ), add A →  to.

Compiler Compiler ConstructionConstruction

Compiler Compiler ConstructionConstruction

Sohail Aslam

Lecture 19

Page 2: Compiler Construction Sohail Aslam Lecture 19. 2 LL(1) Table Construction For each production A →  1.for each terminal a in FIRST(  ), add A →  to.

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).

Page 3: Compiler Construction Sohail Aslam Lecture 19. 2 LL(1) Table Construction For each production A →  1.for each terminal a in FIRST(  ), add A →  to.

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).

Page 4: Compiler Construction Sohail Aslam Lecture 19. 2 LL(1) Table Construction For each production A →  1.for each terminal a in FIRST(  ), add A →  to.

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).

Page 5: Compiler Construction Sohail Aslam Lecture 19. 2 LL(1) Table Construction For each production A →  1.for each terminal a in FIRST(  ), add A →  to.

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,$].

Page 6: Compiler Construction Sohail Aslam Lecture 19. 2 LL(1) Table Construction For each production A →  1.for each terminal a in FIRST(  ), add A →  to.

6

LL(1) Table ConstructionLL(1) Table ConstructionLL(1) Table ConstructionLL(1) Table Construction

Make each undefined entry of M be error.

Page 7: Compiler Construction Sohail Aslam Lecture 19. 2 LL(1) Table Construction For each production A →  1.for each terminal a in FIRST(  ), add A →  to.

7

LL(1) Table ConstructionLL(1) Table ConstructionLL(1) Table ConstructionLL(1) Table Construction

Let us apply the algorithm to the expression grammar.

Page 8: Compiler Construction Sohail Aslam Lecture 19. 2 LL(1) Table Construction For each production A →  1.for each terminal a in FIRST(  ), add A →  to.

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'

Page 9: Compiler Construction Sohail Aslam Lecture 19. 2 LL(1) Table Construction For each production A →  1.for each terminal a in FIRST(  ), add A →  to.

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 )

Page 10: Compiler Construction Sohail Aslam Lecture 19. 2 LL(1) Table Construction For each production A →  1.for each terminal a in FIRST(  ), add A →  to.

10

LL(1) Table ConstructionLL(1) Table ConstructionLL(1) Table ConstructionLL(1) Table Construction

Production E' → +TE' causes

M[E',+] to get E' →+TE'

Page 11: Compiler Construction Sohail Aslam Lecture 19. 2 LL(1) Table Construction For each production A →  1.for each terminal a in FIRST(  ), add A →  to.

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 )

Page 12: Compiler Construction Sohail Aslam Lecture 19. 2 LL(1) Table Construction For each production A →  1.for each terminal a in FIRST(  ), add A →  to.

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' ) = { ), $ }

Page 13: Compiler Construction Sohail Aslam Lecture 19. 2 LL(1) Table Construction For each production A →  1.for each terminal a in FIRST(  ), add A →  to.

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 )

Page 14: Compiler Construction Sohail Aslam Lecture 19. 2 LL(1) Table Construction For each production A →  1.for each terminal a in FIRST(  ), add A →  to.

14

LL(1) Table ConstructionLL(1) Table ConstructionLL(1) Table ConstructionLL(1) Table Construction

And so on.

The final parsing table produced is:

Page 15: Compiler Construction Sohail Aslam Lecture 19. 2 LL(1) Table Construction For each production A →  1.for each terminal a in FIRST(  ), add A →  to.

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 )

Page 16: Compiler Construction Sohail Aslam Lecture 19. 2 LL(1) Table Construction For each production A →  1.for each terminal a in FIRST(  ), add A →  to.

16

Left FactoringLeft FactoringLeft FactoringLeft Factoring

Consider the grammar

E → T + E | TT → int | int T | (E)

Page 17: Compiler Construction Sohail Aslam Lecture 19. 2 LL(1) Table Construction For each production A →  1.for each terminal a in FIRST(  ), add A →  to.

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

Page 18: Compiler Construction Sohail Aslam Lecture 19. 2 LL(1) Table Construction For each production A →  1.for each terminal a in FIRST(  ), add A →  to.

18

Left FactoringLeft FactoringLeft FactoringLeft Factoring

E → T + E | TT → int | int T | (E)

For E, it is not clear how to predict

Page 19: Compiler Construction Sohail Aslam Lecture 19. 2 LL(1) Table Construction For each production A →  1.for each terminal a in FIRST(  ), add A →  to.

19

Left FactoringLeft FactoringLeft FactoringLeft Factoring

A grammar must be left factored before use for predictive parsing

Page 20: Compiler Construction Sohail Aslam Lecture 19. 2 LL(1) Table Construction For each production A →  1.for each terminal a in FIRST(  ), add A →  to.

20

Left FactoringLeft FactoringLeft FactoringLeft Factoring

If , replace all productionsA → 1 | 2 | .... | n |

withA → Z |

Z → 1| 2| .... | n

where Z is new nonterminal

Page 21: Compiler Construction Sohail Aslam Lecture 19. 2 LL(1) Table Construction For each production A →  1.for each terminal a in FIRST(  ), add A →  to.

21

Left FactoringLeft FactoringLeft FactoringLeft Factoring

If , replace all productionsA → 1 | 2 | .... | n |

withA → Z |

Z → 1| 2| .... | n

where Z is new nonterminal

Page 22: Compiler Construction Sohail Aslam Lecture 19. 2 LL(1) Table Construction For each production A →  1.for each terminal a in FIRST(  ), add A →  to.

22

Left FactoringLeft FactoringLeft FactoringLeft FactoringA graphical explanation

A 2

1

3

Page 23: Compiler Construction Sohail Aslam Lecture 19. 2 LL(1) Table Construction For each production A →  1.for each terminal a in FIRST(  ), add A →  to.

23

Left FactoringLeft FactoringLeft FactoringLeft Factoring

becomes

Z 2

1

3

A

Page 24: Compiler Construction Sohail Aslam Lecture 19. 2 LL(1) Table Construction For each production A →  1.for each terminal a in FIRST(  ), add A →  to.

24

Left FactoringLeft FactoringLeft FactoringLeft Factoring

Consider following fragment of expression grammarFactor → id

| id [ ExprList ] | id ( ExprList )

Page 25: Compiler Construction Sohail Aslam Lecture 19. 2 LL(1) Table Construction For each production A →  1.for each terminal a in FIRST(  ), add A →  to.

25

Left FactoringLeft FactoringLeft FactoringLeft FactoringFactor → id

| id [ ExprList ]| id ( ExprList )

FIRST(rhs1) = {id}FIRST(rhs2) = {id}FIRST(rhs3) = {id}

Page 26: Compiler Construction Sohail Aslam Lecture 19. 2 LL(1) Table Construction For each production A →  1.for each terminal a in FIRST(  ), add A →  to.

26

Left FactoringLeft FactoringLeft FactoringLeft Factoring

after left factoring

Factor → id ArgsArgs → [ ExprList ]

| ( ExprList ) |

Page 27: Compiler Construction Sohail Aslam Lecture 19. 2 LL(1) Table Construction For each production A →  1.for each terminal a in FIRST(  ), add A →  to.

27

Left FactoringLeft FactoringLeft FactoringLeft FactoringFactor → id ArgsArgs → [ ExprList ]

| ( ExprList ) |

FIRST(rhs1) = {id}FIRST(rhs2) = { [ }FIRST(rhs3) = { ( }

FIRST(rhs4) = FOLLOW(Factor)

Page 28: Compiler Construction Sohail Aslam Lecture 19. 2 LL(1) Table Construction For each production A →  1.for each terminal a in FIRST(  ), add A →  to.

28

Left FactoringLeft FactoringLeft FactoringLeft Factoring

[

(

Factor id ExprList

ExprList

]

)id

id

No basis for choice

Page 29: Compiler Construction Sohail Aslam Lecture 19. 2 LL(1) Table Construction For each production A →  1.for each terminal a in FIRST(  ), add A →  to.

29

Left FactoringLeft FactoringLeft FactoringLeft Factoring

[

(

Factor id ExprList

ExprList

]

)

correct choice possible

Page 30: Compiler Construction Sohail Aslam Lecture 19. 2 LL(1) Table Construction For each production A →  1.for each terminal a in FIRST(  ), add A →  to.

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?)

Page 31: Compiler Construction Sohail Aslam Lecture 19. 2 LL(1) Table Construction For each production A →  1.for each terminal a in FIRST(  ), add A →  to.

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


Recommended