Sub topic 16
Decidable and undecidable questions about CFLs
This chapter assumes you know context-free grammars (CFGs), Chomsky normal form, grammar simplification, the CYK algorithm, and the closure properties of CFLs. It sorts the standard questions about a CFG into those an algorithm can always answer and those no algorithm can answer. GATE asks this classification often, usually as a "which of these is undecidable?" question.
What decidable means
A question is decidable if some algorithm halts on every input and answers yes or no correctly. It is undecidable if no such algorithm exists. The input here is a grammar (or PDA), not a single string. For regular languages every standard question is decidable. For CFLs the picture splits.
Decidable questions for CFLs
- Membership: is w in L(G)? Convert G to Chomsky normal form and run CYK. This takes O(n^3 · |G|) time. A CNF derivation of a string of length n takes exactly 2n − 1 steps, so you can also search all derivations of that length.
- Emptiness: is L(G) = ∅? Compute the set of generating variables, meaning those that derive some terminal string. L(G) is empty exactly when the start symbol is not generating.
- Finiteness: is L(G) finite? Remove useless symbols and put G in CNF. Draw a graph with an edge A → B whenever some rule A → BC or A → CB exists. L(G) is infinite exactly when this graph has a cycle. A cycle means some variable derives a string containing itself, which lets you pump.
The emptiness test is a fixpoint computation:
def is_empty(rules, start, terminals):
"""rules: list of (lhs, rhs_tuple). An epsilon rule has rhs ()."""
generating = set()
changed = True
while changed:
changed = False
for lhs, rhs in rules:
if lhs not in generating and all(
s in terminals or s in generating for s in rhs
):
generating.add(lhs)
changed = True
return start not in generating
terminals = {"a", "b"}
g1 = [("S", ("A", "B")), ("A", ("a",)), ("B", ("B", "b"))]
g2 = [("S", ("a", "S", "b")), ("S", ("a", "b"))]
print(is_empty(g1, "S", terminals)) # True: B never reaches terminals only
print(is_empty(g2, "S", terminals)) # False: S derives abIn g1, B only ever rewrites to Bb, so B is not generating. Then S is not generating either, and the language is empty.
Undecidable questions for CFLs
For arbitrary CFGs G, G1, G2, none of the following can be decided:
- Universality: is L(G) = Σ*?
- Equivalence: is L(G1) = L(G2)?
- Containment: is L(G1) ⊆ L(G2)?
- Disjointness: is L(G1) ∩ L(G2) = ∅?
- Ambiguity: is G ambiguous?
- Regularity: is L(G) regular?
- Is the complement of L(G) context-free? (Also undecidable.)
- Is the intersection L(G1) ∩ L(G2) context-free?
Why they are undecidable
The standard proof reduces from the Post Correspondence Problem (PCP), which is known to be undecidable. An instance is a list of domino pairs (xi, yi) of strings. The question is whether some sequence of indices i1, …, ik gives x{i1}…x{ik} = y{i1}…y{ik}.
From a PCP instance you build two CFGs. One generates the "top" sequences with their index lists, and the other generates the "bottom" sequences. A common string in both languages is exactly a PCP solution. So an algorithm for disjointness would solve PCP. The same construction shows that the complement of a suitable CFL is non-empty exactly when PCP has no solution. That gives universality, and from universality (compare G with a grammar for Σ*) you get equivalence and containment.
Ambiguity and regularity are shown undecidable by similar reductions from PCP.
Closure properties explain some of these
CFLs are not closed under intersection or complement. That is why "is L1 ∩ L2 empty?" has no general algorithm, while "is L(G) empty?" does. The decidable questions are the ones that ask about a single grammar's own structure, and CYK or a fixpoint answers them directly.
Deterministic CFLs behave better
DCFLs are closed under complement, which changes some answers. Universality of a DPDA reduces to emptiness of its complement, which is decidable. Two further results are deeper:
- DPDA equivalence is decidable (Sénizergues, 1997).
- Regularity of a DCFL is decidable (Stearns, 1967).
Intersection-based questions stay undecidable for DCFLs, because the PCP construction uses only deterministic languages.
| Question | Regular | DCFL | CFL |
|---|---|---|---|
| Membership | Decidable | Decidable | Decidable |
| Emptiness | Decidable | Decidable | Decidable |
| Finiteness | Decidable | Decidable | Decidable |
| Universality (= Σ*) | Decidable | Decidable | Undecidable |
| Equivalence | Decidable | Decidable | Undecidable |
| Containment | Decidable | Undecidable | Undecidable |
| Disjointness | Decidable | Undecidable | Undecidable |
| Is it regular? | Trivial | Decidable | Undecidable |
| Ambiguity of a grammar | Not applicable | Not applicable | Undecidable |
A decision map
Common errors
- Claiming CFG equivalence is decidable because DFA equivalence is. The proof for DFAs uses the product construction and complement, and CFLs lack both.
- Saying emptiness is undecidable because intersection emptiness is. They are different questions: one grammar versus two.
- Forgetting the exceptions for DCFLs. "Equivalence of DPDAs" is decidable, and a GATE option that lists it as undecidable is wrong.
- Treating finiteness as undecidable. It is decidable for CFLs.
- Applying the CYK algorithm to a grammar that is not in CNF. Convert first, and handle the empty string separately.
GATE pattern
Questions typically give four statements and ask which are undecidable. A reliable rule: if the statement compares two CFGs, or asks whether one grammar generates everything, is ambiguous, or is regular, mark it undecidable. If it asks whether one grammar generates a given string, nothing at all, or finitely many strings, mark it decidable. Then check whether the question restricts to DCFLs or regular languages, where the answers shift.
Exercise
For context-free grammars, which of these are decidable: (a) w ∈ L(G), (b) L(G) = ∅, (c) L(G) is finite, (d) L(G1) ∩ L(G2) = ∅, (e) L(G) = Σ*? Give the method for each decidable one.
Show answer
(a), (b) and (c) are decidable. (a) uses CYK on a CNF grammar. (b) checks whether the start symbol is a generating variable by a fixpoint computation. (c) removes useless symbols, converts to CNF, and tests the variable graph for a cycle. (d) and (e) are undecidable, by reduction from the Post Correspondence Problem.
Exercise
Run the emptiness procedure by hand on S → AB | a, A → AA, B → b. Is L(G) empty, and which variables are generating?
Show answer
Start with no generating variables. S → a has only terminals, so S is generating. B → b makes B generating. A → AA only produces strings containing A, so A is never generating. The start symbol S is generating, so L(G) is not empty. The language is {a}, because the rule S → AB can never complete.