Designing the FIRST and FOLLOW Sets for a Grammar

Designing the FIRST and FOLLOW Sets for a Grammar

Verified Sources
Nov 4, 2026

To design LL(1) parsing logic, you must compute two lookahead sets for each nonterminal in a context-free grammar: FIRST and [FOLLOW]{def="Set of terminals (and possibly ) that can appear immediately to the right of a nonterminal in some sentential form"}. These sets are constructed using production rules and (for many grammars) an iterative "fixed-point" procedure until they stop changing. Standard definitions treat ε as a special case in FIRST, and the end-marker [ ]{def="Input end marker used in parsing tables and FOLLOW sets"} as a special case in FOLLOW (added for the start symbol).2

Key idea:

  • FIRST focuses on “what tokens can start what this nonterminal can generate.”
  • FOLLOW focuses on “what tokens can legally follow this nonterminal in some derivation,” which is essential for resolving parsing decisions.


Learning targets (what you should be able to do)

  1. Compute FIRST(X) for every nonterminal XX.
  2. Compute [FOLLOW]{def="Set of terminals (and possibly ) that can appear immediately to the right of a nonterminal in some sentential form"}(A) for every nonterminal A$.
  3. Correctly handle ε propagation and when to stop scanning symbols in a production body.

Footnotes

  1. "FIRST set (context-free grammar) — Wikipedia" https://en.wikipedia.org/wiki/LL_parser - Defines FIRST concept and ε handling in LL parsing. ↩

  2. "FOLLOW set (context-free grammar) — Wikipedia" https://en.wikipedia.org/wiki/LL_parser - Defines FOLLOW computation, including $ and propagation rules. ↩ ↩2

FIRST and FOLLOW Sets (LL(1) Parsing) — Core Examples

Core definitions you will use

Let G=(V,T,P,S)G=(V,T,P,S) where VV are nonterminals, TT terminals, PP productions, and SS start symbol.

FIRST of a string of grammar symbols

To compute FIRST for a production body like X1X2⋯XkX_1X_2\cdots X_k, you need a helper notion: FIRST of a sequence. A standard rule is:

  • Add   t∈T\;t\in T to FIRST(X1X2⋯Xk)\text{FIRST}(X_1X_2\cdots X_k) if t∈FIRST(Xi)t\in \text{FIRST}(X_i) for the first position ii where XiX_i can derive something starting with tt.
  • If X1X_1 can derive ε, then FIRST can include what starts from X2X_2 as well; similarly continue.
  • If every symbol XiX_i in the sequence can derive ε, then ε is included in FIRST of the sequence.

This “scan left-to-right until a symbol can’t derive ε” behavior is crucial to avoid over-approximations and to make your algorithm converge correctly.

FOLLOW of a nonterminal

FOLLOW(A)\text{FOLLOW}(A) collects terminals that can appear immediately after AA in some sentential form  αAβ⇒∗αaγ \,\alpha A \beta\Rightarrow^* \alpha a \gamma\, where aa is a terminal and γ\gamma is any string of symbols.

Operationally, in LL(1) computation:

  • Add \,\, toto\text{FOLLOW}(S)$.
  • For each production A→αBβA\to \alpha B \beta, everything in FIRST(β)\text{FIRST}(\beta) (except ε) goes into FOLLOW(B)\text{FOLLOW}(B).
  • If β\beta can derive ε (or if β\beta is empty), then FOLLOW(A)\text{FOLLOW}(A) goes into FOLLOW(B)\text{FOLLOW}(B).

Footnotes

  1. "FIRST set (context-free grammar) — Wikipedia" https://en.wikipedia.org/wiki/LL_parser - Defines FIRST concept and ε handling in LL parsing. ↩ ↩2

  2. "FOLLOW set (context-free grammar) — Wikipedia" https://en.wikipedia.org/wiki/LL_parser - Defines FOLLOW computation, including $ and propagation rules. ↩ ↩2

Pro Tip: Treat ε as a “permission to continue”

When scanning a production body for FIRST/FOLLOW propagation, ε doesn’t add a terminal token. Instead, ε tells you that you should keep looking at later symbols. This prevents the common mistake of stopping too early.

Footnotes

  1. "FIRST set (context-free grammar) — Wikipedia" https://en.wikipedia.org/wiki/LL_parser - Defines FIRST concept and ε handling in LL parsing. ↩

Common Pitfall: Adding ε into FOLLOW sets

In standard LL(1) construction rules, FOLLOW sets contain terminals and $ only; ε is handled indirectly via “if the remainder can derive ε” rules. Don’t place ε directly into FOLLOW(A) when adding FIRST(β).

Footnotes

  1. "FOLLOW set (context-free grammar) — Wikipedia" https://en.wikipedia.org/wiki/LL_parser - Defines FOLLOW computation, including $ and propagation rules. ↩

