FIRST and FOLLOW Sets for the Grammar , ,
We consider the grammar (with explicit alternatives):
This is a standard expression grammar with operator precedence ( binds tighter than ). To compute FIRST and FOLLOW we apply the FIRST/FOLLOW rules for LL parsing, taking care of left recursion (which is handled naturally by the fixed-point computation of these sets).
We will compute:
- FIRST FIRST FIRST
- FOLLOW FOLLOW FOLLOW
Note: The input grammar uses “/” for alternatives; we interpret it as “”.
How to think about FIRST/FOLLOW here
Because and have base productions via and , the sets stabilize quickly: FIRST is driven by what can start from , and FOLLOW is driven by where and appear inside and in the / operator contexts.
Key parsing sets we will use
- Terminals: \{id, +, *, (, ), \}$
- Start symbol:
- [End marker]{def="Special terminal indicating end of input"}: $$$
By LL convention:
- \ \in(E)$
Step 1 — Compute FIRST sets
- 1Step 1
From , the strings derived from begin with either '(' or 'id'.
- 2Step 2
Therefore .
- 3Step 3
Since exists, must include . Also does not introduce new starting terminals beyond those already possible for the leading .
- 4Step 4
So .
- 5Step 5
Because exists, includes . Again, is left-recursive and does not change the possible leading terminals.
- 6Step 6
So .
FIRST sets (final)
No production derives the empty string , so none of these FIRST sets contains .
Do not forget FOLLOW(E) contains end marker
Since is the start symbol, \ must be included in FOLLOW. This is the most common omission in manual computations.
Step 2 — Compute FOLLOW sets
- 1Step 1
Add \ to FOLLOW(E). Initialize others as empty: FOLLOW(T)=\emptyset, FOLLOW(F)=\emptyset.
- 2Step 2
In , the symbol is followed by '+' in that sentential form, so is in FOLLOW(E). Also appears at the end of the right side, so FOLLOW(T) must include FOLLOW(E).
- 3Step 3
In , is at the end, so FOLLOW(T) includes FOLLOW(E). (This confirms the propagation from the previous step.)
- 4Step 4
In , is followed by '', so '' is in FOLLOW(T). Also is at the end of the right side, so FOLLOW(F) must include FOLLOW(T).
- 5Step 5
In , is at the end, so FOLLOW(F) includes FOLLOW(T).
- 6Step 6
In , the nonterminal is followed immediately by ')', so ')' is in FOLLOW(E).
- 7Step 7
Combine all derived terminals into each FOLLOW set; since there is no , we do not need FIRST-of-suffix propagation beyond end-of-production cases.
FOLLOW sets (final)
From the rules:
- FOLLOW(E) gets (start symbol), '+' (from ), and ')' (from ).
- FOLLOW(T) gets FOLLOW(E) (because appears at the end in ) and also '+' (already in FOLLOW(E)), plus '*' (from ).
- FOLLOW(F) gets FOLLOW(T) (because appears at the end in and ).
Concretely:
\begin{aligned} \text{FOLLOW}(E) &= \\{\\$, +, )\\} \\ \text{FOLLOW}(T) &= \\{\\$, +, *, )\\} \\ \text{FOLLOW}(F) &= \\{\\$, +, *, )\\} \end{aligned}Fixed-Point Computation View (Conceptual)
Set starts
InitializationStart with FOLLOW(E)={$}, others empty; compute FIRST from base F productions."
Apply local FOLLOW rules
Propagation via productionsFrom add '+' to FOLLOW(E) and FOLLOW(E) to FOLLOW(T); from add ')' to FOLLOW(E)."
Add '*' and shift FOLLOW(T) to FOLLOW(F)
Operator contextsFrom add '*' to FOLLOW(T) and FOLLOW(T) to FOLLOW(F)."
No epsilon, sets stop changing
StabilizationBecause no is derived, FIRST-of-suffix terms don’t extend via nullable chains; sets stabilize."
Common Pitfalls and Edge Cases
Knowledge Check
Which terminal set is FIRST(F) for the grammar?