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?
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.
Suppose $\Sigma=\{0\}$, and $\ell_1=\{0^n|n\text{ is prime}\}$, and $\ell_2=\{0^n|n\text{ is not prime}\}$ that neither of them are context-free languages. But the union of them:
$$L=\ell_1\cup\ell_2=\{0^n|n\geq 0\}.$$
Obviously $L$ is context-free.