Step-by-step algorithm to compute FIRST sets

  1. 1
    Step 1

    For every terminal a∈Ta\in T, set FIRST(a)={a}\text{FIRST}(a)=\{a\}. For every nonterminal A∈VA\in V, initialize FIRST(A)=∅\text{FIRST}(A)=\emptyset. If the grammar contains an explicit production A→ϵA\to \epsilon, then include ε in FIRST(A)\text{FIRST}(A) (usually as part of your iterative update logic).

    Footnotes

    1. "FIRST set (context-free grammar) — Wikipedia" https://en.wikipedia.org/wiki/LL_parser - Defines FIRST concept and ε handling in LL parsing. ↩

  2. 2
    Step 2

    For each production A→X1X2⋯XkA\to X_1X_2\cdots X_k, compute what can start strings derived from the RHS: add FIRST(X1)\text{FIRST}(X_1) excluding ε to FIRST(A)\text{FIRST}(A). If ϵ∈FIRST(X1)\epsilon\in \text{FIRST}(X_1), also add FIRST(X2)\text{FIRST}(X_2) excluding ε, and so on. Keep going while the prefix symbols can derive ε.

    Footnotes

    1. "FIRST set (context-free grammar) — Wikipedia" https://en.wikipedia.org/wiki/LL_parser - Defines FIRST concept and ε handling in LL parsing. ↩

  3. 3
    Step 3

    If ε is in FIRST of every symbol XiX_i in the RHS (equivalently, the whole RHS can derive ε), then add ε to FIRST(A)\text{FIRST}(A).

    Footnotes

    1. "FIRST set (context-free grammar) — Wikipedia" https://en.wikipedia.org/wiki/LL_parser - Defines FIRST concept and ε handling in LL parsing. ↩

  4. 4
    Step 4

    Because FIRST sets only grow (monotonic growth) and are finite over T∪{ϵ}T\cup\{\epsilon\}, you can keep looping through productions until a full pass adds nothing new (fixed point).

  5. 5
    Step 5

    Pick a few nonterminals and manually derive short strings to verify that all terminals you found in FIRST(X) can actually appear as initial tokens, and that you didn’t miss derivable terminals (a quick sanity check before using them in FOLLOW).

Step-by-step algorithm to compute FOLLOW sets

  1. 1
    Step 1

    Set \text{FOLLOW}(S)=\{\}forthestartsymbolfor the start symbolS.Initialize. Initialize \text{FOLLOW}(A)=\emptysetforallothernonterminalsfor all other nonterminalsA
    e S$.

    Footnotes

    1. "FOLLOW set (context-free grammar) — Wikipedia" https://en.wikipedia.org/wiki/LL_parser - Defines FOLLOW computation, including $ and propagation rules. ↩

  2. 2
    Step 2

    For every production A→αBβA\to \alpha B \beta, consider each occurrence of a nonterminal BB on the RHS and treat the symbols after it as β\beta.

    Footnotes

    1. "FOLLOW set (context-free grammar) — Wikipedia" https://en.wikipedia.org/wiki/LL_parser - Defines FOLLOW computation, including $ and propagation rules. ↩

  3. 3
    Step 3

    If β\beta starts with some sequence of symbols, add every terminal in FIRST(β)\text{FIRST}(\beta) except ε to FOLLOW(B)\text{FOLLOW}(B).

    Footnotes

    1. "FOLLOW set (context-free grammar) — Wikipedia" https://en.wikipedia.org/wiki/LL_parser - Defines FOLLOW computation, including $ and propagation rules. ↩

  4. 4
    Step 4

    If ϵ∈FIRST(β)\epsilon\in \text{FIRST}(\beta) (or if β\beta is empty), then add everything in FOLLOW(A)\text{FOLLOW}(A) into FOLLOW(B)\text{FOLLOW}(B).

    Footnotes

    1. "FOLLOW set (context-free grammar) — Wikipedia" https://en.wikipedia.org/wiki/LL_parser - Defines FOLLOW computation, including $ and propagation rules. ↩

  5. 5
    Step 5

    FOLLOW sets also grow monotonically and reach a fixed point after repeated passes over productions.

    Footnotes

    1. "FOLLOW set (context-free grammar) — Wikipedia" https://en.wikipedia.org/wiki/LL_parser - Defines FOLLOW computation, including $ and propagation rules. ↩

  6. 6
    Step 6

    FOLLOW computation depends on FIRST(β), so ensure FIRST sets are computed first and are stable before running FOLLOW iterations.

Visualizing the FIRST/FOLLOW propagation

Consider a production AtoalphaBbetaA\\to \\alpha B\\beta. The update behavior is:

  • From FIRST side: terminals that can begin strings from beta\\beta should be legal followers of BB.
  • From ε side: if beta\\beta can disappear (derive ε), then whatever can follow AA should also follow BB.

Rule-to-effect mapping for LL(1) FIRST/FOLLOW design

How each grammar situation contributes to set updates.

Edge cases and correctness checks

Workflow to design FIRST/FOLLOW

Parse grammar rules

1

Identify terminals/nonterminals, ε productions, and the start symbol S."

Compute FIRST sets

2

Use left-to-right scanning of RHS with ε as a continuation rule; iterate to fixed point."

Compute FOLLOW sets

3

Initialize FOLLOW(S) with $; propagate FIRST(β){ε} and FOLLOW(A) when β⇒*ε; iterate to fixed point."

Use results for LL(1) table checks

4

FIRST/FOLLOW guide which production to place under each lookahead terminal."

FIRST & FOLLOW micro-deck

1 / 5
Question · Term

What does FIRST(A) contain for a nonterminal A?

Click to reveal
Answer · Definition

Terminals that can begin some string derived from A; includes ε if A can derive ε.

Footnotes

  1. "FIRST set (context-free grammar) — Wikipedia" https://en.wikipedia.org/wiki/LL_parser - Defines FIRST concept and ε handling in LL parsing. ↩

Knowledge Check

Question 1 of 4
Q1Single choice

In the standard LL(1) FOLLOW update rule for a production A → α B β, what is added to FOLLOW(B) from FIRST(β)?