Calculating First and Follow Functions
Calculating First and Follow Functions
Definitions
1. First(X): The set of terminals that can appear at the beginning of strings
derived from XXX.
o For a terminal XXX, First(X)={X}\text{First}(X) = \{ X \}First(X)={X}.
2. If X → ε, then ε ∈ First(X).
3. For a production X → Y1 Y2 ... Yk:
o Add First(Y1) (except ε) to First(X).
2. First(A):
o A → aA ⇒ Add a to First(A).
o A → ε ⇒ Add ε to First(A).
3. First(B):
o B → bB ⇒ Add b to First(B).
o B → ε ⇒ Add ε to First(B).
4. First(S):
o S → AB
o Since A can derive ε, also add First(B). So, First(S) = {a, b, ε}.
2. Follow(A):
3. Follow(B):
o S → AB: Add Follow(S) to Follow(B).
Follow(S) = {$}.
o So, Follow(B) = {$}.
Final Results:
First(S) = {a, b, ε}
First(A) = {a, ε}
First(B) = {b, ε}
Follow(S) = {$}
Follow(A) = {b, $}
Follow(B) = {$}