1

need help regarding context free grammar. I want a cfg in which letter b is never tripled. This means that no word contains the substring bbb in it. Language contains only letters {a,b}

Haris Akhtar
  • 13
  • 1
  • 5

1 Answers1

1

Here is a helpful recursive definition of this language:

  • the empty string is in the language
  • b is in the language
  • bb is in the language
  • if x is a string in the language, then xa is in the language
  • if x is a string in the language, then xab is in the language
  • if x is a string in the language, then xabb is in the language
  • nothing else is in the language unless by the above rules

First, let's make sure this definition is right. It is not hard to argue that this definition defines strings that must be in our target language; no string with the substring bbb can satisfy the above rules. Does the definition cover all cases so that all strings without the substring bbb work? In fact, it must. Consider any string in the language. It either has length less than three (in this case, we can check all possible strings are treated correctly, which they are); for longer strings, they must end with a, ab or abb (they cannot end with bbb). Our rules imply the existence of a string x in the language without these suffixes, which can be recursively checked for membership. This can be reversed to yield a convincing proof by mathematical induction.

With a recursive definition like the above in hand, we can just write down the corresponding grammar:

S -> e | b | bb | Sa | Sab | Sabb

The real ingenuity here was getting the definition. How did I do that? I thought of the shortest strings in the language - the unique ones, that don't fit a pattern - and then I asked how to make longer strings from shorter ones. That is, given a string in the language, how do you make one bigger? That's the key to what context-free grammars let us do.

Patrick87
  • 27,682
  • 3
  • 38
  • 73