FIRST Set in Syntax Analysis (original) (raw)

Last Updated : 9 Jan, 2026

The FIRST set is used in syntax analysis to identify which terminal symbols can appear at the start of strings derived from a non-terminal. It is crucial for LL and LR parsers, helping them decide which rules to apply.

To compute the FIRST set:

  1. Add the terminal itself to the FIRST set for all terminals.
  2. For non-terminals, add the FIRST terminal from the right-hand side of their productions.
  3. Repeat until no more symbols can be added.

In LL parsers, if the next input symbol matches the FIRST set of a non-terminal, that rule can be safely applied. The FIRST set is also used in computing the FOLLOW set, which identifies what can come immediately after a non-terminal in a grammar.

Rules to Compute FIRST Set

If x is a terminal, then FIRST(x) = { ‘x’ }

If x-> ε, is a production rule, then add ε to FIRST(x).

If X->Y1 Y2 Y3….Yn is a production,

**Example 1:

Production Rules of Grammar
E -> TE’
E’ -> +T E’| ε
T -> F T’
T’ -> *F T’ | ε
F -> (E) | id

**FIRST sets
FIRST(E) = FIRST(T) = { ( , id }
FIRST(E’) = { +, ε}
FIRST(T) = FIRST(F) = { ( , id }
FIRST(T’) = { *, ε }
FIRST(F) = { ( , id }

**Example 2:

Production Rules of Grammar
S -> ACB | Cbb | Ba
A -> da | BC
B -> g | ε
C -> h | ε

**FIRST sets
FIRST(S) = FIRST(ACB) U FIRST(Cbb) U FIRST(Ba)
= { d, g, h, b, a, ε}
FIRST(A) = { d } U FIRST(BC)
= { d, g, h, ε }
FIRST(B) = { g , ε }
FIRST(C) = { h , ε }

**Notes:

  1. The grammar used above is Context-Free Grammar (CFG). Syntax of most programming languages can be specified using CFG.
  2. CFG is of the form A -> B, where A is a single Non-Terminal, and B can be a set of grammar symbols ( i.e. Terminals as well as Non-Terminals)

Features of FIRST Sets

**Advantages of Using FIRST Set in Syntax Analysis

**Disadvantages of Using FIRST Set in Syntax Analysis