2

I'm pretty sure I've actually got one, but it has 42 construction rules and doesn't generalize well. How can I do it with fewer construction rules?

The language is {a,b}* where the number of a's is five times the number of b's.

I know that for a language {a^n (concatenate) b^m; m = 5n} it would just be

S = aSbbbbb | λ

But when the characters can be in any order, I'm lost.

Aurast
  • 87
  • 1
  • 8

1 Answers1

4

First of all, observe that if a sentence has 5 times as many characters as the other, it'll always look something like aaabaabaaaaa. So one sentence can be aaaaab or aaabaa. Another observation is that whenever we add a b, we must add five a characters.

The following grammar indeed has five times as many a characters as b characters:

S = AS | λ
A = Baaaaa | aBaaaa | aaBaaa | aaaBaa | aaaaBa | aaaaaB
B = bS | Sb

We start with S which can either by empty (which satisfies the requirement) or A.

The rule for A produces at least 5 a characters and a B. Now for B, we can either place b and stop there (by choosing the empty string for S) or by starting again (by choosing A for S). This guarantees that we're always placing 5 times as many a characters as b characters.

Lastly, this grammar can easily be generalized to a grammar than needs to contain n times as many characters of one as the other (by straightforwardly extending rule A).

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
  • 1
    I don't know what you just said, but you used a character I don't know how to do with a keyboard, so it must be right. +1 ;) – hvgotcodes Mar 21 '12 at 19:32
  • Thanks for the answer! But I am not sure that this is correct. I can not figure out a way to derive, for example, baaaaaaaaaab from those rules. My apologies if I am overlooking something. Can you see a way to derive baaaaaaaaaab? It seems only possible to get a b on one end of the string. – Aurast Mar 21 '12 at 20:04
  • You're right, I couldn't find a way to derive either `baaaaaaaaaab`. I've adjusted the rule for `S` to make it possible: `S` = `AS` = `AAS` = `AA` = `BaaaaaA` = `BaaaaaaaaaaB` = `bSaaaaaaaaaaB` = `bSaaaaaaaaaabS` = `bSaaaaaaaaaab` = `baaaaaaaaaab`. – Simeon Visser Mar 21 '12 at 20:15
  • Beautiful, I think it works. Thanks! Don't think I ever could have come up with that >. – Aurast Mar 21 '12 at 20:32