Construct grammar $\ a^i b^j c^{i+j} b^j a^i $

189 Views Asked by At

I've been going through old exams at my college and I found this problem that I haven't yet been able to solve.

Construct grammar defined on the alphabet $\ \{{a, b, c}\} $ which generates strings of the form $\ a^i\ b^j\ c^{i+j}\ b^j\ a^i\ where\ i, j \geq 0. $ It doesn't say anything which form the grammar should be; that is it can be context-free, restricted, unrestricted... anything goes.

1

There are 1 best solutions below

3
On BEST ANSWER

$$\begin{align*} &S\to TY\\ &T\to aTA\mid U\\ &U\to bUB\mid ZV\\ &VB\to Wb\\ &bW\to Wb\\ &ZW\to cZV\\ &Vb\to bV\\ &VA\to Xa\\ &aX\to Xa\\ &bX\to Xb\\ &ZX\to cZV\\ &VY\to C\\ &aC\to Ca\\ &bC\to Cb\\ &ZC\to\lambda \end{align*}$$

$Y$ marks the end of the string. $T$ generates something of the form $a^nUA^n$; the $A$s will eventually become $a$s. The $U$ generates something of the form $b^mZVB^m$; the $Z$ marks where the $c$s will go, and the $B$s will eventually become $b$s. At this point we have $b^ma^nZVA^nB^mY$.

The $V$ runs across the string to the right until it encounters a $B$; it turns that into a $b$ and turns itself into $W$, which runs back to the left until it hits the $Z$. At that point it deposits a $c$ to the left of the $Z$ and turns back into a $V$. The $V$ runs to the right through any $b$s that have been produced until it hits either a $B$ or an $A$. In the former case it behaves as previously described. In the latter it turns the $A$ into an $a$ and itself into $X$. The $X$ then runs left through $a$s and $b$s until it hits the $Z$, at which point it deposits a $c$ to the left of the $Z$ and turns into a $V$.

Eventually the $V$ running to the right must hit the $Y$. We collapse the $VY$ to $C$, which runs left until it encounters the $Z$, and the $C$ and $Z$ undergo mutual annihilation.