Context free grammar $L=\{a^ib^jc^k|j=i+k-2\}$

2.6k Views Asked by At

$L=\{a^ib^jc^k|j=i+k-2\}$

This expression surprise me a lot and put me into deep thinking. what i am doing by solving the expressions:

         j+2 = i+k
         i= j-k+2

         a^j-k+2 b^j c^k
         a^j a^-k a^2 b^j c^k
         .........

feeling astonish because it is not possible having string like S^-p. So any idea that could help to understand?

2

There are 2 best solutions below

1
On

Hint: $a^ib^jc^k = a^ib^{i+k+2}c^k=a^ib^ib^2b^kc^k$

Now it should be easy.

1
On

There are two $\mathtt a$s or $\mathtt c$s that don't correspond to any $\mathtt b$. And there are only three ways to split those two into a number of $\mathtt a$s and a number of $\mathtt c$s, so we can write $$ L = \{\mathtt a^{2+i}\mathtt b^{i+j}\mathtt c^j\mid i,j\ge 0\} \cup \{\mathtt a^{1+i}\mathtt b^{i+j}\mathtt c^{j+1}\mid i,j\ge 0\} \cup \{\mathtt a^{i}\mathtt b^{i+j}\mathtt c^{j+2}\mid i,j\ge 0\} $$ where each of the summands is easy to write a grammar for.