FIRST and FOLLOW sets in Compiler Design - Time & Space Complexity
Analyzing time complexity helps us understand how the work to compute FIRST and FOLLOW sets grows as the grammar size increases.
We want to know how the number of steps changes when the grammar has more rules or symbols.
Analyze the time complexity of the following pseudocode for computing FIRST sets.
initialize FIRST(t) = {t} for each terminal t; FIRST(A) = {} for each nonterminal A
repeat
for each production A -> α in grammar
nullable = true
for each symbol X in α from left to right
add FIRST(X) \ {ε} to FIRST(A)
if ε not in FIRST(X)
nullable = false
break
if nullable
add ε to FIRST(A)
until no changes in any FIRST set
This code computes FIRST sets by repeatedly updating them until no new symbols can be added.
Look at what repeats in the code:
- Primary operation: Looping over all productions and their right-hand side symbols.
- How many times: The whole process repeats until no FIRST set changes, which can happen multiple times.
As the number of productions and symbols grows, the number of checks and updates grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 productions | About 100 to 200 operations |
| 100 productions | Thousands of operations |
| 1000 productions | Hundreds of thousands of operations |
Pattern observation: The work grows quickly as grammar size increases, because each update can cause many more checks.
Time Complexity: O(n^3)
This means the time to compute FIRST sets grows roughly with the cube of the grammar size.
[X] Wrong: "Computing FIRST sets is always quick and linear in grammar size."
[OK] Correct: Because the sets update repeatedly and depend on each other, the process can take many passes, making it grow faster than just the number of rules.
Understanding how FIRST and FOLLOW sets scale helps you explain parser design clearly and shows you can analyze algorithms beyond just writing code.
"What if we used a grammar with no epsilon (empty) productions? How would the time complexity change?"
Practice
FIRST set of a non-terminal in a grammar represent?Solution
Step 1: Understand the definition of FIRST set
The FIRST set of a non-terminal includes all terminals that can appear at the beginning of any string derived from that non-terminal.Step 2: Differentiate from FOLLOW set
FOLLOW sets represent terminals that can appear immediately after the non-terminal, not at the start.Final Answer:
The set of terminals that can appear at the start of strings derived from that non-terminal -> Option DQuick Check:
FIRST set = starting terminals [OK]
- Confusing FIRST with FOLLOW sets
- Thinking FIRST includes all terminals in grammar
- Mixing terminals and non-terminals in FIRST
A in a grammar?Solution
Step 1: Understand FOLLOW set definition
FOLLOW(A) contains terminals that can appear immediately after A in some sentential form.Step 2: Consider special case for end of productions
If A appears at the end of a production, FOLLOW of the production's left side non-terminal is included in FOLLOW(A).Final Answer:
Include terminals that can appear immediately after A in any production, plus FOLLOW of the start symbol if A can be at the end -> Option CQuick Check:
FOLLOW set = terminals after A [OK]
- Confusing FOLLOW with FIRST sets
- Ignoring FOLLOW of start symbol when non-terminal is at production end
- Including terminals before the non-terminal
S -> A B
A -> a | ε
B -> b
What is the
FIRST set of B?Solution
Step 1: Analyze productions of B
B produces only terminal 'b', so FIRST(B) includes 'b'.Step 2: Check for epsilon in B
There is no epsilon (empty string) production for B, so ε is not included.Final Answer:
{b} -> Option AQuick Check:
FIRST(B) = {b} [OK]
- Including ε when no epsilon production exists
- Mixing FIRST sets of A and B
- Adding terminals from other productions incorrectly
E -> T E' E' -> + T E' | ε T -> int
Which mistake is present if FOLLOW(E') is computed as {+} only?
Solution
Step 1: Identify where E' appears
E' appears at the end of production E -> T E'.Step 2: Apply FOLLOW rule for end non-terminal
Since E' is at the end, FOLLOW(E) must be included in FOLLOW(E'). Ignoring this causes incomplete FOLLOW set.Final Answer:
Ignoring that E' can be at the end of production, so FOLLOW(E) should be included -> Option AQuick Check:
FOLLOW of end non-terminal includes FOLLOW of left side [OK]
- Forgetting to add FOLLOW of left side non-terminal
- Mixing FIRST and FOLLOW sets
- Assuming FOLLOW only includes terminals directly after
S -> A B C A -> a | ε B -> b | ε C -> c
What is the
FIRST set of S?Solution
Step 1: Compute FIRST sets of A, B, and C
FIRST(A) = {a, ε}, FIRST(B) = {b, ε}, FIRST(C) = {c}.Step 2: Calculate FIRST(S) considering ε in A and B
Since A and B can be ε, FIRST(S) includes FIRST(A) without ε, FIRST(B) without ε, and FIRST(C). So FIRST(S) = {a, b, c}.Final Answer:
{a, b, c} -> Option BQuick Check:
FIRST(S) = {a, b, c} considering ε in A and B [OK]
- Including ε in FIRST(S) incorrectly
- Ignoring ε in A or B when computing FIRST(S)
- Only taking FIRST(A) and ignoring B and C
Ready to prove it?
Pass the test and this pattern moves unknown
