Short trick method to find Follow of any grammar- Compiler Design
Short trick method to find Follow of any grammar- Compiler Design
Contents [hide]
- 0.0.1 Short Trick to Find FOLLOW of Any Grammar in Compiler Design
- 0.0.2 Step 1: Start with the Start Symbol
- 0.0.3 Step 2: Look at the Right-Hand Side of Productions
- 0.0.4 Step 3: If a Non-Terminal is at the End of a Production
- 0.0.5 Step 4: If a Nullable Symbol is Present
- 0.0.6 Shortcut Formula for FOLLOW
- 0.0.7 Example for Quick Understanding
- 0.0.8 Grammar:
- 0.0.9 Step-by-Step Trick to Find FOLLOW
- 0.0.10 Final FOLLOW Sets:
- 0.0.11 Conclusion:
- 0.0.12 Short trick method to find Follow of any grammar- Compiler Design
- 0.0.13 Introduction to Compilers and Language Design
- 0.0.14 LECTURE NOTES ON COMPILER DESIGN
- 0.0.15 FIRST and FOLLOW
- 1
Shortcut Method to Find FOLLOW Set in Compiler Design
Short Trick to Find FOLLOW of Any Grammar in Compiler Design
In Compiler Design, the FOLLOW set of a non-terminal helps in parsing and syntax analysis. It determines the set of terminals that can appear immediately after a given non-terminal in the derivation.
Finding the FOLLOW set can be tricky, but here’s a simple and easy trick to compute it quickly!
Steps to Find FOLLOW Set Using a Short Trick
Step 1: Start with the Start Symbol
- Always add
$
(EOF symbol) to the FOLLOW of the start symbol. - Example: If
S
is the start symbol, thenFOLLOW(S) = {$}
.
Step 2: Look at the Right-Hand Side of Productions
- If a non-terminal (A) appears in a production before a terminal or non-terminal, then FOLLOW(A) will contain FIRST of the next symbol.
- Example:
Here,
FOLLOW(A) ⊇ FIRST(B)
.
Step 3: If a Non-Terminal is at the End of a Production
- If
A
is at the end of a production, then FOLLOW(A) ⊇ FOLLOW of LHS. - Example:
Since
B
is at the end,FOLLOW(B) ⊇ FOLLOW(S)
.
Step 4: If a Nullable Symbol is Present
- If a nullable symbol (ε-production) is after
A
, then check the next symbol’s FOLLOW or LHS’s FOLLOW. - Example:
Then,
FOLLOW(A) ⊇ FOLLOW(S)
.
Shortcut Formula for FOLLOW
Case | Rule to Follow |
---|---|
A → αBβ |
FOLLOW(B) ⊇ FIRST(β) (excluding ε) |
A → αB (B is at the end) |
FOLLOW(B) ⊇ FOLLOW(A) |
A → αBβ , and FIRST(β) contains ε |
FOLLOW(B) ⊇ FOLLOW(A) |
Example for Quick Understanding
Grammar:
Step-by-Step Trick to Find FOLLOW
FOLLOW(S) = {$}
(Start symbol rule)FOLLOW(A) ⊇ FIRST(B) = {b, c}
FOLLOW(B) ⊇ FOLLOW(S) = {$}
FOLLOW(C) ⊇ FOLLOW(B) = {$}
(Since C → ε
)
Final FOLLOW Sets:
Conclusion:
With this short trick, you can quickly find FOLLOW sets without confusion! Start with
$
for the start symbol. Use FIRST of next symbol for FOLLOW.
If at the end, take FOLLOW of LHS.
Handle ε-productions carefully.
Would you like me to explain with more examples?
Short trick method to find Follow of any grammar- Compiler Design
Introduction to Compilers and Language Design
LECTURE NOTES ON COMPILER DESIGN
FIRST and FOLLOW
Sure! Here’s a short-trick method to find the FOLLOW set of any non-terminal in Compiler Design (used in parsing, especially LL(1) parsers):
Shortcut Method to Find FOLLOW Set in Compiler Design
What is the FOLLOW set?
The FOLLOW(A) of a non-terminal A is the set of terminals that can appear immediately after A in some “sentential form” (derivation).
Short Trick to Find FOLLOW(A):
Step-by-Step Shortcut (Simple Rules):
Rule 1: Start Symbol Rule
If A is the start symbol, then add $
(end marker) to FOLLOW(A).
![]()
If S is start → FOLLOW(S) = { $ }
Rule 2: A → αBβ Rule (Non-terminal followed by symbols)
If there is a production like:
A → α B β
Then, everything in FIRST(β) (except ε) is added to FOLLOW(B).
FOLLOW(B) += FIRST(β) – { ε }
Rule 3: A → αB or A → αBβ where β ⇒ ε (Nullable)
If B is at the end, or β ⇒ ε, then
FOLLOW(B) += FOLLOW(A)
This is called “FOLLOW through FOLLOW”
(when nothing follows B, or what follows is nullable)
Rule 4: Keep Applying Until Stable
Apply all above rules iteratively until no new symbols are added.
Example Using Shortcut:
Let’s take a simple grammar:
S → A B
A → a | ε
B → b
Step-by-Step:
➤ FIRST Sets:
- FIRST(A) = { a, ε }
- FIRST(B) = { b }
FOLLOW(S):
- S is start symbol → FOLLOW(S) = {
$
}
FOLLOW(A):
S → A B
→ A is followed by B → so:
FOLLOW(A) += FIRST(B) = { b }
FOLLOW(B):
S → A B → B is at end → FOLLOW(B) += FOLLOW(S) = { $
}
Final FOLLOW Sets:
Non-terminal | FOLLOW |
---|---|
S | { $ } |
A | { b } |
B | { $ } |
Pro Tips:
- Always calculate FIRST sets first — they are used in FOLLOW computation.
- Don’t forget to handle ε (epsilon) carefully.
- Keep applying rules until sets stop changing (fixpoint).
Would you like:
PDF cheat sheet of FOLLOW & FIRST shortcuts?
Animated YouTube script for this?
Practice problems with solutions?
Let me know what you need next!