You are on page 1of 31

FIRST and

FOLLOW
Robb T.
Koether

Left Factoring
FIRST and FOLLOW
Table-Driven
LL Parsing Lecture 8
Nullability
The FIRST Function Section 4.4
The FOLLOW
Function

Assignment

Robb T. Koether

Hampden-Sydney College

Mon, Feb 9, 2009


Outline

FIRST and
FOLLOW
Robb T.
Koether

Left Factoring
1 Left Factoring
Table-Driven
LL Parsing
Nullability
The FIRST Function 2 Table-Driven LL Parsing
The FOLLOW
Function Nullability
Assignment
The FIRST Function
The FOLLOW Function

3 Assignment
Left Factoring

FIRST and
FOLLOW
Robb T.
Koether

Left Factoring

Table-Driven
LL Parsing
A problem occurs when two productions for the same
Nullability nonterminal begin with the same token.
The FIRST Function
The FOLLOW
Function We cannot decide which production to use.
Assignment
This is not necessarily a problem since we could
process the part they have in common, then make a
decision based on what follows.
Left Factoring

FIRST and
FOLLOW
Robb T.
Koether
Consider the grammar
Left Factoring

Table-Driven
LL Parsing
A → αβ | αγ
Nullability
The FIRST Function
The FOLLOW
Function
We use left factorization to transform it into the form
Assignment
A → αA0
A0 → β | γ.

Now we can apply the productions immediately and


unambiguously.
Example

FIRST and
FOLLOW
Robb T.
Koether

Left Factoring Example (Left Factoring)


Table-Driven
LL Parsing
In the earlier example, we had the productions
Nullability
The FIRST Function
The FOLLOW
Function
C → id == num | id != num | id < num
Assignment
To perform left factoring, introduce a nonterminal C0 :

C → id C0
C0 → == num | != num | < num
Example

FIRST and
FOLLOW
Robb T.
Koether
Example (Left Factoring)
Left Factoring

Table-Driven
Consider the grammar of if statements.
LL Parsing
Nullability
The FIRST Function S → if C then S else S
The FOLLOW
Function
| if C then S
Assignment

We rewrite it as

S → if C then SS0
S0 → else S | ε
Table-Driven LL Parsing

FIRST and
FOLLOW
Robb T.
Koether

Left Factoring

Table-Driven
LL Parsing
Nullability To build the parsing table, we need three concepts:
The FIRST Function
The FOLLOW
Function
Nullability
Assignment
The FIRST function
The FOLLOW function
Nullability

FIRST and
FOLLOW
Robb T.
Koether

Left Factoring

Table-Driven
LL Parsing
Nullability Definition (Nullable)
The FIRST Function
The FOLLOW
Function
A nonterminal A is nullable if
Assignment
A ⇒∗ ε.
Nullability

FIRST and
FOLLOW
Robb T.
Koether
Clearly, A is nullable if it has a production
Left Factoring

Table-Driven
LL Parsing A → ε.
Nullability
The FIRST Function
The FOLLOW
Function
But A is also nullable if there are, for example,
Assignment productions

A → BC
B → A | aC | ε
C → aB | Cb | ε
Nullability

FIRST and
FOLLOW
Robb T.
Koether

Left Factoring
In other words, A is nullable if there is a production
Table-Driven
LL Parsing
Nullability
The FIRST Function
A → ε,
The FOLLOW
Function

Assignment
or there is a production

A → B1 B2 . . . Bn ,

where B1 , B2 , . . . , Bn are nullable.


Example

FIRST and
FOLLOW
Robb T.
Koether Example (Nullability)
Left Factoring In the grammar
Table-Driven
LL Parsing
Nullability
E → T E0
E0 → + T E0 | ε
The FIRST Function
The FOLLOW
Function

Assignment T → F T0
T0 → * F T0 | ε
F → (E) | id | num

E0 and T 0 are nullable.


E, T, and F are not nullable.
Example

FIRST and
FOLLOW
Robb T.
Koether

Left Factoring
Example (Nullability)
Table-Driven
LL Parsing
Nullability
Nonterminal Nullable
The FIRST Function
The FOLLOW E No
Function

Assignment
E0 Yes
T No
T0 Yes
F No
FIRST and FOLLOW

