Computing Follow(N)
- If a grammar contains the rule N
1 M R
2,
then Follow(M) must contain the first symbols that might appear in strings
derived from R. In addition, if R derives the empty string, then the first symbols
that might be derived from
2 will be in Follow(M).
- The computation of the Follow set will depend on several other sub-computations.
- nullable
- Given a grammar G, we say that a non-terminal N is
nullable if N
.
- first set
- Given a grammar G and

(Vn U Vt)*
we define First(
) to be the set of terminals that
might appear as the first symbol in a string derived from
. First(
) will include
if
. Thus,
First(
) = { a
Vt |
a
, for some 
( Vn U Vt )* }
U {
if
}
- The set of nullable non-terminals can be computed by the following
algorithm:
- Set "nullable" equal to the set of non-terminals appearing
on the left side of productions of the form N
.
- Until doing so adds no new non-terminals to "nullable", examine
each production in the grammar adding to "nullable" all
left-hand-sides of productions whose right-hand-side consist
entirely of symbols in "nullable".
- Given that we have computed the set of nullable non-terminal,
we can compute First for each terminal and non-terminal using a
"run until nothing changes" algorithm:
- We use a table called `FirstSet' with one entry for each
terminal and non-terminal.
Throughout the algorithm, for any x
(Vt U Vn)
First( x )
FirstSet[ x ]
- FirstSet will be initialized as follows.
- Set FirstSet[ x ] to {
} for all nullable
non-terminals and to { } for all other non-terminals.
- Set FirstSet[ x ] equal to { x } for all terminals.
- For each production of the form N
t
add
t to FirstSet[ N ].
- For each production of the form N
,
write
as
= 
'
where
is a string of nullable non-terminals,
and
' is either the
or a string of terminals
and non-terminals beginning with a non-nullable symbol we will
call M.
- The approximations for the First sets stored in the FirstSet
table are then improved until they become exact by repeating
the following process until no further changes occur.
- Consider how to determine nullable, First and Follow for the grammar:
- All the non-terminals are nullable:
- B and C are directly nullable.
- The production A
B C, then implies
A is nullable.
- Then, the production S
A B C implies S is
nullable.
- The productions break down as:
- The FirstSet values start out as shown in the table below and
can be refined by iterating over the production table.
- The computation of Follow(N) depends of the First sets defined earlier.
If
M
N 
then Follow(N) must contains First(
) . If
is nullable, then Follow(N) must also
contain Follow(M). These observations are enough to give us
an approximation algorithm for computing Follow. See the books
on reserve for details.
Computer Science 434
Department of Computer Science
Williams College