Decision problems for CFLs
Which questions about context-free grammars and pushdown automata can an algorithm answer: membership (CYK), emptiness (generating variables), finiteness (cycle in the reduced grammar), emptiness of the intersection with a regular language and containment in a regular language (product construction); and which cannot: universality, equivalence, containment, disjointness of two grammars, ambiguity and regularity, all by reduction from the Post correspondence problem; the contrast with regular languages and with Turing machines.
Why it exists
For regular languages every natural question has an algorithm: is the language empty, finite, all of , equal to another one, contained in another one. For context-free languages the picture splits. Some questions (is a string generated, is the language empty or finite) have algorithms; many others (are two grammars equivalent, is the grammar ambiguous) have none. GATE asks: which of these problems about grammars and automata is decidable? The task is to classify, not to run the algorithms, but the algorithms explain the classification.
Prerequisites
You need the closure properties of context-free languages (CFL closure): in particular that the intersection of a context-free language and a regular language is context-free while the intersection of two context-free languages need not be. You also need the Chomsky normal form (normal forms) and the notion of decidability (decidable and recognisable languages).
Precise definitions
A decision problem about grammars is a language of encodings, for example . It is decidable if some Turing machine halts on every input and accepts exactly the encodings with answer yes. The problems of this page:
| name | question about the grammar (or , ) and the DFA or expression |
|---|---|
| does generate the string | |
| is | |
| is finite | |
| is (universality) | |
| is | |
| containment | |
| disjointness | |
| is ambiguous | |
| is regular | |
| , | questions about a grammar and a regular language |
Everything is the same for pushdown automata, since a grammar and a pushdown automaton can be converted into each other (PDA and CFG).
How it works
The decidable problems
Membership (). Convert to Chomsky normal form and run CYK: the table of variables that derive each substring is filled in steps for a string of length (normal forms). (The empty string is handled separately: it is generated exactly when the start variable is nullable.)
Emptiness (). Mark the generating variables: first those with a production whose right side has only terminals, then repeatedly those with a production whose right side uses only terminals and variables already marked. The language is empty exactly when the start variable is never marked. The table shows the rounds for two grammars:
The emptiness test: which variables can derive a string of terminals: start
Computed by a script, not typed| step | variables added | generating so far | S generating? |
|---|---|---|---|
| G1, round 1 | |||
| G1, round 2 | |||
| G1, final | |||
| G2, round 1 | |||
| G2, final |
Step 0
G1: S -> A B | C, A -> aA | a, B -> bB | b, C -> Dc, D -> Cd. G2: S -> AB, A -> aA, B -> b. The language is empty exactly when S never joins the set. In G1 the variables C and D are never added (each needs the other first) but S -> AB makes S generating anyway.
(In the variables and never become generating, but does through .) The same fixpoint, keeping the shortest string found for each variable, produces a shortest string of the language.
Finiteness (). First remove the epsilon productions, the unit productions and the useless symbols (normal forms). Draw a graph with an edge when occurs on the right side of a production of . In the reduced grammar every variable is reachable and generating, and every production makes the derived form at least as long as the variable; so the language is infinite exactly when the graph has a cycle. (A cycle with can be repeated; with no cycle the depth of every derivation is bounded, so there are finitely many.) Test the grammars of the table:
The finiteness test: start
Computed by a script, not typed| grammar | reduced grammar | cycle among the variables | language | strings of length ≤ 8 / ≤ 14 |
|---|---|---|---|---|
| S -> A B; A -> a | a a; B -> b | c | ||||
| S -> A B; A -> a A | a; B -> b | ||||
| S -> a S b | ε | ||||
| S -> A; A -> B; B -> A | a | ||||
| S -> a S | C; C -> C C | ||||
| S -> S S | a |
Step 0
Reduce first (no epsilon productions, no unit productions, no useless symbols), then draw the graph A -> B when B occurs on a right side of A. A cycle means that some variable derives a longer form containing itself, so pumping gives infinitely many strings; no cycle means a bounded derivation depth, so only finitely many strings. A cycle in the unreduced grammar proves nothing (rows 4 and 5).
Rows 4 and 5 show why the reduction must come first: a cycle made only of unit productions () adds no strings, and a cycle among variables that can never produce a terminal string is useless.
A grammar and a regular language. The intersection of a context-free language with a regular language is context-free: run a pushdown automaton and a DFA in parallel, or build a grammar whose variables mean " derives a string that takes the DFA from to ". Then two questions are decidable:
- : build the product grammar and test it for emptiness.
- : this is , and the complement of a regular language is regular (swap accepting states of a complete DFA).
Both are decided by the same code; the table lists six cases, with the shortest witness string (a string in the intersection, or in but not in ):
Questions about a context-free grammar and a regular language: start
Computed by a script, not typed| question | answer | witness (shortest string) |
|---|---|---|
| L(G) ∩ L(R) = ∅ ? G: a^n b^n (n ≥ 1); R: even number of a's | ||
| L(G) ∩ L(R) = ∅ ? G: a^n b^n (n ≥ 1); R: contains ba | ||
| L(G) ∩ L(R) = ∅ ? G: a^n b^n (n ≥ 1); R: length divisible by 3 | ||
| L(G) ⊆ L(R) ? G: a^n b^n (n ≥ 1); R: a*b* | ||
| L(G) ⊆ L(R) ? G: a^n b^n (n ≥ 1); R: even number of a's | ||
| L(G) ⊆ L(R) ? G: palindromes; R: even length |
Step 0
Both kinds of question are decided by the same construction: build a grammar for L(G) ∩ L(R) (the product of a Chomsky normal form grammar with the DFA) and test it for emptiness. Containment L(G) ⊆ L(R) uses the complement of the DFA. The reverse question L(R) ⊆ L(G) is NOT decidable.
The reverse direction, , is not decidable: it contains universality as the special case .
The undecidable problems
None of the following has an algorithm. They all come from the Post correspondence problem (PCP): given tiles of strings, is there a sequence of tile numbers (with , repetition allowed) such that ? That PCP is undecidable is a standard theorem (quoted). To see the connection, build two grammars over the tile letters, a marker and the tile numbers:
where is the top and the bottom of tile . A string of looks like (the tile numbers come out in reverse order, because the innermost rule is applied last). The same string lies in exactly when the bottoms spell the same word for the same sequence of tiles. So
From the Post correspondence problem to two context-free grammars: start
Computed by a script, not typed| instance | shortest solution (tile numbers) | common top and bottom string | string generated by G_A | string generated by G_B |
|---|---|---|---|---|
| P1 1: ab/a 2: b/bb | ||||
| P2 1: b/bbb 2: babbb/ba 3: ba/a | ||||
| P3 1: aa/a 2: ab/b | ||||
| P4 1: ab/ba 2: a/b |
Step 0
G_A has the productions S -> w_i S i | w_i c i (w_i the top of tile i); G_B the same with the bottoms. A string x c i_k ... i_1 is in both languages exactly when the tiles i_1 ... i_k give the same string on top and on bottom. So deciding 'is the intersection of two CFLs empty' would decide PCP. A bounded search can find a solution, but its failure proves nothing: the unsolvable rows are proved by a length argument and by a first-letter argument, not by the search.
If "is empty?" had an algorithm, PCP would have one. Hence disjointness of two context-free grammars is undecidable. The same instances give the other results, with a variation of the encoding that makes the two languages deterministic (so that their complements are context-free); the proofs are in the textbooks and are quoted here:
- Universality : with the deterministic variant of the encoding, the complements of the two languages are context-free, their union is a context-free language, and it is all of exactly when the PCP instance has no solution.
- Equivalence and containment : universality is the special case where generates .
- Ambiguity: a grammar built from the PCP instance is ambiguous exactly when there is a solution.
- Regularity: given , "is regular?" is undecidable (a variation of the same argument).
Bounded search shows the limit of the reduction: running through all tile sequences up to some length finds solutions that exist, but its failure to find one proves nothing. The unsolvable instances in the table are proved so by an argument (every top is longer than its bottom; every pair starts with different letters), not by the search.
Summary
| problem | regular languages | context-free languages | Turing machines |
|---|---|---|---|
| membership | decidable | decidable (CYK) | undecidable |
| emptiness | decidable | decidable | undecidable |
| finiteness | decidable | decidable | undecidable |
| universality | decidable | undecidable | undecidable |
| equivalence | decidable | undecidable | undecidable |
| containment | decidable | undecidable | undecidable |
| intersection empty | decidable | undecidable (two CFLs); decidable (CFL and regular) | undecidable |
| ambiguity of a grammar | not applicable | undecidable | not applicable |
| is the language regular | trivially yes | undecidable | undecidable |
The row of Turing machines follows from Rice's theorem (Rice's theorem). One notable exception to "equivalence is undecidable": equivalence of deterministic pushdown automata is decidable (Senizergues, 1997; quoted), while equivalence of nondeterministic pushdown automata is not.
Worked examples
EX 1Emptiness of a grammar
Easy, , . Round 1 marks (it has a production with only terminals); has none, since its only production uses itself; round 2 marks nothing new. is not marked, so the language is empty. Change into : round 1 marks and , round 2 marks , and the shortest string is .
EX 2Finiteness: reduce first
Easy, , . The dependency graph has the cycle , but after removing the unit productions the grammar is : the language is , finite. And , : is not generating, so after removing useless symbols nothing is left and the language is empty (finite).
EX 3Intersection with a regular language
Medium: generates . With = "contains " the intersection is empty (a string of 's followed by 's never contains ): decidable by the product grammar. With = "length divisible by 3" the shortest common string is . And holds, but "even number of 's" fails with the witness .
EX 4From a PCP instance to two grammars
MediumTiles and . The sequence gives on top and on the bottom. generates and so does ; the common string spells out the solution (the tops, the marker, the tile numbers backwards). For tiles and no sequence works, because every top is longer than its bottom; the grammars generate no common string.
EX 5Typical questions
GATE level(a) "Which of the following is decidable: ; ; ; ?" Only emptiness. (b) "Which of these is undecidable: membership for CFGs, ambiguity of CFGs, finiteness for finite automata, equivalence of finite automata?" Ambiguity of CFGs. (c) "Whether the intersection of two regular languages is infinite; whether a CFL is regular; whether two PDAs accept the same language; whether a grammar is context-free": decidable, undecidable, undecidable, decidable (a grammar is context-free if each production has a single variable on its left, a syntactic check). (d) "Is it decidable whether a given CFG generates all strings?" No. (e) "Is it decidable whether a given string is generated by a given context-sensitive grammar?" Yes (quoted: linear bounded automaton), but not whether the language is empty.
Cheat box
Cheat box
- Decidable for a CFG: membership (CYK), emptiness (generating variables), finiteness (cycle in the reduced grammar).
- Decidable with a regular language: , (product construction; complement the DFA).
- Undecidable: , , , , ambiguity, "is regular", , equivalence of PDAs.
- DPDA equivalence is decidable (quoted); regular languages: everything decidable.
- Source of the undecidability: Post correspondence problem; iff a solution exists.
- Bounded search can confirm yes, never prove no.
Traps and GATE patterns
Connections
Builds on
Unlocks
Nothing built depends on this yet.
Across subjects
No cross-subject links yet.
The decidable questions use constructions from other pages: CYK and the removal of useless symbols (normal forms), the product of a pushdown automaton with a DFA (CFL closure). The undecidable ones extend the decidability results of decidable and recognisable languages and the reductions of the halting problem. The corresponding questions for grammars, automata and expressions of regular languages all have algorithms (closure of regular languages). Ambiguity in practice is handled by grammar transformations, since no algorithm can decide it in general.
PYQs
5 previous year questions from your book. I re-solved each one myself (0 by running or simulating, 5 by reasoning only); the book's key is shown next to my answer. All PYQs
Let G1, G2 be Context Free Grammars (CFGs) and R be a regular expression. For a grammar G, let L(G) denote the language generated by G. Which ONE among the following questions is decidable?
- (a)Is L(G1 ) = L(G2 )?
- (b)Is L(G1 ) ∩ L(G2 ) = ∅?
- (c)Is L(G1 ) = L(R )?
- (d)Is L(G1 ) = ∅?
Let <M> denote an encoding of an automaton M. Suppose
that Σ = {0, 1}. Which of the following languages is/are
NOT recursive?- (a)L= {<M>| M is a DFA such that L(M) = ∅}
- (b)L = {<M>| M is a DFA such that L(M) = Σ*}
- (c)L= {<M>| M is a PDA such that L(M) = ∅}
- (d)L = {<M>| M is a PDA such that L(M) = Σ*
Let L(R) be the language represented by regular expression R. Let L(G) be the language generated by a context free grammar G. Let L(M) be the language accepted by a Turing machine M. Which of the following decision problems are undecidable? I. Given a regular expression R and a string w, is w ∈ L(R)? II. Given a context-free grammar G, is L(G) = ∅? III. Given a context-free grammar G, is L(G) = Σ* for some alphabet Σ? IV. Given a Turing machine M and a string w, is w ∈L(M)?
- (a)I and IV only
- (b)II and III only
- (c)II, III and IV only
- (d)III and IV only
Which of the following are decidable? I. Whether the intersection of two regular languages is infinite II. Whether a given context-free language is regular III. Whether two push-down automata accept the same language IV. Whether a given grammar is context-free
- (a)I and II
- (b)I and IV
- (c)II and III
- (d)II and IV
Which of the following problems is undecidable?
- (a)Membership problem for CFGs
- (b)Ambiguity problem for CFGs
- (c)Finiteness problem for FSAs
- (d)Equivalence problem for FSAs
Self-check
Question 1
Is "" decidable for a CFG? Why?
Show answerHide
Yes: mark generating variables until nothing changes; the language is empty iff the start variable is not marked.
Question 2
Is "is empty" decidable for CFGs? For a CFG and a DFA?
Show answerHide
Not for two CFGs (PCP); yes for a CFG and a DFA (product construction, then emptiness).
Question 3
How do you decide whether is finite?
Show answerHide
Remove epsilon productions, unit productions and useless symbols; the language is infinite iff the dependency graph of the remaining variables has a cycle.
Question 4
Why is universality of a CFG undecidable although emptiness is decidable?
Show answerHide
Complement does not preserve context-freeness, so universality cannot be reduced to emptiness; in fact PCP reduces to it.
Question 5
True or false: given two deterministic pushdown automata, it is decidable whether they accept the same language.
Show answerHide
True (Senizergues; quoted). For nondeterministic pushdown automata it is false.
Revision card
One-screen revision
- Decidable: membership (CYK), emptiness, finiteness, CFL with regular (intersection empty, containment in regular).
- Undecidable: , equivalence, containment, disjointness of two CFLs, ambiguity, regularity, .
- Source: PCP; generate ; common string = solution.
- DPDA equivalence decidable (quoted). Search finds solutions, never proves there are none.
References
- ·Sipser, Introduction to the Theory of Computation (3rd edition), Section 4.1 (decidable problems about context-free languages), Section 5.2 (the Post correspondence problem) and the problems on undecidability for grammars
- ·Hopcroft, Motwani, Ullman, Introduction to Automata Theory, Languages, and Computation (3rd edition), Sections 7.4 (decision properties of context-free languages) and 9.4-9.5 (the Post correspondence problem and undecidable questions about grammars)
- ·Linz, An Introduction to Formal Languages and Automata, Chapter 8 (properties of context-free languages) and Chapter 12 (limits of algorithmic computation)
Verification
scripts/toc_t2.py and scripts/enum-checks/toc_t2_check.py: emptiness by the generating-variable rounds equals the fixpoint of generated strings on 400 random grammars, the shortest generated string is verified by CYK and nothing shorter is generated; finiteness by the cycle in the reduced grammar is checked against bounded sets of generated strings on 500 random grammars with one terminal (finite grammars generate nothing beyond the proven bound, infinite ones do); the intersection of a grammar with a DFA by the product grammar equals brute force (shortest string included) on 300 random grammar and DFA pairs and equals a second, different construction (a pushdown automaton run in parallel with the DFA, then the triple construction) on 25 of them; containment in a regular language agrees with brute force on the same 300 cases; the reduction from the Post correspondence problem is exact: on 300 random tile sets the strings generated by both grammars (up to length 13) are exactly the encoded solutions. That the Post correspondence problem is undecidable, that universality, equivalence, containment, ambiguity and regularity of context-free grammars are undecidable, and that equivalence of deterministic pushdown automata is decidable (Senizergues) are standard textbook results QUOTED here and not machine-proved (needs verification); the page shows the reduction for intersection only, and says where the other proofs differ. Textbook chapter references are from memory. The independent second pass has not been run, so the page stays draft. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand or by a small script (state counts, derivations, parse-tree and move counts, decomposition counts); tables and animations are machine output and were read, not re-run; No other defect found. Status raised to self-checked; verified needs a textbook or human check.
last verified: never
My notes (saved in this browser)