Question

this is the Language

L = {a^i b^j a^i b^k | i, j, k >= 0}

Then, I can to try a grammar for this Language:

S -> ABCD  
A -> a | aA | lambda  
B -> b | bB | lambda  
C -> a | aC | lambda  
D -> b | bD | lambda  

It's context-free...is it right the grammar ?

Was it helpful?

Solution

Your grammar allows aaba, which it shouldn't, as there should always be a even number of as:

ABCD
aABCD
aaBCD
aabCD
aabaD
aaba

A correct answer would be:

Start → A B

A → a A a | B
B → bB | ε
  • B generates any number of bs (including none).
  • A is a sequence of a, followed by B, followed by the same number of a
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top