FIRST and FOLLOW Sets for the Grammar E→E+T/TE \rightarrow E+T/T, T→T∗F/FT \rightarrow T*F/F, F→(E)/idF \rightarrow (E)/id

FIRST and FOLLOW Sets for the Grammar E→E+T/TE \rightarrow E+T/T, T→T∗F/FT \rightarrow T*F/F, F→(E)/idF \rightarrow (E)/id

Verified Sources
Nov 4, 2026

We consider the grammar (with explicit alternatives):

E→E+T∣TT→T∗F∣FF→(E)∣id\begin{aligned} E &\rightarrow E + T \mid T \\ T &\rightarrow T * F \mid F \\ F &\rightarrow (E) \mid id \end{aligned}

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(E),(E), FIRST(T),(T), FIRST(F)(F)
  • FOLLOW(E),(E), FOLLOW(T),(T), FOLLOW(F)(F)

Note: The input grammar uses “/” for alternatives; we interpret it as “∣\mid”.

How to think about FIRST/FOLLOW here

Because EE and TT have base productions via E→TE\to T and T→FT\to F, the sets stabilize quickly: FIRST is driven by what can start from FF, and FOLLOW is driven by where EE and TT appear inside (E)(E) and in the ++/ ∗* operator contexts.

Key parsing sets we will use

  • Terminals: \{id, +, *, (, ), \}$
  • Start symbol: EE
  • [End marker]{def="Special terminal \\ indicating end of input"}: $$$

By LL convention:

  • \ \in[FOLLOW]def="Setofterminalsthatcanappearimmediatelytotherightofanonterminalinsomesententialform" FOLLOW(E)$

Step 1 — Compute FIRST sets

  1. 1
    Step 1

    From F→(E)∣idF \to (E) \mid id, the strings derived from FF begin with either '(' or 'id'.

  2. 2
    Step 2

    Therefore FIRST(F)={(,id}\text{FIRST}(F) = \{(, id\}.

  3. 3
    Step 3

    Since T→FT \to F exists, FIRST(T)\text{FIRST}(T) must include FIRST(F)\text{FIRST}(F). Also T→T∗FT \to T*F does not introduce new starting terminals beyond those already possible for the leading TT.

  4. 4
    Step 4

    So FIRST(T)={(,id}\text{FIRST}(T) = \{(, id\}.

  5. 5
    Step 5

    Because E→TE \to T exists, FIRST(E)\text{FIRST}(E) includes FIRST(T)\text{FIRST}(T). Again, E→E+TE \to E+T is left-recursive and does not change the possible leading terminals.

  6. 6
    Step 6

    So FIRST(E)={(,id}\text{FIRST}(E) = \{(, id\}.

FIRST sets (final)

FIRST(E)={(,id}FIRST(T)={(,id}FIRST(F)={(,id}\begin{aligned} \text{FIRST}(E) &= \{(, id\} \\ \text{FIRST}(T) &= \{(, id\} \\ \text{FIRST}(F) &= \{(, id\} \end{aligned}

No production derives the empty string varepsilon\\varepsilon, so none of these FIRST sets contains varepsilon\\varepsilon.

Do not forget FOLLOW(E) contains end marker

Since EE is the start symbol, \ must be included in FOLLOW(E)(E). This is the most common omission in manual computations.

Step 2 — Compute FOLLOW sets

  1. 1
    Step 1

    Add \ to FOLLOW(E). Initialize others as empty: FOLLOW(T)=\emptyset, FOLLOW(F)=\emptyset.

  2. 2
    Step 2

    In E→E+TE \to E + T, the symbol EE is followed by '+' in that sentential form, so ++ is in FOLLOW(E). Also TT appears at the end of the right side, so FOLLOW(T) must include FOLLOW(E).

  3. 3
    Step 3

    In E→TE \to T, TT is at the end, so FOLLOW(T) includes FOLLOW(E). (This confirms the propagation from the previous step.)

  4. 4
    Step 4

    In T→T∗FT \to T * F, TT is followed by '', so '' is in FOLLOW(T). Also FF is at the end of the right side, so FOLLOW(F) must include FOLLOW(T).

  5. 5
    Step 5

    In T→FT \to F, FF is at the end, so FOLLOW(F) includes FOLLOW(T).

  6. 6
    Step 6

    In F→(E)F \to (E), the nonterminal EE is followed immediately by ')', so ')' is in FOLLOW(E).

  7. 7
    Step 7

    Combine all derived terminals into each FOLLOW set; since there is no ε\varepsilon, 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 EtoE+TE \\to E+T), and ')' (from Fto(E)F \\to (E)).
  • FOLLOW(T) gets FOLLOW(E) (because TT appears at the end in EtoTE \\to T) and also '+' (already in FOLLOW(E)), plus '*' (from TtoT∗FT \\to T*F).
  • FOLLOW(F) gets FOLLOW(T) (because FF appears at the end in TtoFT \\to F and TtoT∗FT \\to T*F).

Concretely:

\begin{aligned} \text{FOLLOW}(E) &= \\{\\$, +, )\\} \\ \text{FOLLOW}(T) &= \\{\\$, +, *, )\\} \\ \text{FOLLOW}(F) &= \\{\\$, +, *, )\\} \end{aligned}

Fixed-Point Computation View (Conceptual)

Set starts

Initialization

Start with FOLLOW(E)={$}, others empty; compute FIRST from base F productions."

Apply local FOLLOW rules

Propagation via productions

From E→E+TE\to E+T add '+' to FOLLOW(E) and FOLLOW(E) to FOLLOW(T); from F→(E)F\to (E) add ')' to FOLLOW(E)."

Add '*' and shift FOLLOW(T) to FOLLOW(F)

Operator contexts

From T→T∗FT\to T*F add '*' to FOLLOW(T) and FOLLOW(T) to FOLLOW(F)."

No epsilon, sets stop changing

Stabilization

Because no ε\varepsilon is derived, FIRST-of-suffix terms don’t extend via nullable chains; sets stabilize."

Common Pitfalls and Edge Cases

Knowledge Check

Question 1 of 4
Q1Single choice

Which terminal set is FIRST(F) for the grammar?