Sub topic 15
Closure properties of CFLs and deterministic CFLs
This chapter assumes you can build CFGs and PDAs, know that DPDAs are strictly weaker than NPDAs, and have seen the pumping lemma for CFLs. It answers one question: if you combine context-free languages with an operation, is the result still context-free? For GATE, you mainly need the table in the next section, plus the arguments that justify it.
What closure means
A class of languages is closed under an operation if applying the operation to members of the class always gives a member of the same class. To show closure, give a construction. To show non-closure, give one counterexample.
The tests treat two classes separately:
- CFLs are accepted by nondeterministic PDAs.
- DCFLs (deterministic CFLs) are accepted by deterministic PDAs, by final state.
The summary table
| Operation | CFL | DCFL |
|---|---|---|
| Union | Closed | Not closed |
| Concatenation | Closed | Not closed |
| Kleene star | Closed | Not closed |
| Reversal | Closed | Not closed |
| Homomorphism | Closed | Not closed |
| Inverse homomorphism | Closed | Closed |
| Intersection | Not closed | Not closed |
| Complement | Not closed | Closed |
| Difference L1 − L2 | Not closed | Not closed |
| Intersection with a regular language | Closed | Closed |
| Union with a regular language | Closed | Closed |
| Difference L − R, R regular | Closed | Closed |
Two patterns help you remember it. CFLs are closed under the "regular-style" operations (union, concatenation, star) but not under the Boolean operations that need both a stack and a second check (intersection, complement). DCFLs are the reverse: complement works, the regular-style operations mostly fail. Both classes stay closed under intersection with a regular language.
Why CFLs are closed under union, concatenation, and star
Take grammars G1 and G2 with start symbols S1 and S2. Rename the variables so they do not overlap, then add a new start symbol S:
- Union:
S -> S1 | S2 - Concatenation:
S -> S1 S2 - Star:
S -> S1 S | ε
The following program builds these grammars and lists the strings they generate up to a length bound. A grammar is a dictionary from variable to a list of right-hand sides. Every key is a variable, and every other symbol is a terminal. An empty tuple is ε.
def tag(g, t):
"""Prefix every variable with t so two grammars never share names."""
return {t + A: [tuple(t + s if s in g else s for s in rhs) for rhs in R]
for A, R in g.items()}
def union(g1, g2):
g = {**tag(g1, "1"), **tag(g2, "2")}
g["S"] = [("1S",), ("2S",)]
return g
def concat(g1, g2):
g = {**tag(g1, "1"), **tag(g2, "2")}
g["S"] = [("1S", "2S")]
return g
def star(g1):
g = tag(g1, "1")
g["S"] = [("1S", "S"), ()]
return g
def lang(g, n):
"""All strings of length <= n derived from S (fixed-point iteration)."""
L = {A: set() for A in g}
changed = True
while changed:
changed = False
for A, R in g.items():
for rhs in R:
parts = {""}
for s in rhs:
opts = L[s] if s in g else {s}
parts = {p + o for p in parts for o in opts if len(p + o) <= n}
new = parts - L[A]
if new:
L[A] |= new
changed = True
return L["S"]
order = lambda w: (len(w), w)
anbn = {"S": [("a", "S", "b"), ("a", "b")]}
cplus = {"S": [("c", "S"), ("c",)]}
print(sorted(lang(union(anbn, cplus), 4), key=order))
print(sorted(lang(concat(anbn, cplus), 4), key=order))
print(sorted(lang(star(anbn), 6), key=order))The output is:
['c', 'ab', 'cc', 'ccc', 'aabb', 'cccc']
['abc', 'abcc']
['', 'ab', 'aabb', 'abab', 'aaabbb', 'aabbab', 'abaabb', 'ababab']Reversal is also easy: reverse every right-hand side of every production. Homomorphism replaces each terminal by a string, which you do by substituting inside the productions.
Why CFLs are not closed under intersection
Let
- L1 = { a^n b^n c^m : n, m ≥ 0 }
- L2 = { a^m b^n c^n : n, m ≥ 0 }
Both are context-free. A PDA for L1 matches a's with b's and then ignores the c's. A PDA for L2 skips the a's and matches b's with c's. Their intersection is { a^n b^n c^n : n ≥ 0 }, which the pumping lemma shows is not context-free.
An intersection that fails is the key counterexample for GATE.
Complement and difference
CFLs are not closed under complement. If they were, then L1 ∩ L2 = (L1' ∪ L2')' would be context-free, because CFLs are closed under union. That contradicts the previous section.
Difference fails for the same reason: Σ is context-free, and Σ − L is the complement of L.
Intersection with a regular language
Given a PDA P and a DFA D, build a product automaton. Its state is a pair (p, q), the stack is P's stack, and each move advances both machines on the same input symbol. It accepts when both components are in final states. The stack comes from P alone, so the result is still a PDA. If P is deterministic, the product is deterministic, so DCFLs are also closed.
This gives a useful trick for the difference L − R. Since L − R = L ∩ R' and R' is regular, the result is context-free. It also gives a proof tool: to show that a language is not context-free, intersect it with a regular language and show that the result is not context-free.
The DCFL column
Complement. Complete a DPDA so that it always reads the whole input, which means removing loops that never consume input and adding a trap state. Then swap final and non-final states. The textbook construction needs care with ε-moves, but the result holds: DCFLs are closed under complement.
Union is not closed. Let
- L = { a^i b^j c^k : i ≠ j or j ≠ k }
This is the union of two DCFLs, { i ≠ j } and { j ≠ k }. Suppose L were a DCFL. Then its complement would be a DCFL. Intersecting that complement with the regular language a\b\c\* would give a DCFL. But this intersection is { a^n b^n c^n }, which is not even context-free. So L is a union of two DCFLs that is not a DCFL. L is still a CFL, because CFLs are closed under union.
Other failures. DCFLs are also not closed under concatenation, Kleene star, reversal, or general homomorphism, and not under intersection. Intersection fails because the two languages in the earlier example are both DCFLs. The earlier union proof shows that DCFLs and CFLs are different classes.
Inverse homomorphism. DCFLs are closed under it, as are CFLs and regular languages.
Common errors
- Saying "CFLs are closed under complement because DFAs are." The DFA construction depends on determinism, and nondeterministic PDAs cannot be complemented by swapping states.
- Treating "L1 ∩ L2 is not a CFL" as true for every pair. It is only not guaranteed. For many pairs, such as two regular languages, the intersection is a CFL. Closure failure means some counterexample exists.
- Forgetting that the language itself matters. { a^n b^n c^n } is not a CFL, but its complement is a CFL, since the complement can be accepted by choosing which counting condition to check.
- Confusing "DCFL" with "unambiguous." Every DCFL has an unambiguous grammar, but some unambiguous CFLs are not deterministic, such as { w w^R }.
Exercise
L1 = { a^n b^n : n ≥ 0 } is a DCFL and R = a\b\ is regular. State whether each of the following must be context-free: (a) L1 ∪ R, (b) L1 ∩ R, (c) the complement of L1, (d) L1 − R.
Show answer
(a) Yes, since CFLs are closed under union and R is regular, hence context-free. (b) Yes, since L1 ∩ R = L1 here and intersection with a regular language is always context-free. (c) Yes, because L1 is a DCFL and DCFLs are closed under complement, so its complement is a DCFL and hence context-free. (d) Yes, since L1 − R = L1 ∩ R' and R' is regular. In this case it is the empty language.
Exercise
Is { a^n b^n c^n : n ≥ 0 } context-free? Use closure properties to justify your answer without applying the pumping lemma to it directly, assuming you already know that { w w : w in {a,b}\* } is not context-free.
Show answer
A direct closure argument here is awkward, so the statement is best proved with the pumping lemma, as in the previous chapter. The closure argument works in the other direction. If { a^n b^n c^n } were context-free, then it would be the intersection of the CFLs { a^n b^n c^m } and { a^m b^n c^n } and nothing would be contradictory, so closure alone cannot settle it. Use the pumping lemma on z = a^p b^p c^p: any window vwx of length at most p misses at least one letter, so pumping breaks the equal counts. Hence the language is not context-free.