A problem I came across was:
Design a CFG for the language $\{a^ib^jc^k\,|\,i=j+k \}$
The solution I came up with :
$S\rightarrow aSc\,|\,S_1$
$S\rightarrow aS_1b\,|\,\epsilon$
It took more than an hour to come up with solution and there are many more to solve.
What is the best way to tackle such problems?
How would you break this problem into a word problem?
EDIT: How I broke it down was :
- $a$ is followed by $b$, $b$ is followed by $c$
- $a$ = $b$ for $k$ = $0$ OR $a>b,c$ for $j,k>0$
Here I would notice that if $b$ and $c$ were the same symbol, this language would be just $\{a^kb^k:k\ge 0\}$, which is generated by $S\to aSb\mid\epsilon$. However, $b$ and $c$ are not the same, so somehow instead of generating $b$s to the right of the $S$, I need to generate a mixture of $b$s and $c$s. If I didn’t care about their order, I could replace $S\to aSb$ by $S\to aSX$ and add the productions $X\to b\mid c$.
Unfortunately, I do care about their order: I have to generate $c$s for a while, and then $b$s. That suggests replacing $S$ with two new non-terminals, say $C$ generating $c$s and $B$ generating $b$s, with a switch from $C$ to $B$ at some point:
$$\begin{align*} &S\to aCc\mid aBb\mid\epsilon\\ &C\to aCc\mid aBb\mid\epsilon\\ &B\to aBb\mid\epsilon\;. \end{align*}$$
Now I realize that I can simplify this a bit: $S$ and $C$ have the same productions and might as well be the same non-terminal.
$$\begin{align*} &S\to aSc\mid aBb\mid\epsilon\\ &B\to aBb\mid\epsilon\;. \end{align*}$$