. If X is a terminal symbol then First(X) = {X}
. If X
ε is a production then ε is in First(X)
. If X is a non terminal
and X
Y 1 Y 2 2 . Yk is a production
then
if for some i, a is in First(Yi )
and ε is in all of First(Yj ) (such that j<i)
then a is in First(X)
. If ε is in First (Y1 ) . First(Yk ) then ε is in First(X)
To compute FIRST ( X ) for all grammar symbols X, apply the following rules until no more terminals or e can be added to any FIRST set.
1. If X is terminal, then First (X) is {X}.
2. If X
ε is a production then add e to FIRST(X).
3. If X is a non terminal and X
Y 1 Yk .........Y k is a production, then place a in First (X) if for some i, a is in FIRST(Yi ) and e is in all of FIRST(Y 1 ), FIRST(Y 2 ),.., FIRST(Yi-1 );that is, Y1 ..Y i-1
* ε . If ε is in FIRST(Yj ) for all i = 1,2,..,k, then add ε to FIRST(X). For example, everything in FIRST(Y1 ) is surely in FIRST(X). If Y 1 does not derive ε , then we add nothing more to FIRST(X), but if Y1
* ε , then we add FIRST(Y2 ) and so on.