The Wayback Machine - https://web.archive.org/web/20261119005020/https://www.geeksforgeeks.org/compiler-design/why-first-and-follow-in-compiler-design/

FIRST and FOLLOW in Compiler Design

Last Updated : 16 Nov, 2026

FIRST and FOLLOW are two sets used to help parsers understand how to process a grammar.

  • FIRST Set: The FIRST set of a non-terminal contains all terminal symbols that can appear at the beginning of strings derived from it (including ε if applicable).
  • FOLLOW Set: The FOLLOW set of a non-terminal contains all terminal symbols that can appear immediately to its right in any sentential form (including $ for the start symbol).

FIRST (X)

The FIRST(X) set is a collection of terminal symbols (and possibly ε) that can appear as the leftmost symbol when we expand X in a derivation of a given grammar.

1. When X is a terminal

If X is a terminal symbol (for example, 'a' or 'b'), the FIRST(X) set simply contains X itself, since a terminal always starts with itself.

Example: If X = a, then FIRST(a) = {a}.

2. When X has only one production rule of type X → aY

If a non-terminal has a production that begins with a terminal symbol, then the FIRST set contains that terminal symbol.

Example:

A → a B

Here, the FIRST set of A contains the first terminal symbol on the right-hand side.

FIRST(A) = { a }

3. When X has multiple production rules

If X is a non-terminal (for example, A or B), the FIRST(X) set includes all the terminal symbols that could be the first symbol of any string derived from X. This means, for each production rule of X, you look at what can be the first symbol of that rule.

Example:

A → a B
A → b
A → ε

Here, FIRST(A) = {a, b, ε} because:

  • The first symbol in the first production A → a B is 'a'.
  • The first symbol in the second production A → b is 'b'.
  • The third production A → ε means A can derive an empty string, so ε is included.

4. When X is a string of non-terminals

If X is a sequence of symbols (such as A B C), calculate FIRST(X) by checking the symbols from left to right.

  • Add all elements of FIRST(A), excluding ε, to FIRST(X).
  • If A can derive ε, add all elements of FIRST(B), excluding ε.
  • If both A and B can derive ε, also consider FIRST(C).
  • Continue checking subsequent symbols only if all preceding symbols can derive ε.
  • Include ε in FIRST(X) only if all symbols in the sequence can derive ε.

Example:

X → A B C
A → a | ε
B → b | ε
C → c | d

The FIRST sets of the individual non-terminals are:

  • FIRST(A) = {a, ε}
  • FIRST(B) = {b, ε}
  • FIRST(C) = {c, d}

Therefore:

FIRST(ABC) = {a, b, c, d}

FOLLOW (X)

The FOLLOW(X) set contains all the terminal symbols that can appear immediately after the non-terminal X in any valid string derived from the grammar. It is used in parsing to decide what symbols can follow a particular non-terminal in a derivation.

1. When X is the start symbol of the grammar

If X is the start symbol (e.g., S), the FOLLOW(S) set always includes the special end-of-input marker $ to indicate that nothing comes after the start symbol in a complete string.

Example:

Start Symbol = S
FOLLOW(S) = { $ }

2. When X appears before a terminal

If X is followed by a terminal symbol in a production rule, that terminal symbol is added to FOLLOW(X).

Example:

A → a B c

Here, B is followed by c, so c is in FOLLOW(B).

3. When X is followed by a sequence of symbols

For a production A → αXβ, add FIRST(β) − {ε} to FOLLOW(X).

  • If β can derive ε, also add FOLLOW(A) to FOLLOW(X).
  • If β is empty (i.e., X appears at the end of the production), add FOLLOW(A) to FOLLOW(X).

Example:

A → aXB
B → b | ε

Since FIRST(B) = {b, ε}:

  • Add b to FOLLOW(X).
  • Since B can derive ε, also add FOLLOW(A) to FOLLOW(X).

Therefore, FOLLOW(X) = {b} ∪ FOLLOW(A).

Note: ε is never included in a FOLLOW set because FOLLOW contains only terminal symbols and the end-of-input marker ($). If a symbol following a non-terminal can derive ε, add the FOLLOW set of the left-hand-side non-terminal instead.

Importance of FIRST and FOLLOW Set

The FIRST and FOLLOW sets play a crucial role in LL(1) parsing and grammar analysis, with multiple important applications:

Building LL(1) Parsing Tables

  • FIRST and FOLLOW sets help create parsing tables used by LL(1) parsers. These tables guide the parser in selecting the correct production based on the next input symbol.
  • Without these sets, it would be impossible to systematically and deterministically decide which rule to apply.

Checking LL(1) Grammar

  • FIRST and FOLLOW sets help identify conflicts while constructing an LL(1) parsing table.
  • A grammar satisfies the LL(1) condition if each parsing table entry contains at most one production.
  • Every LL(1) grammar is unambiguous and can be parsed using predictive parsing.

Handling ε-Productions

  • The FIRST set includes ε (empty string) when a non-terminal can derive ε.
  • The FOLLOW set ensures that after ε, the parser knows which symbols can legally follow.

Predictive Parsing

  • These sets allow a parser to predict the correct production to use without backtracking.
  • This makes LL(1) parsers efficient, as they don’t need to retry multiple rules.

Error Detection and Recovery

  • The FOLLOW set helps the parser identify errors in the input string by showing what symbols are expected at a given point.
  • This aids in implementing error recovery mechanisms during parsing.

Compiler Design and Syntax Analysis

  • Both sets are foundational in compiler design, enabling systematic analysis of context-free grammars.
  • They are used to construct LL(1) parsing tables and enable predictive parsing without backtracking.

Related Links:
Quiz on Syntax Analysis
Program to calculate FIRST and FOLLOW sets of given grammar

Comment

Explore