TITLE: How to create a grammar for complement of $a^nb^n$?
QUESTION [11 upvotes]: I've got a language L:
$$
\Sigma = \{a,b\} , L = \{a^nb^n | n \ge 0 \}
$$
And I'm trying to create a context-free grammar for co-L.
I've created grammar of L:
P = {
  S -> aSb
  S -> ab | epsilon
}

In co-L, I don't know how to ensure, that there won't be the same number of a,b. Should I create something like this?
P = {
  S -> aSb
  S -> a | b | aS | bS
}

REPLY [3 votes]: We can break this language into the union of several simpler languages:
L = { $a^i b^j$ | i > j } ∪ { $a^i b^j$ | i < j } ∪ $(a ∪ b)^∗b(a ∪ b)^∗a(a ∪ b)^∗$.
That is, all strings of a’s followed by b’s in which the number of a’s and b’s differ, unioned with all strings not of the form $ a^ib^j$.
First, we can achieve the union of the CFGs for the three languages:
S → $S_1|S_2|S_3$
Now, the set of strings { $a^ib^j$ | i > j } is generated by a simple CFG:
$S_1 → aS_1b|aS_1|a$
Similarly for { $a^ib^j$ | i < j }:
$S_2 → aS_2b|S_2b|b$
Finally, $(a ∪ b)^∗b(a ∪ b)^∗a(a ∪ b)^∗$ is easily generated as follows:
S3 → XbXaX
X → aX|bX|ϵ

Source: http://cseweb.ucsd.edu/classes/su99/cse105/sample2.pdf