Example.
S -> Ab | Bc
A -> Df | CA
B -> gA | e
C -> dC | c
D -> h | i
First compute all of the firsts.
First(S) = First(A)UFirst(B); ({h, i, d, c}U{g, e} ={h, i, d, c, g, e}
First(A) = First(D)UFirst(C); (= {h, i}U{d, c} = {h, i, d, c})
First(B) = {g, e}
First(C) = {d, c}
First (D) = {h, i}
Start at the start symbol (in this case S)
$ is in the follow of S. S does not appear on the right, so
Follow(S) = {$} (in most of our grammars Follow for start will be {$}
Compute Follow(A)
Step 1. Where does A appear on the right-hand side of a production?
1) S -> Ab
2) A -> CA
3) B -> gA
From 1) we know b is in Follow(A)
2 tells us the Follow of A is in itself (we ignor all cases with A on left
3. tells us that the Follow(B) is in the Follow(A), but we don't know the Follow(B)=> recursive call.
Follow(B)
1) S -> Bc
Follow(B) = {c}
Follow (A) = {b, c}
Follow (B) is known
Find Follow(C);
1. A -> C A => C contains First(A) -e = {h, i, d, c} no e in First(A) A is not in A-> e , So A does not contain Follow(A).
2. C -> dC => no help
Follow(C) = {h, i, d, c}
Find Follow(D)
A -> Df so
Follow(D) = {f}
E. Example 2
E -> TQ
Q -> +TQ | -TQ | e
T -> FR
R -> *FR | /FR | e
F -> (E) | i
Compute Firsts.
First(E) = First(T) = First(F) = { (, i}
First(R) = {*, /, e }
First(Q) = { +, - ,e }
Compute Follows:
Follow(E) contains {$} and from F -> (E) it contains ). {$, )}
Follow(Q) = Follow(E) why?
Follow(T)
1. E -> TQ
2. Q -> + TQ
3. Q -> - TQ
All indicate Follow(T) contains First(Q) - e = {+, -}
Q -> e indicates that Follow(T) also contains Follow(E) and Follow(Q)
Follow(T) = {+, -, ), $}
Follow (R) = Follow(T)
Follow(F)
T -> FR
R -> *FR | /FR | e
Follow(F) = {+, -, *, /, ), $)