Recall warm-up
- 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.
- 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.
- 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.
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.
Useless symbols
- Find generating symbols: terminals generate; A generates if some rule A → α has only generating symbols in α. Repeat until stable. Delete the rest.
- Find reachable symbols from S (graph reachability, 0.2.5). Delete the rest.
Doing step 2 before step 1 can leave useless symbols behind.
ε-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 rule | nullable occurrences | new rules |
|---|---|---|
| S → ASB | A, S, B all nullable | ASB, SB, AB, AS, A, S, B (drop ε) |
| S → aSb | S | aSb, ab |
| A → aA | A | aA, a |
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.
Safe order
- Remove ε-productions.
- Remove unit productions.
- Remove useless symbols (generating, then reachable).
This order never re-introduces something already removed.
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} unchangedWorked Examples
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.
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.
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
- MCQ
In which order should useless symbols be removed?
- unreachable, then non-generating
- non-generating, then unreachable
- either order
- only non-generating
- MSQ
Grammar: S → AB | a, A → BC | ε, B → b | ε, C → c. Which variables are nullable?
- S
- A
- B
- C
- NAT
After removing ε-rules from S → aSbS | ε (not adding ε back), how many S-rules are there?
Write your answer before opening the key.
- MCQ
A unit production is:
- A → a
- A → B
- A → ε
- 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
- One string with two parse trees (two leftmost derivations).
- S → SS | (S) | ε.
- Check whether an accepting state is reachable.
Self-test answers
- B — The other order may leave useless symbols.
- A: Can miss some.
- C: Not safe.
- D: Unreachable ones must go too.
- ABC — A → ε, B → ε, S → AB with both nullable.
- D: C → c only.
- 4 — aSbS, abS, aSb, ab.
- B — One variable on the right.
- A: Terminal rule.
- C: ε-rule.
- D: Two variables.
Flashcards
- Which useless-symbol step comes first?
Reveal answer
Remove non-generating symbols, then unreachable ones. - Safe simplification order?
Reveal answer
ε-productions, unit productions, useless symbols. - What is a nullable variable?
Reveal answer
A ⇒* ε.