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\}$$
Asked
Active
Viewed 5,429 times
2
2 Answers
1
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.

babou
- 19,445
- 40
- 76
0
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 $$

Yuval Filmus
- 276,994
- 27
- 311
- 503
-
1You hint explains question but not helpful for writing grammar. I downvoted as I dislike it in answer section. – Grijesh Chauhan Mar 28 '14 at 18:59
-
On the contrary, using this idea I can write a grammar for the language. – Yuval Filmus Mar 28 '14 at 20:10
-
@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. – Grijesh Chauhan Mar 29 '14 at 05:06
-
@Grijesh My policy is not to give away answers. I stand by my stance that my hint contains one basic idea behind the solution. – Yuval Filmus Mar 29 '14 at 05:23
-
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 – Grijesh Chauhan Mar 29 '14 at 05:55
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. – collapsar Mar 28 '14 at 13:32n
!=0 and 1. n should be >= 2. and for n=2.. possible strings areaabbbbb
only for n=3 strings can beaaa bbb bbb b
,aaa bbb bbb bb
for n=4 strings can beaaaa bbbb bbbb b
,aaaa bbbb bbbb bb
,aaaa bbbb bbbb bbb
So for an=N
no. of possible strings areN-1
. So you need rules like. So rules should be whenever you add onea
you should add twoXb
(whereX
can be replace byb
or^
) asY --> aYXb
Now In first rule I leftS
to add morea
s andb
s. ... Now --CONTINU.. – Grijesh Chauhan Mar 28 '14 at 18:46aabbb
start with m > 2n asS --> aaYbbb
. Of-Course addX --> b | ^
. And To removeY
from sentential from addY --> a
. – Grijesh Chauhan Mar 28 '14 at 18:48Y --> aYXb
should beY --> aYXbb
as I am saying add twob
s from onea
. -- So to summarize my idea: Start with one extraa
, latter add twob
for onea
. AddX
so that you can add eitherb
or^
to keepb
's between 2n to 3n (note you always have more than 2nb
) – Grijesh Chauhan Mar 28 '14 at 19:14