G27
Self-checkedTier 2 12 min readtoc.cfl-decision-problems

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 Σ∗\Sigma^*, 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 ECFG={⟨G⟩:L(G)=∅}E_{CFG} = \{\langle G \rangle : L(G) = \emptyset\}. It is decidable if some Turing machine halts on every input and accepts exactly the encodings with answer yes. The problems of this page:

namequestion about the grammar GG (or G1G_1, G2G_2) and the DFA or expression RR
ACFGA_{CFG}does GG generate the string ww
ECFGE_{CFG}is L(G)=∅L(G) = \emptyset
FINCFGFIN_{CFG}is L(G)L(G) finite
ALLCFGALL_{CFG}is L(G)=Σ∗L(G) = \Sigma^* (universality)
EQCFGEQ_{CFG}is L(G1)=L(G2)L(G_1) = L(G_2)
L(G1)⊆L(G2)L(G_1) \subseteq L(G_2)containment
L(G1)∩L(G2)=∅L(G_1) \cap L(G_2) = \emptysetdisjointness
AMBCFGAMB_{CFG}is GG ambiguous
REGCFGREG_{CFG}is L(G)L(G) regular
L(G)∩L(R)=∅L(G) \cap L(R) = \emptyset, L(G)⊆L(R)L(G) \subseteq L(R)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 (ACFGA_{CFG}). Convert GG to Chomsky normal form and run CYK: the table of variables that derive each substring is filled in O(n3)O(n^3) steps for a string of length nn (normal forms). (The empty string is handled separately: it is generated exactly when the start variable is nullable.)

Emptiness (ECFGE_{CFG}). 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
stepvariables addedgenerating so farS 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.

0 / 5
computed by script: machines are simulated and tables constructed by scripts/fst_lib.py and scripts/toc_t2.py, one step at a time. python3 scripts/gen-toc6-traces.py

(In G1G_1 the variables CC and DD never become generating, but SS does through S→ABS \to AB.) The same fixpoint, keeping the shortest string found for each variable, produces a shortest string of the language.

Finiteness (FINCFGFIN_{CFG}). First remove the epsilon productions, the unit productions and the useless symbols (normal forms). Draw a graph with an edge A→BA \to B when BB occurs on the right side of a production of AA. 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 A⇒+xAyA \Rightarrow^{+} xAy with xy≠εxy \neq \varepsilon 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
grammarreduced grammarcycle among the variableslanguagestrings 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).

0 / 6
computed by script: machines are simulated and tables constructed by scripts/fst_lib.py and scripts/toc_t2.py, one step at a time. python3 scripts/gen-toc6-traces.py

Rows 4 and 5 show why the reduction must come first: a cycle made only of unit productions (A→B→AA \to B \to A) 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 [p A q][p\, A\, q] mean "AA derives a string that takes the DFA from pp to qq". Then two questions are decidable:

  • L(G)∩L(R)=∅L(G) \cap L(R) = \emptyset: build the product grammar and test it for emptiness.
  • L(G)⊆L(R)L(G) \subseteq L(R): this is L(G)∩L(R)‾=∅L(G) \cap \overline{L(R)} = \emptyset, 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 L(G)L(G) but not in L(R)L(R)):

Questions about a context-free grammar and a regular language: start

