Follow Calculation in Compiler Design
In compiler design, the FOLLOW set of a non-terminal symbol is crucial for constructing parsing tables and determining when to apply production rules. This guide explains how to calculate FOLLOW sets, provides an interactive calculator, and includes practical examples.
What is FOLLOW in Compiler Design?
The FOLLOW set of a non-terminal symbol A in a context-free grammar is the set of terminals that can appear immediately to the right of A in any sentential form derived from the start symbol. Unlike FIRST sets, which look at what can come immediately after a symbol, FOLLOW sets consider what can come after a symbol in any context.
FOLLOW sets are essential for:
- Constructing LL(1) parsing tables
- Determining when to use a production rule
- Resolving conflicts in grammar design
Note: The FOLLOW set of the start symbol always includes the end-of-input marker ($).
How to Calculate FOLLOW Sets
The FOLLOW set can be calculated using the following rules:
- Place $ in FOLLOW(S), where S is the start symbol
- If there is a production A → αBβ, then:
- FIRST(β) is added to FOLLOW(B)
- If β can derive ε (the empty string), then FOLLOW(A) is added to FOLLOW(B)
These rules are applied repeatedly until no more elements can be added to any FOLLOW set.
FOLLOW(A) = {a | S ⇒* αAaβ, a ∈ Terminals ∪ {$}}
Worked Example
Consider the following grammar:
S → aBd | bAe A → cA | ε B → bB | ε
Calculating FOLLOW sets for this grammar:
- Initialize FOLLOW(S) = {$}
- From S → aBd: FOLLOW(B) = {d}
- From S → bAe: FOLLOW(A) = {e}
- From A → cA: FOLLOW(A) = FOLLOW(A) ∪ FIRST(cA) = {e, c}
- From B → bB: FOLLOW(B) = FOLLOW(B) ∪ FIRST(bB) = {d, b}
The final FOLLOW sets are:
- FOLLOW(S) = {$}
- FOLLOW(A) = {e, c}
- FOLLOW(B) = {d, b}
FAQ
What is the difference between FIRST and FOLLOW sets?
FIRST sets contain the first terminals that can appear in strings derived from a non-terminal, while FOLLOW sets contain terminals that can appear immediately after a non-terminal in any sentential form.
When is the FOLLOW set of a non-terminal empty?
The FOLLOW set of a non-terminal is empty if the non-terminal never appears in any sentential form. This typically happens with unreachable non-terminals in the grammar.
How do I know when to stop calculating FOLLOW sets?
You stop when no more elements can be added to any FOLLOW set through repeated application of the rules. This usually happens after a few iterations through all productions.