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 6,534 times
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,645
- 43
- 77
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
- 280,205
- 27
- 317
- 514