in simple language with definitions, rules, examples, steps, and exam tips.
In syntax analysis (LL(1) parsing), we need to decide which grammar rule to use.
👉 For that, we compute:
These help in building predictive parsing tables.
The FIRST set of a symbol is the set of all terminals that can appear as the first symbol in some string derived from it.
FIRST(X)
If X is a terminal:
FIRST(X) = {X}
If:
A → α
Then:
If:
A → ε
Then:
ε ∈ FIRST(A)
If:
A → X Y Z
Then:
Grammar:
E → T E'
E' → + T E' | ε
T → id
FIRST(id) = {id}
FIRST(T) = {id}
FIRST(E') = {+, ε}
FIRST(E) = {id}
The FOLLOW set of a non-terminal is the set of terminals that can appear immediately after it in a derivation.
FOLLOW(A)
For start symbol S:
$ ∈ FOLLOW(S)
($ = end of input marker)
Then:
FIRST(β) - ε ⊆ FOLLOW(B)
Then:
FOLLOW(A) ⊆ FOLLOW(B)
Then also:
FOLLOW(A) ⊆ FOLLOW(B)
Grammar:
E → T E'
E' → + T E' | ε
T → id
Step-by-step:
FOLLOW(E) = {$}
FOLLOW(E') = {$}
FOLLOW(T) = {+, $}
$| Feature | FIRST Set | FOLLOW Set |
|---|---|---|
| Meaning | First terminal in derivation | What follows a non-terminal |
| Applies to | Terminal & Non-terminal | Only Non-terminal |
| Includes ε | Yes | No |
| Used for | Starting rule selection | Parsing decision making |
| Input direction | Left side | Right side |
Grammar:
E → T E'
E' → + T E' | ε
T → id
FIRST(E) = {id}
FIRST(E') = {+, ε}
FIRST(T) = {id}
FOLLOW(E) = {$}
FOLLOW(E') = {$}
FOLLOW(T) = {+, $}
They are used to build:
$ in FOLLOW of start symbol👉 Frequently asked:
$| Aspect | FIRST Set | FOLLOW Set |
|---|---|---|
| Definition | First terminal in derivation | Next possible terminals |
| Symbol Type | Terminal / Non-terminal | Non-terminal only |
| ε Handling | Included | Not included |
| Start Symbol | Not special | Contains $ |
| Use | Parsing decisions | Parsing table creation |
| Importance | Very High | Very High |
Open this section to load past papers