FIRST and
FOLLOW
Robb T.
Koether
Definition (FIRST)
Left Factoring
FIRST(α) is the set of all terminals that may appear as the
Table-Driven
LL Parsing first symbol in a replacement string of α.
Nullability
The FIRST Function
The FOLLOW
Function
Definition (FOLLOW)
Assignment
FOLLOW(α) is the set of all terminals that may follow α in a
derivation.

Given a grammar G, we may define the functions


FIRST and FOLLOW on the strings of symbols of G.
FIRST

FIRST and
FOLLOW
Robb T.
Koether
For a grammar symbol X, FIRST(X) is computed as
Left Factoring
follows.
Table-Driven
LL Parsing For every terminal X, FIRST(X) = {X}.
Nullability
The FIRST Function
For every nonterminal X, if
The FOLLOW
Function
X → Y1 Y2 . . . Yn
Assignment

is a production, then
FIRST(Y1 ) ⊆ FIRST(X).
Furthermore, if Y1 , Y2 , . . . , Yk are nullable, then

FIRST(Yk+1 ) ⊆ FIRST(X).
FIRST

FIRST and
FOLLOW
Robb T.
Koether

Left Factoring

Table-Driven
LL Parsing We are concerned with FIRST(X) only for the
Nullability
The FIRST Function nonterminals of the grammar.
The FOLLOW
Function
FIRST(X) for terminals is trivial.
Assignment
According to the definition, to determine FIRST(A), we
must inspect all productions that have A on the left.
Example

FIRST and
FOLLOW
Robb T.
Koether

Left Factoring Example (FIRST)


Table-Driven
LL Parsing
Let the grammar be
Nullability

E → T E0
The FIRST Function
The FOLLOW
Function

Assignment E0 → + T E0 | ε
T → F T0
T0 → * F T0 | ε
F → (E) | id | num
Example

FIRST and
FOLLOW
Robb T.
Koether

Left Factoring Example (FIRST)


Table-Driven
LL Parsing
Find FIRST(E).
Nullability E occurs on the left in only one production
The FIRST Function
The FOLLOW
Function
E → T E0
Assignment

Therefore, FIRST(T) ⊆ FIRST(E).


Furthermore, T is not nullable.
Therefore, FIRST(E) = FIRST(T).
We have yet to determine FIRST(T).
Example

FIRST and
FOLLOW
Robb T.
Koether Example (FIRST)
Left Factoring Find FIRST(T).
Table-Driven T occurs on the left in only one production
LL Parsing
Nullability
The FIRST Function
The FOLLOW
T → F T0
Function

Assignment Therefore,
FIRST(F) ⊆ FIRST(T).
Furthermore, F is not nullable.
Therefore,
FIRST(T) = FIRST(F).
We have yet to determine FIRST(F).
Example

FIRST and
FOLLOW
Robb T.
Koether

Left Factoring

Table-Driven Example (FIRST)


