Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 68

Undecidability of CFL Equivalence

Recommended resources

Hello! Welcome to the final lesson in our module on undecidability.

In our last session, we dove into the Post's Correspondence Problem (PCP), a seemingly simple combinatorial puzzle that turned out to be undecidable. We proved this by reducing the Turing Machine acceptance problem () to it. That lesson highlighted how an undecidable problem can serve as a known "hard" problem to prove that other problems are also undecidable.

Today, we will use this same strategy of reduction to tackle a fundamental question about context-free languages: can we determine if two context-free grammars generate the exact same language? This is the Equivalence Problem for Context-Free Languages (). You will learn not only that this problem is undecidable, but also the elegant, multi-step proof that establishes this crucial result, demonstrating how the limits of computation ripple up through the Chomsky Hierarchy.

1. Why is Harder Than ?

Let's start by recalling a problem we've already solved. In a previous module, we showed that the equivalence problem for DFAs () is decidable. Why can't we just use the same method for CFGs?

The solution for DFAs was constructive. Given two DFAs, and , we could construct a third DFA, , that accepted the symmetric difference of their languages:

The languages and are equivalent if and only if is empty. Since emptiness is a decidable problem for DFAs, we could decide equivalence.

This entire strategy hinges on the fact that regular languages are closed under complement (), intersection (), and union ().

This is where the strategy breaks down for Context-Free Languages.

Undecidable Problems for Context-Free Languages

These lecture notes provide a concise explanation for why the methods used for regular languages are not applicable to CFLs.

Read the 'Equivalence Problem' subsection on page 7. Focus on the core argument: the symmetric difference construction requires closure under intersection and complement, properties that CFLs lack.

As the notes explain, CFLs are not closed under intersection or complement. A classic example is:

  • is a CFL.
  • is also a CFL.
  • But their intersection, , is the canonical example of a language that is not context-free.

Since we cannot reliably construct a CFG for the symmetric difference, we need a completely different approach to assess the decidability of . This leads us back to our powerful tool: proof by reduction.


2. The Undecidability of

We will prove that is undecidable using a two-step reduction:

  1. First, we'll show that the Universality Problem for CFGs () is undecidable.
  2. Then, we'll show that reduces to .

This establishes the chain of reasoning: . Since is undecidable, must also be undecidable.

Step 1: The Universality Problem () is Undecidable

The universality problem asks whether a given CFG generates all possible strings over its alphabet:

To prove this is undecidable, we will reduce to it. The core of the reduction is this: for any Turing Machine and input , we will construct a CFG, , that generates the language of all strings that are NOT an accepting computation history of on .

To formalize this, let's first define a computation history.

Undecidable Problems for Context-Free Languages (Redux)

These notes provide the formal definitions needed to understand the proof.

Read 'Definition 16 (Computation history)' on page 8 to understand what a computation history is. Then, read the proof sketch for 'Theorem 17. UCFG is undecidable' on pages 8-9. Focus on understanding the three conditions that define a non-accepting computation history and the clever trick of reversing every other configuration.

The logic of the reduction is as follows:

  • If accepts : There is exactly one accepting computation history. Our constructed grammar generates every string except this one. Therefore, .
  • If does not accept : There is no accepting computation history. Therefore, the set of non-accepting computation histories includes all strings. Thus, generates everything, and .

So, if and only if does not accept . A decider for could be used to decide (by inverting the answer). Since is undecidable, must be too.

The ingenious part, as described in the notes, is how a CFG (or more intuitively, a PDA) can generate this language. A string fails to be an accepting computation history if:

  1. It doesn't start with the correct initial configuration.
  2. It doesn't end in an accepting configuration.
  3. An intermediate configuration does not correctly yield .

A PDA can nondeterministically guess which of these flaws a string has. Checking the start and end configurations is easy. The brilliant trick is checking the intermediate transitions. By representing the history as #C1#C2^R#C3#C4^R..., a PDA can use its stack to compare with (the reverse of the next configuration) to see if a valid TM transition occurred. Your experience designing complex systems with validation rules might give you an appreciation for this "local flaw detection" approach. The PDA only needs to find one flaw to accept the string as a "non-accepting history."

