
in simple language with definitions, rules, examples, steps, and exam tips .
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 view and download past papers
Click on Show Past Papers to load available exam papers.