Bird
Raised Fist0
Compiler Designknowledge~5 mins

FIRST and FOLLOW sets in Compiler Design - Time & Space Complexity

Choose your learning style10 modes available
Time Complexity: FIRST and FOLLOW sets
O(n^3)
Understanding Time 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.

Scenario Under Consideration

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.

Identify Repeating Operations

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.
How Execution Grows With Input

As the number of productions and symbols grows, the number of checks and updates grows too.

Input Size (n)Approx. Operations
10 productionsAbout 100 to 200 operations
100 productionsThousands of operations
1000 productionsHundreds of thousands of operations

Pattern observation: The work grows quickly as grammar size increases, because each update can cause many more checks.

Final Time Complexity

Time Complexity: O(n^3)

This means the time to compute FIRST sets grows roughly with the cube of the grammar size.

Common Mistake

[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.

Interview Connect

Understanding how FIRST and FOLLOW sets scale helps you explain parser design clearly and shows you can analyze algorithms beyond just writing code.

Self-Check

"What if we used a grammar with no epsilon (empty) productions? How would the time complexity change?"

Practice

(1/5)
1. What does the FIRST set of a non-terminal in a grammar represent?
easy
A. The set of all non-terminals reachable from that non-terminal
B. The set of terminals that can appear immediately after that non-terminal
C. The set of all terminals in the grammar
D. The set of terminals that can appear at the start of strings derived from that non-terminal

Solution

  1. 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.
  2. Step 2: Differentiate from FOLLOW set

    FOLLOW sets represent terminals that can appear immediately after the non-terminal, not at the start.
  3. Final Answer:

    The set of terminals that can appear at the start of strings derived from that non-terminal -> Option D
  4. Quick Check:

    FIRST set = starting terminals [OK]
Hint: FIRST = start terminals of derivations [OK]
Common Mistakes:
  • Confusing FIRST with FOLLOW sets
  • Thinking FIRST includes all terminals in grammar
  • Mixing terminals and non-terminals in FIRST
2. Which of the following is the correct way to compute the FOLLOW set of a non-terminal A in a grammar?
easy
A. Include all terminals that can start strings derived from A
B. Include all non-terminals reachable from A
C. Include terminals that can appear immediately after A in any production, plus FOLLOW of the start symbol if A can be at the end
D. Include only the terminals that appear before A in productions

Solution

  1. Step 1: Understand FOLLOW set definition

    FOLLOW(A) contains terminals that can appear immediately after A in some sentential form.
  2. 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).
  3. 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 C
  4. Quick Check:

    FOLLOW set = terminals after A [OK]
Hint: FOLLOW = terminals after non-terminal in productions [OK]
Common Mistakes:
  • Confusing FOLLOW with FIRST sets
  • Ignoring FOLLOW of start symbol when non-terminal is at production end
  • Including terminals before the non-terminal
3. Given the grammar:
S -> A B
A -> a | ε
B -> b

What is the FIRST set of B?
medium
A. {b}
B. {a, b}
C. {ε}
D. {a}

Solution

  1. Step 1: Analyze productions of B

    B produces only terminal 'b', so FIRST(B) includes 'b'.
  2. Step 2: Check for epsilon in B

    There is no epsilon (empty string) production for B, so ε is not included.
  3. Final Answer:

    {b} -> Option A
  4. Quick Check:

    FIRST(B) = {b} [OK]
Hint: FIRST of terminal-only production is that terminal [OK]
Common Mistakes:
  • Including ε when no epsilon production exists
  • Mixing FIRST sets of A and B
  • Adding terminals from other productions incorrectly
4. Consider the grammar:
E -> T E'
E' -> + T E' | ε
T -> int

Which mistake is present if FOLLOW(E') is computed as {+} only?
medium
A. Ignoring that E' can be at the end of production, so FOLLOW(E) should be included
B. Including terminals from FIRST sets instead of FOLLOW sets
C. Adding terminals that never appear after E'
D. Confusing E' with T in FOLLOW computation

Solution

  1. Step 1: Identify where E' appears

    E' appears at the end of production E -> T E'.
  2. 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.
  3. Final Answer:

    Ignoring that E' can be at the end of production, so FOLLOW(E) should be included -> Option A
  4. Quick Check:

    FOLLOW of end non-terminal includes FOLLOW of left side [OK]
Hint: Add FOLLOW of left side if non-terminal is at production end [OK]
Common Mistakes:
  • Forgetting to add FOLLOW of left side non-terminal
  • Mixing FIRST and FOLLOW sets
  • Assuming FOLLOW only includes terminals directly after
5. Given the grammar:
S -> A B C
A -> a | ε
B -> b | ε
C -> c

What is the FIRST set of S?
hard
A. {a, b, c, ε}
B. {a, b, c}
C. {a, b, ε}
D. {a, b}

Solution

  1. Step 1: Compute FIRST sets of A, B, and C

    FIRST(A) = {a, ε}, FIRST(B) = {b, ε}, FIRST(C) = {c}.
  2. 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}.
  3. Final Answer:

    {a, b, c} -> Option B
  4. Quick Check:

    FIRST(S) = {a, b, c} considering ε in A and B [OK]
Hint: Include FIRST of next symbol if previous can be ε [OK]
Common Mistakes:
  • Including ε in FIRST(S) incorrectly
  • Ignoring ε in A or B when computing FIRST(S)
  • Only taking FIRST(A) and ignoring B and C
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard