I need to find a context-free grammar for the following language which uses the alphabet $\{a, b\}$ $$L=\{a^nb^m\mid 2n<m<3n\}$$

  • $\begingroup$ I tried many solutions by ended with nothing, getting more confused $\endgroup$ Mar 28, 2014 at 13:16
  • 3
    $\begingroup$ This is a dump of a problem, not a question. If you have a specific question regarding the wording of the problem or about concrete steps in your own attempts at solving the problem, feel free to edit accordingly and we can reopen the question. See here for a relevant discussion. If you are uncertain how to improve your question, why not ask around in Computer Science Chat? $\endgroup$ Mar 28, 2014 at 13:20
  • $\begingroup$ start with the 'classical' CFG: $ S \to aSb $. Consider expanding into one additional non-terminal B on the rhs instead ($ S \to aSB $). How could the non-terminal help you reaching your goal ? To exclude the bounds of the range expressed by your inequality, for each bound observe by how many symbols you are off at maximum in any derivation your grammar admits. $\endgroup$
    – collapsar
    Mar 28, 2014 at 13:32
  • 1
    $\begingroup$ The correct approach to write grammar for the language is write strings first. Understand n!=0 and 1. n should be >= 2. and for n=2.. possible strings are aabbbbb only for n=3 strings can be aaa bbb bbb b, aaa bbb bbb bb for n=4 strings can be aaaa bbbb bbbb b, aaaa bbbb bbbb bb, aaaa bbbb bbbb bbb So for a n=N no. of possible strings are N-1. So you need rules like. So rules should be whenever you add one a you should add two Xb (where X can be replace by b or ^) as Y --> aYXb Now In first rule I left S to add more as and bs. ... Now --CONTINU.. $\endgroup$ Mar 28, 2014 at 18:46
  • $\begingroup$ Now with min length string that is aabbb start with m > 2n as S --> aaYbbb. Of-Course add X --> b | ^. And To remove Y from sentential from add Y --> a. $\endgroup$ Mar 28, 2014 at 18:48

2 Answers 2


Hint: Can you do $$L=\{a^nb^m\mid m=3n\}$$

Try it also for: $$L=\{a^nb^m\mid m=3n-1\}$$

Then you might want to be able not to always have that many $b$.

And there is a bit more to take care of.


Let's start with a grammar for the language $$ \{ a^n b^m : 2n \leq m \leq 3n \} $$ The idea is that if $c_1,\ldots,c_n \in \{2,3\}$ then $2n \leq c_1 + \cdots + c_n \leq 3n$. We can implement this idea in the following way: $$ S \to aSbb \mid aSbbb \mid \epsilon $$

Now we want to rule out $m = 2n$ and $m = 3n$. We can enforce this by requiring $c_1 = 2$ and $c_n = 3$ (note that when $n \leq 1$, no $m$ satisfies $2n < m < 3n$). This leads to the following grammar: $$ S \to aSbb \mid aSbbb \mid aabbbbb $$

  • 1
    $\begingroup$ You hint explains question but not helpful for writing grammar. I downvoted as I dislike it in answer section. $\endgroup$ Mar 28, 2014 at 18:59
  • $\begingroup$ On the contrary, using this idea I can write a grammar for the language. $\endgroup$ Mar 28, 2014 at 20:10
  • $\begingroup$ @YuvalFilmus Yes, ofcourse I knew that you can write a grammar for the language but I believe you have written this answer for other users. $\endgroup$ Mar 29, 2014 at 5:06
  • $\begingroup$ @Grijesh My policy is not to give away answers. I stand by my stance that my hint contains one basic idea behind the solution. $\endgroup$ Mar 29, 2014 at 5:23
  • $\begingroup$ Yuval I see. Yes I saw many of your answers and I got your idea. But I think this question is little bit hard at-least for new uses. So I request you to add some more details so that it can be helpful to write grammar for average users like me... And I saw two more users are disagree with this kind of hints in answer section on similar question Anyways answer is yours If I disagree I down-vote, If I like vote-up also. my downvotes are very less than up-votes $\endgroup$ Mar 29, 2014 at 5:55

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