0

I would like to know if someone can help me designing a context free grammar

for a language where { w | |w|a=2|w|b }

for example w=aab , aaaabb , aaaaaabbb ,baa , aba , aabbaaaba ...

S-> aab | baa | aba | SS | abSa | baSa | aaSb | bSaa would not generate aaabba.

So my next question is , isn't it too ambiguous to have a grammar that looks like this ->

**

S-> aab | baa | aba | aSab | aSba | aaSb |abSa |aabS | abaS | Saab | Saba | Sbaa | SS | bSaa | baSa | baaS ?

**

Thank you in advance

Zok
  • 355
  • 2
  • 15

2 Answers2

1

None of the grammars you posted can product aaabba, you need something like this:

S-> HaSa | aHSa | aSHa | aSaH | HSaa | SHaa | SaHa | SaaH | HaaS | aHaS | aaHS | aaSH | epsilon

H -> b

It can probably be done with a shorter grammar, but I think this will do.

user2268997
  • 1,263
  • 2
  • 14
  • 35
  • Okay you are right I tried and I couldn't find a way to produce aaabba , but I have a question here , Shouldn't the epsilon be there ? Because S would produce epsilon and this word doesn't follow the mentioned condition , which is #a=2#b ? – Zok Oct 26 '15 at 06:13
  • It does actually `0 = 2 * 0` – user2268997 Oct 26 '15 at 07:20
1

Try this one:

S  -> ε | S1 S
S1 -> ε | aaS1bS1 | bS1aaS1 | aS1bS1a
Psycarlo
  • 11
  • 4