Convert this grammar to language

175 Views Asked by At

I want to convert the following grammar to language but I am not able to think and answer.

$$S \to aSa \mid bSa \mid ab \mid ba$$

It gives me a lot of choices when I tried building its derivation tree.

1

There are 1 best solutions below

0
On BEST ANSWER

Welcome to MSE. There is basically one case:

Start either with $S\rightarrow^* a^mSa^m$ or $S\rightarrow^* b^nSa^n$ and alternate and finally replace $S$ either by $ab$ or $ba$. This gives the language $$\{a^{n_1} b^{n_2} \ldots a^{n_{k-1}} b^{n_k} (ab) a^{n_k} a^{n_{k-1}} \ldots a^{n_2} a^{n_1}\mid n_1,\ldots,n_k\geq 0, k\geq 0\}$$ union with $$\{a^{n_1} b^{n_2} \ldots a^{n_{k-1}} b^{n_k} (ba) a^{n_k} a^{n_{k-1}} \ldots a^{n_2} a^{n_1}\mid n_1,\ldots,n_k\geq 0, k\geq 0\}.$$