# Union of non-context free languages

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?

## 1 Answer

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.

• In fact, you can take two languages that are not even recognizable and their union will be regular, using the same trick as here – nir shahar Apr 22 at 7:31
• 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? – Ayaz Vural Apr 22 at 13:43
• 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. – D. Ben Knoble Apr 22 at 13:43
• 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 ) – nir shahar Apr 22 at 14:25
• @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.) – rici Apr 22 at 22:17