Docsity

Table Construction, Left Factoring - Compiler Construction - Lecture Slides, Slides of Compiler Construction

Partial preview of the text

Download Table Construction, Left Factoring - Compiler Construction - Lecture Slides and more Slides Compiler Construction in PDF only on Docsity!

Compiler

Construction

Lecture 19

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

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

LL(1) Table Construction

(2 cont. )

If ε is in FIRST(α), and $ is in

FOLLOW( A ), add A → α to

M[ A ,$].

7

LL(1) Table Construction

Let us apply the algorithm to the expression grammar.

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'

10

LL(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 )

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 Construction

And so on.

The final parsing table produced is:

16

Left Factoring

Consider the grammar E → T + E | T T → int | int∗ T | (E)

17

Left Factoring

E → T + E | T

T → int | int∗ T | (E)

Impossible to predict because for T,

two productions start with int

19

Left Factoring

A grammar must be left factored before use for predictive parsing

20

Left Factoring

If α ≠ ε, replace all productions A → αβ

| αβ

| .... | αβ

n

| γ with A → α Z | γ Z → β

| β

| .... | β

n

where Z is new nonterminal