Step 2: Reducing to

Now that we have established that is undecidable, the final step is surprisingly straightforward. We will show that if we could decide , we could decide .

Equivalence for Context-Free Grammars is Undecidable and co-Recognizable

This short video clearly explains this elegant reduction.

Watch the segment from 00:50 to 03:22. The video walks through the logic of using a hypothetical decider for EQ_CFG to build a decider for ALL_CFG.

Here is the argument in text:

Assume you have a decider for , let's call it decider_E. We can use it to build a decider for , let's call it decider_A.

decider_A on input :

  1. Construct a simple CFG, , that is known to generate . For an alphabet , could have the single rule .
  2. Run decider_E on the input pair .
  3. If decider_E accepts, it means . So, decider_A accepts.
  4. If decider_E rejects, it means , which implies . So, decider_A rejects.

We have successfully built a decider for . But we just proved that is undecidable. This is a contradiction. The only flawed assumption was that decider_E exists.

Therefore, is undecidable.

Test your understanding!

We proved is decidable and is undecidable. What property of CFLs is the primary reason for this difference, and why does it prevent the proof method from working?

Show answer

The primary reason is that the class of Context-Free Languages is not closed under complementation and intersection.

The proof for relies on constructing a DFA for the symmetric difference of the two languages, which is defined as . This construction is only possible because regular languages are closed under union, intersection, and complement. Since CFLs are not closed under intersection or complement, we cannot guarantee that the symmetric difference of two CFLs is itself a CFL. Therefore, we cannot construct a CFG (or PDA) to recognize it and then check for emptiness.


3. Recognizability and

We've answered the question of decidability, but we can go one step further. Is Turing-recognizable? What about its complement?

Let's look at the complement, , which is the language of pairs of CFGs that generate different languages.

If two languages are different, there must be at least one string that is in one but not the other. This string acts as a "witness" to their inequality. We can search for such a witness.

Equivalence for Context-Free Grammars is Undecidable and co-Recognizable

The same video also offers a clear explanation of why the complement of EQ_CFG is recognizable.

Watch from 03:22 to 09:16. Pay close attention to the construction of the recognizer for the complement and the final conclusion it allows us to draw about EQ_CFG itself.

The recognizer for works as follows:

  1. Take as input .
  2. Systematically generate every possible string over the alphabet .
  3. For each string , use the decider for (the membership problem for CFGs, which is decidable) to check if and if .
  4. If the answers differ for any , then we have found our witness. Accept.
  5. If the answers are the same, continue to the next string.

If , this recognizer will eventually find a witness string and halt. If , it will run forever. This perfectly fits the definition of a Turing-recognizable language.

So, is recognizable. Now, recall a key theorem:

A language is decidable if and only if it is Turing-recognizable and its complement is also Turing-recognizable.

We already proved that is undecidable. Since its complement, , is recognizable, it must be the case that itself is not Turing-recognizable.


Conclusion

This lesson marks a significant point in our study of computability. We've moved beyond problems directly about Turing Machines and used the principle of reducibility to uncover a fundamental limitation in the realm of context-free languages, a cornerstone of compiler design and parsing.

Key Takeaways:

  • The equivalence problem for Context-Free Grammars () is undecidable.
  • The proof strategy involves a reduction from via the undecidable universality problem for CFGs ().
  • The proof for being undecidable relies on constructing a grammar that generates strings that are not accepting computation histories of a TM.
  • The reduction from to is achieved by comparing the input grammar to a standard grammar that generates .
  • Furthermore, is Turing-recognizable, which implies that is not Turing-recognizable.

Preview of the Next Lesson:
We have now reached the end of our exploration of what is and is not computable. In the next module, we will shift our perspective. For problems that are decidable, we will start asking a new question: how efficiently can they be solved? This will lead us into the world of computational complexity, where we will formally define concepts like time complexity, and the famous complexity classes P and NP.