CFL closure lab
Hopcroft 2e §7.3. What the context-free languages are closed under, each built in front of you — and what they are not: two grammars whose intersection is aⁿbⁿcⁿ, with the pumping game that proves it. For the regular languages’ version, see the closure lab.
- S → a S b
- S → a b
- S' → b S'
- S' → b
Narration
The two grammars must share no variable, so in the second S becomes S'.
b ab bb bbb aabb bbbb bbbbb aaabbb
- S → A B
- A → a A b
- A → a b
- B → c B
- B → c
- S → A B
- A → a A
- A → a
- B → b B c
- B → b c
Both are context-free — the grammars are right there. Their intersection is L₁ ∩ L₂ = {aⁿbⁿcⁿ | n ≥ 1}, and the CFL pumping game proves that is not context-free (Theorem 7.18). So the CFLs are not closed under intersection, and by De Morgan not under complement or difference either (Theorem 7.29) — while intersection with a regular language always works. (The book writes this example over 0, 1, 2; the letters are renamed a, b, c to match the game.)
For context-free grammars the following are undecidable — no algorithm answers them for every grammar, so this tool does not pretend to. Where a page touches one, it reports a bounded search and says so.
- Is G ambiguous? — the ambiguity detector searches up to a length and reports "no counterexample within bounds", never "unambiguous".
- Do G₁ and G₂ generate the same language? — only a sample comparison is offered, labelled as a sample.
- Is L(G₁) ∩ L(G₂) empty? — the intersection is not even context-free in general.
- Is L(G) = Σ*? Is L(G) regular? Is the complement of L(G) context-free?