Computed by a script, not typed
questionanswerwitness (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.

0 / 6
computed by script: machines are simulated and tables constructed by scripts/fst_lib.py and scripts/toc_t2.py, one step at a time. python3 scripts/gen-toc6-traces.py

The reverse direction, L(R)⊆L(G)L(R) \subseteq L(G), is not decidable: it contains universality as the special case R=Σ∗R = \Sigma^*.

The undecidable problems

None of the following has an algorithm. They all come from the Post correspondence problem (PCP): given tiles (t1,b1),…,(tk,bk)(t_1, b_1), \dots, (t_k, b_k) of strings, is there a sequence of tile numbers i1,…,imi_1, \dots, i_m (with m≥1m \ge 1, repetition allowed) such that ti1⋯tim=bi1⋯bimt_{i_1} \cdots t_{i_m} = b_{i_1} \cdots b_{i_m}? That PCP is undecidable is a standard theorem (quoted). To see the connection, build two grammars over the tile letters, a marker cc and the tile numbers:

GA:  S→wi S i∣wi c iGB:  S→vi S i∣vi c i(i=1,…,k)G_A : \; S \to w_i\, S\, i \mid w_i\, c\, i \qquad G_B : \; S \to v_i\, S\, i \mid v_i\, c\, i \qquad (i = 1, \dots, k)

where wiw_i is the top and viv_i the bottom of tile ii. A string of L(GA)L(G_A) looks like ti1⋯tim c im⋯i1t_{i_1} \cdots t_{i_m}\, c\, i_m \cdots i_1 (the tile numbers come out in reverse order, because the innermost rule is applied last). The same string lies in L(GB)L(G_B) exactly when the bottoms spell the same word for the same sequence of tiles. So

L(GA)∩L(GB)≠∅  ⟺  the PCP instance has a solution.L(G_A) \cap L(G_B) \neq \emptyset \iff \text{the PCP instance has a solution.}

From the Post correspondence problem to two context-free grammars: start

Computed by a script, not typed
instanceshortest solution (tile numbers)common top and bottom stringstring generated by G_Astring 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.

0 / 4
computed by script: machines are simulated and tables constructed by scripts/fst_lib.py and scripts/toc_t2.py, one step at a time. python3 scripts/gen-toc6-traces.py

If "is L(G1)∩L(G2)L(G_1) \cap L(G_2) 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 L(G)=Σ∗L(G) = \Sigma^*: 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 Σ∗\Sigma^* exactly when the PCP instance has no solution.
  • Equivalence L(G1)=L(G2)L(G_1) = L(G_2) and containment L(G1)⊆L(G2)L(G_1) \subseteq L(G_2): universality is the special case where G2G_2 generates Σ∗\Sigma^*.
  • Ambiguity: a grammar built from the PCP instance is ambiguous exactly when there is a solution.
  • Regularity: given GG, "is L(G)L(G) 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

problemregular languagescontext-free languagesTuring machines
membershipdecidabledecidable (CYK)undecidable
emptinessdecidabledecidableundecidable
finitenessdecidabledecidableundecidable
universality =Σ∗= \Sigma^*decidableundecidableundecidable
equivalencedecidableundecidableundecidable
containmentdecidableundecidableundecidable
intersection emptydecidableundecidable (two CFLs); decidable (CFL and regular)undecidable
ambiguity of a grammarnot applicableundecidablenot applicable
is the language regulartrivially yesundecidableundecidable

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

S→ABS \to AB, A→aAA \to aA, B→bB \to b. Round 1 marks BB (it has a production with only terminals); AA has none, since its only production uses AA itself; round 2 marks nothing new. SS is not marked, so the language is empty. Change A→aAA \to aA into A→aA∣aA \to aA \mid a: round 1 marks AA and BB, round 2 marks SS, and the shortest string is abab.

EX 2Finiteness: reduce first

Easy

S→AS \to A, A→BA \to B, B→A∣aB \to A \mid a. The dependency graph has the cycle A→B→AA \to B \to A, but after removing the unit productions the grammar is S→aS \to a: the language is {a}\{a\}, finite. And S→aS∣CS \to aS \mid C, C→CCC \to CC: CC is not generating, so after removing useless symbols nothing is left and the language is empty (finite).

EX 3Intersection with a regular language

Medium

GG: S→aSb∣abS \to aSb \mid ab generates {anbn:n≥1}\{a^n b^n : n \ge 1\}. With RR = "contains baba" the intersection is empty (a string of aa's followed by bb's never contains baba): decidable by the product grammar. With RR = "length divisible by 3" the shortest common string is aaabbbaaabbb. And L(G)⊆a∗b∗L(G) \subseteq a^* b^* holds, but L(G)⊆L(G) \subseteq "even number of aa's" fails with the witness abab.

EX 4From a PCP instance to two grammars

Medium

Tiles 1:ab/a1 : ab/a and 2:b/bb2 : b/bb. The sequence 1,21, 2 gives ab⋅b=abbab \cdot b = abb on top and a⋅bb=abba \cdot bb = abb on the bottom. GAG_A generates abb c 21abb\,c\,21 and so does GBG_B; the common string spells out the solution (the tops, the marker, the tile numbers backwards). For tiles 1:aa/a1 : aa/a and 2:ab/b2 : ab/b 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: L(G1)=L(G2)L(G_1) = L(G_2); L(G1)∩L(G2)=∅L(G_1) \cap L(G_2) = \emptyset; L(G)=L(R)L(G) = L(R); L(G)=∅L(G) = \emptyset?" 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: L(G)∩L(R)=∅L(G) \cap L(R) = \emptyset, L(G)⊆L(R)L(G) \subseteq L(R) (product construction; complement the DFA).
  • Undecidable: L(G)=Σ∗L(G) = \Sigma^*, L(G1)=L(G2)L(G_1) = L(G_2), L(G1)⊆L(G2)L(G_1) \subseteq L(G_2), L(G1)∩L(G2)=∅L(G_1) \cap L(G_2) = \emptyset, ambiguity, "is L(G)L(G) regular", L(R)⊆L(G)L(R) \subseteq L(G), equivalence of PDAs.
  • DPDA equivalence is decidable (quoted); regular languages: everything decidable.
  • Source of the undecidability: Post correspondence problem; L(GA)∩L(GB)≠∅L(G_A) \cap L(G_B) \ne \emptyset iff a solution exists.
  • Bounded search can confirm yes, never prove no.

Traps and GATE patterns

Connections

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

GATE 2025 · Set 2 · MCQ · 2 marksQ4 · page 225 of the book
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 ) = ∅?
GATE 2021 · MCQQ4 · page 241 of the book
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) = Σ*
GATE 2017 · Set 2 · MCQQ9 · page 242 of the book
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
GATE 2008 · MCQ · 2 marksQ23 · page 244 of the book
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
GATE 2007 · MCQ · 1 markQ25 · page 244 of the book
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 "L(G)=∅L(G) = \emptyset" decidable for a CFG? Why?

Show answer

Yes: mark generating variables until nothing changes; the language is empty iff the start variable is not marked.

Question 2

Is "is L(G1)∩L(G2)L(G_1) \cap L(G_2) empty" decidable for CFGs? For a CFG and a DFA?

Show answer

Not for two CFGs (PCP); yes for a CFG and a DFA (product construction, then emptiness).

Question 3

How do you decide whether L(G)L(G) is finite?

Show answer

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 answer

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 answer

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: =Σ∗= \Sigma^*, equivalence, containment, disjointness of two CFLs, ambiguity, regularity, L(R)⊆L(G)L(R) \subseteq L(G).
  • Source: PCP; GA,GBG_A, G_B generate x c reverse(indices)x\,c\,\text{reverse(indices)}; 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