FIRST and FOLLOW in Compiler Design (original) (raw)

Last Updated : 11 Apr, 2026

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

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.

**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}.

**When X has only one production rule of type XaY:

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 }

**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:

**When X is a string of non-terminals:

If X is a string made up of terminals and/or non-terminals (like A B C), we start with the leftmost symbol and use its FIRST set.

To compute FIRST(X), you look at FIRST(A). If A can derive ε (i.e., the empty string), then you also look at FIRST(B), and so on. If both A and B can derive ε, you also need to consider FIRST(C).

Example:

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

The FIRST(X) is {a,b,c,d}.

This is because:

Read more about FIRST Set in Syntax Analysis.

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.

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) = { $ }

**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).

**When X appears before a non-terminal:

If **X is followed by a non-terminal (e.g., B), the FIRST(B) set is added to FOLLOW(X). However, if B can derive ε (empty string), then

Example:

A → a X B
B → b | ε

If FIRST(B) = {b, ε}, then:

**When X appears at the end of a production rule:

If X appears at the end of a production rule (e.g., A → B X), then FOLLOW(A) is added to FOLLOW(X) because whatever follows A in the string must also follow X.

Example:

A → B X

Here, everything in FOLLOW(A) must also be included in FOLLOW(X).

Read more about FOLLOW Set in Syntax Analysis.

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

**Ensuring Grammar is LL(1)

**Handling ε-Productions

**Predictive Parsing

**Error Detection and Recovery

**Compiler Design and Syntax Analysis

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