I'm doing a little bit of research about context-free languages. A question that's popped up is whether or not there exists an unambiguous context-free language whose complement is not a context-free language.

I know that the complement of a context-free language *in general* is not necessarily context-free. For example, the language $\{a^m b^n c^p \mid m \ne n \text{ or } n \ne p\}$ is context-free, but its complement, $\{a^m b^n c^p \mid m = n = p\} \cup \overline{a^* b^* c^*}$, is not. I don't see any reason why the complement of an *unambiguous* context-free language would have to be context-free.

On the other hand, if I understand correctly, the complement of a *deterministic* context-free language must be a deterministic context-free language; you can get from one automaton to the other just by swapping its accept and reject states. So if there is an unambiguous CFL whose complement is not CF, then it must be non-deterministic.

The most straightforward example of an unambiguous non-deterministic CFL seems to be the language of palindromes of even length, as given by Wikipedia. But the complement of this language seems to be unambiguous as well, having the following grammar:

$$\begin{align*}S &\to T \mid O\\ T &\to aTa \mid bTb \mid U\\ U &\to aVb \mid bVa\\ V &\to aVa \mid aVb \mid bVa \mid bVb \mid \epsilon\\ O &\to aV \mid bV\end{align*}$$

### Further thoughts

I came up with another unambiguous CFL whose complement didn't *seem* like it would be context-free. Upon further reflection, I realized that the language was context-free after all.

Consider the language $D$ defined by $S \to 0S1S \mid \epsilon$ (the language of strings of correctly matched parentheses), and the language $E$ of non-empty even-length palindromes. Both $D$ and $E$ are unambiguous CFGs, and they are disjoint, so $D \cup E$ is also an unambiguous CFG. My reasoning at this point was that a push-down automaton would be unable to recognize $\overline{D} \cap \overline{E}$, because it would have to keep track of two stacks' worth of information.

I then realized that it is indeed possible to create a nondeterministic PDA recognizing this language. Every string in $D$ begins and ends with a different symbol, and every string in $E$ begins and ends with the same symbol. Therefore, the automaton can nondeterministically choose between the following:

- Accept if the string begins and ends with a different symbol, but isn't in $D$; reject otherwise.
- Accept if the string begins and ends with the same symbol, but isn't in $E$; reject otherwise.