For two languages over the same alphabet, if neither of them is context-free, can their union still be context-free?

If not, does one of the languages need to be context-free for this to happen? Do both need to be context-free?


Yes, and this works for regularity and decidability as well! (not that this is very helpful in any sense)

For example, take any problem $A$ such that $A$ and its complement $\overline{A}$ are not CFLs (the halting problem as an extreme example)

Their union is $\Sigma^*$ which is obviously a CFL.

As a side note, and thanks for @D. Ben Knoble for pointing this out in the comments, that this is only an example. It does not necessarily hold for any two non-CFL languages, and on the contrary also a union of two CFL's may (not always!) end up not being a CFL.

  • $\begingroup$ In fact, you can take two languages that are not even recognizable and their union will be regular, using the same trick as here $\endgroup$ – nir shahar Apr 22 at 7:31
  • $\begingroup$ thank you! I really appreciate it. Another question: would this also be true for the intersection of two languages over the same alphabet, if neither of them are context-free? $\endgroup$ – Ayaz Vural Apr 22 at 13:43
  • 2
    $\begingroup$ A word of caution for future readers: this does not mean that all such unions are CF or regular; only that there exists some cases where the union is regular. Said another way, it is not the case that the union of any two non-CF languages is always non-CF. $\endgroup$ – D. Ben Knoble Apr 22 at 13:43
  • 1
    $\begingroup$ Thanks for pointing this out. I added this into the answer so people who read it will notice it (because honestly, who reads comments :o ) $\endgroup$ – nir shahar Apr 22 at 14:25
  • $\begingroup$ @AyazVural: Take two languages which are disjoint. Or take any two non-CF languages $L_1$ and $L_2$ and produce the non-CF languages $L'_1 = aL_1$ and $L'_2 = bL_2$, where $a$ and $b$ are two different symbols in the alphabet. Since the languages are disjoint, their intersection is the empty language, which is trivially regular. (Another trivial case is the one where the intersection of the two languages is finite. Any finite set is regular.) $\endgroup$ – rici Apr 22 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.