Bird
Raised Fist0
Compiler Designknowledge~20 mins

FIRST and FOLLOW sets in Compiler Design - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
FIRST and FOLLOW Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding the FIRST set

Given the grammar rule: A → aB | ε, what is the FIRST(A) set?

A{ ε }
B{ 'a' }
C{ 'B', 'a' }
D{ 'a', ε }
Attempts:
2 left
💡 Hint

Recall that FIRST set includes terminals that begin strings derived from the non-terminal, including ε if it can produce empty.

📋 Factual
intermediate
2:00remaining
FOLLOW set definition

Which of the following best describes the FOLLOW set of a non-terminal in a grammar?

AThe set of terminals that can appear immediately to the right of the non-terminal in some sentential form.
BThe set of terminals that can appear at the start of strings derived from the non-terminal.
CThe set of terminals that never appear after the non-terminal.
DThe set of non-terminals that can replace the given non-terminal.
Attempts:
2 left
💡 Hint

Think about what symbols can follow a non-terminal in the grammar rules.

🔍 Analysis
advanced
2:00remaining
Calculating FIRST set with recursion

Consider the grammar:
S → AB
A → a | ε
B → b
What is the FIRST(S) set?

A{ 'a', 'b' }
B{ 'a' }
C{ 'a', ε }
D{ 'b' }
Attempts:
2 left
💡 Hint

Remember that if FIRST(A) contains ε, then FIRST(S) includes FIRST(B) as well.

❓ Comparison
advanced
2:00remaining
Difference between FIRST and FOLLOW sets

Which statement correctly contrasts the FIRST and FOLLOW sets?

AFIRST set contains non-terminals; FOLLOW set contains terminals.
BFIRST set contains terminals that appear after a non-terminal; FOLLOW set contains terminals that begin strings from the non-terminal.
CFIRST set contains terminals that begin strings from a non-terminal; FOLLOW set contains terminals that can appear immediately after the non-terminal.
DFIRST set is used only in lexical analysis; FOLLOW set is used only in syntax analysis.
Attempts:
2 left
💡 Hint

Think about where each set is used in parsing and what they represent.

❓ Reasoning
expert
2:00remaining
FOLLOW set and epsilon productions impact

Given the grammar:
E → T E'
E' → + T E' | ε
T → int
What is the FOLLOW(E') set?

A{ '+', '$' }
B{ '$' }
C{ 'int' }
D{ '+' }
Attempts:
2 left
💡 Hint

Consider where E' appears and what can follow E in the grammar, including end of input.

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