Short trick method to find Follow of any grammar- Compiler Design
Short trick method to find Follow of any grammar- Compiler Design
Contents [hide]
- 1 Short Trick to Find FOLLOW of Any Grammar in Compiler Design
- 2 Step 1: Start with the Start Symbol
- 3 Step 2: Look at the Right-Hand Side of Productions
- 4 Step 3: If a Non-Terminal is at the End of a Production
- 5 Step 4: If a Nullable Symbol is Present
- 6 Shortcut Formula for FOLLOW
- 7 Example for Quick Understanding
- 8 Grammar:
- 9 Step-by-Step Trick to Find FOLLOW
- 10 Final FOLLOW Sets:
- 11 Conclusion:
- 12 Short trick method to find Follow of any grammar- Compiler Design
- 13 Introduction to Compilers and Language Design
- 14 LECTURE NOTES ON COMPILER DESIGN
- 15 FIRST and FOLLOW
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?