Short trick method to find Follow of any grammar- Compiler Design

Short trick method to find Follow of any grammar- Compiler Design



play-rounded-fill play-rounded-outline play-sharp-fill play-sharp-outline
pause-sharp-outline pause-sharp-fill pause-rounded-outline pause-rounded-fill
00:00

 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, then FOLLOW(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:
    css
    S → A B

    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:
    css
    S → A B

    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:
    css
    S → A B (where FIRST(B) contains ε)

    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:

less
SA B
Aa | ε
Bb | C
Cc | ε

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:

ruby
FOLLOW(S) = { $ }
FOLLOW(A) = { b, c }
FOLLOW(B) = { $ }
FOLLOW(C) = { $ }

 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



Leave a Reply

Your email address will not be published. Required fields are marked *

error: