CS Atlas GATE 2027
TOC course
0 / 91 complete

TOC/Phase 3 · CFG, PDA & Context-Free Languages

Lesson 3.2.1 · Item 51 of 91

Removing useless symbols, ε- and unit productions

Read at your own pace. Try the questions before opening the answers.

On this page

Recall warm-up

  1. What proves a grammar ambiguous?
    Stuck? Peek at 3.1.3 Ambiguity
    • Ambiguous: some w has ≥ 2 parse trees (≡ ≥ 2 leftmost derivations).
    • Witnesses: E+E/E*E with id+id*id; SS with three pieces; aS|Sa with aa; dangling else.
    • Fix with precedence layers E → E+T | T, T → T*F | F, F → (E) | id.
    • Inherently ambiguous language: every grammar ambiguous, e.g. aⁱbʲcᵏ with i=j or j=k. No algorithm decides ambiguity in general.

    Read lesson 3.1.3

  2. CFG for balanced parentheses?
    Stuck? Peek at 3.1.2 Writing CFGs for standard languages
    • Wrap: S → aSb | ε (aⁿbⁿ); ratios S → aSbb.
    • Nested: outer wrap → inner wrap. Concatenation: S → XY. Union: S → S₁ | S₂. Star: S → S₁S | ε.
    • Palindromes S → aSa | bSb | a | b | ε. Balanced S → SS | (S) | ε. #a = #b: S → aSbS | bSaS | ε.
    • i ≠ j: S → AC | CB, C → aCb | ε, A → aA | a, B → bB | b.

    Read lesson 3.1.2

  3. How do you test whether a DFA’s language is empty?
    Stuck? Peek at 2.4.2 Algorithms for regular languages
    • Membership: run. Emptiness: reachable F? Finiteness: trim + cycle (or lengths n..2n−1). Universality: complement + emptiness. Equivalence: minimize & compare, or symmetric difference empty. Containment: L₁ ∩ L̅₂ = ∅.
    • All these have always-terminating algorithms for regular languages.

    Read lesson 2.4.2

The Hook

A grammar written by people collects junk: variables that can never finish into a terminal string, variables that are never reached from S, rules like A → ε that complicate algorithms, and chains A → B → C that do no work. Before converting to a normal form, we clean all of it out without changing the language (except possibly ε).

Predict First

S → AB | a, A → aA, B → b. Can A ever produce a terminal string? What happens to S → AB?

Core Concept

Prediction check

No — A → aA never ends, so A is non-generating. Any rule using A is useless: S → AB goes. Then B is no longer reachable from S and goes too. Result: S → a.

steps illustration
Order matters: non-generating first, then unreachable.

Useless symbols

A symbol X is useful if S ⇒* αXβ ⇒* w for some terminal string w — it is both generating (X ⇒* some terminal string) and reachable (S ⇒* …X…). Otherwise it is useless.
  1. Find generating symbols: terminals generate; A generates if some rule A → α has only generating symbols in α. Repeat until stable. Delete the rest.
  2. Find reachable symbols from S (graph reachability, 0.2.5). Delete the rest.

Doing step 2 before step 1 can leave useless symbols behind.

Computing generating symbols grows a set until it stops.
Computing generating symbols grows a set until it stops.

ε-productions

A variable is nullable if A ⇒* ε. Find nullable variables (A → ε, or A → B₁…Bₖ with all Bᵢ nullable). Then for every rule, add versions with every subset of nullable occurrences removed (but don’t add A → ε). Finally delete all ε-rules. The new grammar generates L − {ε}; if ε ∈ L, add S₀ → S | ε with a new start.

Example: S → aSb | ε. S is nullable. S → aSb gives S → aSb | ab. Result generates aⁿbⁿ, n ≥ 1.

original rulenullable occurrencesnew rules
S → ASBA, S, B all nullableASB, SB, AB, AS, A, S, B (drop ε)
S → aSbSaSb, ab
A → aAAaA, a
Every subset of nullable occurrences may vanish (except making the whole body empty).

Unit productions

A unit production is A → B (one variable). Find all unit pairs (A, B) with A ⇒* B using only unit rules. For each pair and each non-unit rule B → α, add A → α. Then delete all unit rules.

Example: E → T | E + T, T → F | T * F, F → (E) | id. Unit pairs include (E,T), (E,F), (T,F); E gets T’s and F’s non-unit bodies.

E
unit
T
unit
F
copy
(E) | id
non-unit bodies
Shortcut chains: E inherits every non-unit body reachable through unit steps.

Safe order

  1. Remove ε-productions.
  2. Remove unit productions.
  3. Remove useless symbols (generating, then reachable).

This order never re-introduces something already removed.

flowplay illustration
The cleanup pipeline.

Putting it together

  • Useless = non-generating or unreachable; remove in that order.
  • ε-removal adds versions with nullable symbols dropped; unit removal copies bodies along unit chains.
  • Cleanup order: ε, unit, useless.

Visual

before:  S → AB | a,  A → aA,  B → b
generating: {a, b, B, S}      A ✗  → delete S → AB and A → aA
reachable from S:  {S, a}     B ✗  → delete B → b
after:   S → a                 L = {a} unchanged

Worked Examples

Easy

Remove useless symbols: S → aS | A | C, A → a, B → aa, C → aCb.

Show the full solution

Generating: A, B (and S via A). C → aCb never ends ⇒ C non-generating; delete S → C and C. Reachable from S: S, A. B unreachable ⇒ delete. Result: S → aS | A, A → a.

Medium

Remove ε-productions: S → AB, A → aA | ε, B → bB | ε.

Show the full solution

Nullable: A, B, S. S → AB | A | B; A → aA | a; B → bB | b. Since ε ∈ L, add S₀ → S | ε. L = a*b* unchanged.

GATE-level

Remove unit productions: S → A | bb, A → B | b, B → S | a.

Show the full solution

Unit pairs: from S: S, A, B; from A: A, B, S; from B: B, S, A — all three reach each other. Non-unit bodies: bb (S), b (A), a (B). Result: S → bb | b | a, A → bb | b | a, B → bb | b | a. Then A, B are unreachable from S ⇒ S → bb | b | a.

GATE Traps

  • Remove non-generating symbols before unreachable ones.
  • Removing ε-rules removes ε from the language; add S₀ → S | ε if ε was in it.
  • When removing ε-rules, consider every subset of nullable occurrences, not just one at a time.
  • Unit rules can form cycles (S → A → B → S); compute unit pairs by reachability.

Memory Hook

Spring-cleaning a grammar: throw out empty boxes (ε), shortcut hallways (units), then rooms nobody can enter or that lead nowhere (useless).

Connect the Dots

A clean grammar is ready for a strict format. Next: Chomsky Normal Form, where every rule is A → BC or A → a — and every derivation of a length-n string takes exactly 2n − 1 steps.

Self-Test

  1. MCQ

    In which order should useless symbols be removed?

    1. unreachable, then non-generating
    2. non-generating, then unreachable
    3. either order
    4. only non-generating
  2. MSQ

    Grammar: S → AB | a, A → BC | ε, B → b | ε, C → c. Which variables are nullable?

    1. S
    2. A
    3. B
    4. C
  3. NAT

    After removing ε-rules from S → aSbS | ε (not adding ε back), how many S-rules are there?

    Write your answer before opening the key.

  4. MCQ

    A unit production is:

    1. A → a
    2. A → B
    3. A → ε
    4. A → AB

Rule Card

  • Useful = generating + reachable. Remove non-generating first, then unreachable.
  • Nullable: A ⇒* ε. Add all versions dropping nullable occurrences; delete ε-rules; add S₀ → S | ε if needed.
  • Unit pairs by reachability over unit rules; copy non-unit bodies; delete unit rules.
  • Order: ε → unit → useless.

Concept Ledger

  • useless symbol
  • generating symbol
  • reachable symbol
  • nullable variable
  • ε-production
  • unit production
  • unit pair

Answer Vault

Reveal answers when you’re ready

Warm-up answers

  1. One string with two parse trees (two leftmost derivations).
  2. S → SS | (S) | ε.
  3. Check whether an accepting state is reachable.

Self-test answers

  1. B — The other order may leave useless symbols.
    • A: Can miss some.
    • C: Not safe.
    • D: Unreachable ones must go too.
  2. ABC — A → ε, B → ε, S → AB with both nullable.
    • D: C → c only.
  3. 4 — aSbS, abS, aSb, ab.
  4. B — One variable on the right.
    • A: Terminal rule.
    • C: ε-rule.
    • D: Two variables.

Flashcards

  1. Which useless-symbol step comes first?
    Reveal answerRemove non-generating symbols, then unreachable ones.
  2. Safe simplification order?
    Reveal answerε-productions, unit productions, useless symbols.
  3. What is a nullable variable?
    Reveal answerA ⇒* ε.

Ready for the next idea?

Mark this item complete when you’ve finished reading and working through it.