How can I achieve this. Write a grammar for the language consisting of strings built only of the letters a and b. The strings may have any number of these letters, but the letter combinations bab must be in each string somewhere, and each string must start with a bb. For example, the strings bbababbbaa, bbbaaaababaa, and bbbab are in the language, while a, aabb, baaa, and bbba are not.

  • 1
    $\begingroup$ Your language is regular. You can design an NFA for your language, and then convert it to a context-free grammar. $\endgroup$ May 16, 2019 at 20:05

1 Answer 1

  1. The string much start with $ bb $ .
  2. There should at least one occurrence of the sub-string $ bab $

Hence, the structure of the regular expression must be something like this: $$ bb[some \: string]bab[some \: string] $$

and, we can define $ [some \: string] $ something like this:

$$ (a + b)^* $$

which means, there can be any number (including 0) of either a's or b's. Therefore, the final regex for the word is, $$ bb(a+b)^*bab(a+b)^* $$

  • 3
    $\begingroup$ Doesn't match $bbab$. $\endgroup$
    – rici
    May 16, 2019 at 20:32
  • $\begingroup$ Thank you! I figured it out now. $\endgroup$
    – Lizzy
    May 16, 2019 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.