Context-free grammars a*(ba*ba*)*

800 Views Asked by At

I'm having trouble to find context free grammars which generate following languages a* (ba* ba*)*

* means zero to infinite

My solution is : S--> aS | bS | ε

Is that right?


Accroding to the comment I modify my solution

S → AbAbA | ε

A → aA | ε

or

S → aS | SbSbS | ε

Do both of them are correct ?

1

There are 1 best solutions below

2
On BEST ANSWER

Your grammar generates every possible string over the alphabet $\{a,b\}$. For instance, it generates $aabaa$ as follows:

$$S\to aS\to aaS\to aabS\to aabaS\to aabaaS\to aabaa$$

The regular expression $a^*(ba^*ba^*)^*$, on the other hand, generates only strings with an even number of $b$s. Specifically, it generates strings of the form $a^k(ba^\ell ba^m)^n$ for non-negative integers $k,\ell,m$, and $n$, and it’s easy to see that there $b$ occurs $2n$ times in such a string. (In fact the regular expression generates all of the words with an even number of $b$s, though it takes a little more work to verify that.)