Sub topic 16
Decidable and Undecidable Questions About CFLs
Assume you know context-free grammars (CFGs), pushdown automata (PDAs), and basic proof by contradiction. A decision problem asks whether an algorithm can always return a correct yes-or-no answer and halt.
For context-free languages (CFLs), some useful questions are decidable. Others are impossible to decide for every CFG.
Membership and emptiness
The membership problem asks whether a string \(w\) belongs to the language generated by a CFG \(G\).
This problem is decidable. For example, the CYK algorithm converts the grammar to Chomsky normal form and fills a table of substrings. Its running time is \(O(n^3|G|)\), where \(n\) is the input length and \(|G|\) measures the grammar size.
The emptiness problem asks whether \(L(G)=\varnothing\). It is also decidable. Mark variables that can derive terminal strings. If the start variable is never marked, the language is empty.
A grammar has an infinite language exactly when a productive variable can derive itself through a nonempty sentential form. This gives a decision procedure for checking whether a CFL is finite.
Important decision properties
| Question about a CFG | Status for CFLs |
|---|---|
| Is \(w \in L(G)\)? | Decidable |
| Is \(L(G)=\varnothing\)? | Decidable |
| Is \(L(G)\) finite? | Decidable |
| Are \(L(G_1)\) and \(L(G_2)\) equal? | Undecidable |
| Is \(L(G)=\Sigma^*\)? | Undecidable |
| Is the grammar ambiguous? | Undecidable |
The equivalence problem asks whether two CFGs generate exactly the same language. No algorithm can decide this for all pairs of CFGs.
The universality problem asks whether a CFG generates every string over its alphabet. It is also undecidable. The inclusion problem, \(L(G1)\subseteq L(G2)\), is undecidable as well.
Ambiguity is undecidable: there is no general algorithm that always determines whether some string has two distinct parse trees for a given CFG. This is different from checking one specified string, which can be done by searching for multiple derivations.
CFLs versus deterministic CFLs
A deterministic context-free language (DCFL) is accepted by a deterministic PDA. DCFLs form a smaller class than CFLs.
Some difficult questions improve for DCFLs. In particular, equivalence of deterministic pushdown automata is decidable, although the known algorithms are highly complex. This does not make every property easy. Always identify whether a question concerns arbitrary CFLs or DCFLs.
Exercise
Is it decidable whether a CFG generates at least one string?
Show answer
Yes. Mark variables that can derive terminal strings. The language is nonempty exactly when the start variable is marked.