LL(1) Parsing
FIRST and FOLLOW (Syntax Analysis)
Section titled βFIRST and FOLLOW (Syntax Analysis)β-
Used in Top-Down Parsing
-
Essential for LL(1) grammar
-
Helps in predictive parsing== ==table construction
Grammar Basics
- Terminal (T): tokens (
id,+,*,(,), etc.) - Non-terminal (NT): variables (
E,T,F, etc.) -
Ξ΅ (epsilon): empty string
-
$: input end marker
- Grammar:
A β Ξ±
FIRST Set
-
FIRST(X) = set of terminals== that can ==appear as the first symbol== of any ==string derived from X
Rules to Compute FIRST β
-
If
Xis a terminalFIRST(X) = {X} -
If
X β ΡΡ β FIRST(X) -
If
X β Y1 Y2 ... Ykβ- Add
FIRST(Y1) β {Ξ΅}toFIRST(X) - If
Ξ΅ β FIRST(Y1), then addFIRST(Y2) β {Ξ΅} - Continue until:
- Ξ΅ not found, or
- All
Yicontain Ξ΅ β then add Ξ΅ to FIRST(X)
- Add
Key Points
-
FIRST is about starting terminals
- Ξ΅ only included if whole RHS can derive Ξ΅
FOLLOW Set
-
FOLLOW(A) = set of terminals== that can ==appear immediately after A in some sentential form
Rules to Compute FOLLOW β
-
If
Ais start symbol$ β FOLLOW(A) -
For production
X β Ξ± A Ξ²FIRST(Ξ²) β {Ξ΅} β FOLLOW(A) -
If
X β Ξ± AORX β Ξ± A Ξ²andΞ΅ β FIRST(Ξ²)FOLLOW(X) β FOLLOW(A)
Key Points
-
FOLLOW depends on context
- FOLLOW never contains Ξ΅
Algorithm (GATE-Oriented)
- Initialize all FIRST and FOLLOW as empty
-
Apply FIRST rules== until ==no change
-
Apply FOLLOW rules== iteratively until ==fixed point
Example β
Grammar:
E β T E'E' β + T E' | Ξ΅T β F T'T' β * F T' | Ξ΅F β ( E ) | idFIRST:
FIRST(E) = { (, id }FIRST(E') = { +, Ξ΅ }FIRST(T) = { (, id }FIRST(T') = { *, Ξ΅ }FIRST(F) = { (, id }FOLLOW:
FOLLOW(E) = { ), $ }FOLLOW(E') = { ), $ }FOLLOW(T) = { +, ), $ }FOLLOW(T') = { +, ), $ }FOLLOW(F) = { *, +, ), $ }Note: Xβ² (X prime) is a new non-terminal introduced to remove left recursion and make the grammar suitable for LL(1) parsing
FIRST of String β
For string Ξ± = X1 X2 ... Xn
- FIRST(Ξ±) computed same as RHS rule
- Used directly in parsing table
LL(1) Grammar Condition
Grammar is LL(1) iff for every non-terminal A:
-
For
A β Ξ± | Ξ²FIRST(Ξ±) β© FIRST(Ξ²) = β -
If
Ξ΅ β FIRST(Ξ±)FIRST(Ξ²) β© FOLLOW(A) = β
FOLLOW vs LFOLLOW vs RFOLLOW β
FOLLOW
- FOLLOW(A) = set of terminals that can appear immediately after non-terminal A in some sentential form
- Used in LL(1) parsing
$ β FOLLOW(start symbol)- Ξ΅ never appears in FOLLOW
- Example:
S β A bA β a | Ξ΅FOLLOW(S) = { $ }FOLLOW(A) = { b }LFOLLOW (Left FOLLOW)
- LFOLLOW(A) = terminals that can appear immediately to the left of A
- Used in operator precedence / LR parsing concepts
- Not used in LL(1)
- Example:
S β a ALFOLLOW(A) = { a }RFOLLOW (Right FOLLOW)
- RFOLLOW(A) = terminals that can appear immediately to the right of A
- Practically same as FOLLOW(A) in most compiler texts
- Emphasizes right context
- Example:
S β A bRFOLLOW(A) = { b }- FOLLOW β LL(1), predictive parsing
- LFOLLOW / RFOLLOW β theoretical, LR / precedence discussions
FOLLOW usually means RFOLLOW** β
S β ABA β aB β bFOLLOW(A) = {b} because, S β AB β Ab β ab
LFOLLOW(A) = { } because, S β AB β aB β ab
RFOLLOW(B) = {b} because, S β AB β Ab β abPredictive Parsing Table Rule
Section titled βPredictive Parsing Table RuleβFor production A β Ξ±:
-
For each
a β FIRST(Ξ±)M[A, a] = A β Ξ± -
If
Ξ΅ β FIRST(Ξ±)For each b β FOLLOW(A), M[A, b] = A β Ξ±
Common GATE Traps
- Mixing FIRST and FOLLOW rules
- Forgetting
$in FOLLOW(start symbol) - Incorrect Ξ΅ propagation
- FOLLOW depends on LHS, not RHS only
One-Line Memory
- FIRST β what can start
- FOLLOW β what can follow
- LL(1) β no conflict between them
Predictive Parsing Table
Section titled βPredictive Parsing Tableβ- LL(1) Predictive Parsing Table
- Used in Top-Down Parsing
- Built using FIRST and FOLLOW
- Decides which production to apply using (Non-terminal, Input symbol)
Structure of Table
- Rows β Non-terminals
- Columns β Terminals +
$ - Cell Entry β Grammar production
- Empty cell β error
Steps to Construct Parsing Table
Step 1: Compute FIRST
-
FIRST of RHS** tells which terminal can start
- If Ξ΅ β FIRST(RHS), mark it
Step 2: Compute FOLLOW
-
FOLLOW of LHS** tells what can appear next
- Add
$to FOLLOW(start symbol)
Step 3: Fill Table
For each production A β Ξ±
- For every terminal
a β FIRST(Ξ±)anda β Ξ΅
M[A, a] = A β Ξ±- If
Ξ΅ β FIRST(Ξ±)
For every b β FOLLOW(A)M[A, b] = A β Ξ±- Remaining cells β error
Example
S β a A | b B | Ξ΅A β SB β S | Ξ΅FIRST Sets
FIRST(S) = { a, b, Ξ΅ }FIRST(A) = { a, b, Ξ΅ }FIRST(B) = { a, b, Ξ΅ }FOLLOW Sets
FOLLOW(S) = { $ }FOLLOW(A) = { $ }FOLLOW(B) = { $ }Predictive Parsing Table
| a | b | $ | |
|---|---|---|---|
| S | S β aA | S β bB | S β Ξ΅ |
| A | A β S | A β S | error |
| B | B β S | B β S | B β Ξ΅ |
How Each Entry Came (Logic)
Row S
a β FIRST(a)βS β ab β FIRST(b)βS β bΞ΅ β FIRST(S)and$ β FOLLOW(S)βS β Ξ΅
Row A
A β SFIRST(S) β {Ξ΅} = {a, b}β entries underaandb-
Even though Ξ΅ β FIRST(S), FOLLOW(A) is not used here because grammar would cause conflict (LL(1) violation if added)
Note: Ξ΅ β FIRST(RHS) does NOT automatically mean entry under
$.$column is filled only using FOLLOW(LHS) of the same production Always check which non-terminal owns Ξ΅
Row B
B β Sgives entries undera, bB β Ξ΅and$ β FOLLOW(B)β entry under$
LL(1) Check (GATE Rule)
Grammar is LL(1) iff:
- No cell has more than one entry
- FIRSTβFIRST and FIRSTβFOLLOW conflicts absent
One-Line Memory
- FIRST fills terminals
- FOLLOW fills
$for Ξ΅-productions - Multiple entries β not LL(1)