LL Parsing
Nullability
The FIRST Function
Find FIRST(F).
The FOLLOW
Function FIRST(F) = {(, id, num}.
Assignment Therefore,
FIRST(E) = {(, id, num}.
FIRST(T) = {(, id, num}.
Example

FIRST and
FOLLOW
Robb T.
Koether

Left Factoring

Table-Driven
LL Parsing Example (FIRST)
Nullability
The FIRST Function Find FIRST(E0 ).
The FOLLOW
Function
FIRST(E0 ) = {+}.
Assignment
Find FIRST(T 0 ).
FIRST(T 0 ) = {*}.
Example

FIRST and
FOLLOW
Robb T.
Koether

Left Factoring
Example (FIRST)
Table-Driven
LL Parsing
Nullability
Nonterminal Nullable FIRST
The FIRST Function
The FOLLOW E No {(, id, num}
Function

Assignment
E0 Yes {+}
T No {(, id, num}
T0 Yes {*}
F No {(, id, num}
FOLLOW

FIRST and
FOLLOW
Robb T.
Koether
For a grammar symbol X, FOLLOW(X) is defined as
Left Factoring
follows.
Table-Driven
LL Parsing If S is the start symbol, then $ ∈ FOLLOW(S).
Nullability
The FIRST Function If A → αBβ is a production, then
The FOLLOW
Function

Assignment
FIRST(β) ⊆ FOLLOW(B).

If A → αB is a production, or A → αBβ is a production


and β is nullable, then

FOLLOW(A) ⊆ FOLLOW(B).
FOLLOW

FIRST and
FOLLOW
Robb T.
Koether

Left Factoring

Table-Driven
LL Parsing
Nullability We are concerned about FOLLOW(X) only for the
The FIRST Function
The FOLLOW nonterminals of the grammar.
Function

Assignment According to the definition, to determine FOLLOW(A),


we must inspect all productions that have A on the right.
Example

FIRST and
FOLLOW
Robb T.
Koether

Left Factoring Example (FOLLOW)


Table-Driven
LL Parsing
Let the grammar be
Nullability

E → T E0
The FIRST Function
The FOLLOW
Function

Assignment E0 → + T E0 | ε
T → F T0
T0 → * F T0 | ε
F → (E) | id | num
Example

FIRST and
FOLLOW
Robb T.
Koether
Example (FOLLOW)
Left Factoring
Find FOLLOW(E).
Table-Driven
LL Parsing E is the start symbol, therefore
Nullability
The FIRST Function
The FOLLOW
Function
$ ∈ FOLLOW(E).
Assignment
E occurs on the right in only one production.

F → (E)

Therefore
FOLLOW(E) = {$, )}.
Example

FIRST and
FOLLOW
Robb T.
Koether

Left Factoring
Example (FOLLOW)
Table-Driven
LL Parsing
Find FOLLOW(E0 ).
Nullability E0 occurs on the right in two productions.
The FIRST Function
The FOLLOW
Function
E → T E0
Assignment
E0 → + T E0

Therefore,

FOLLOW(E0 ) = FOLLOW(E) = {$, )}.


Example

FIRST and
FOLLOW
Robb T.
Koether
Example (FOLLOW)
Left Factoring
Find FOLLOW(T).
Table-Driven
LL Parsing T occurs on the right in two productions.
Nullability

→ T E0
The FIRST Function
The FOLLOW E
Function
0
Assignment
E → + T E0

Therefore, FOLLOW(T) ⊇ FIRST(E0 ) = {+}.


However, E0 is nullable, therefore it also contains
FOLLOW(E) = {$, )} and FOLLOW(E0 ) = {$, )}.
Therefore,
FOLLOW(T) = {+, $, )}.
Example

FIRST and
FOLLOW
Robb T.
Koether

Left Factoring
Example (FOLLOW)
Table-Driven
LL Parsing
Find FOLLOW(T 0 ).
Nullability T 0 occurs on the right in two productions.
The FIRST Function
The FOLLOW
Function
T → F T0
Assignment
T0 → * F T0

Therefore,

FOLLOW(T 0 ) = FOLLOW(T) = {$, ), +}.


Example

FIRST and
FOLLOW
Example (FOLLOW)
Robb T.
Koether
Find FOLLOW(F).
Left Factoring F occurs on the right in two productions.
Table-Driven
LL Parsing
Nullability
T → F T0
0
The FIRST Function
The FOLLOW
T → * F T 0.
Function

Assignment Therefore,

FOLLOW(F) ⊇ FIRST(T 0 ) = {*}.

However, T 0 is nullable, therefore it also contains


FOLLOW(T) = {+, $, )} and FOLLOW(T 0 ) = {$, ), +}.
Therefore,

FOLLOW(F) = {*, $, ), +}.


Example

FIRST and
FOLLOW
Robb T.
Koether

Left Factoring
Example (FOLLOW)
Table-Driven
LL Parsing
Nullability
Nonterminal Nullable FIRST FOLLOW
The FIRST Function
The FOLLOW E No {(, id, num} {$, )}
Function

Assignment
E0 Yes {+} {$, )}
T No {(, id, num} {$, ), +}
T0 Yes {*} {$, ), +}
F No {(, id, num} {*, $, ), +}
Assignment

FIRST and
FOLLOW
Robb T.
Koether
Homework
Left Factoring

Table-Driven
The grammar
LL Parsing
Nullability
The FIRST Function R → R ∪ R | RR | R* | (R) | a | b
The FOLLOW
Function

Assignment generates all regular expressions on the alphabet


Σ = {a, b}.
Using the result of the exercise from the previous
lecture, find FIRST(X) and FOLLOW(X) for each
nonterminal X in the grammar.

You might also like