4
$\begingroup$

I had to make a context free grammar for the following language: $$ \{a^m b^n \mid 1 \le m \le n \le 2m\} $$

What I thought is: \begin{align*} S &\to aXbb \\ X &\to a|aab|abb|ab|E \end{align*}

Is this correct way of writing a CFG?

  • 0
    Your grammar allows only five words: aabb, aaabbb, aabbb, aabbb, abb. Note that, in addition to missing longer words, you are also missing the short word ab.2012-06-17

4 Answers 4

4

CFG should be $$S\rightarrow aSb|aSbb|ab|abb$$ where $S$ is the start symbol.
So for example if you have to produce the word $a^mb^n$ then as we know $m\leq n\leq 2m$ then you will use $2n-m$ times this production ($S \rightarrow aSb$) and $m-n$ times this production ($S \rightarrow aSbb$). hence you will get $(2n-m)+(m-n) \times a$ and $(2n-m)+2(m-n)\times b$ which is desired.

Again if $m\leq n\leq km$ then CFG would be $$S\rightarrow aSb|aSbb|.....|aS\underbrace{bb....b}_{\text{k times}}|ab|abb|......|a\underbrace{bb....b}_{\text{k times}}$$ and here also logic is same.

  • 0
    There's a small oversight: the empty string is a word in your grammar.2012-06-17
  • 0
    yes the empty string cannot be there. so that needs to be taken care of.2012-06-17
  • 0
    We could modify it to be S -> aAb and A-> aAb, aAbb and empty string2012-06-17
  • 0
    @Richia: the underlying idea will work, but don't forget that `abb` is in your grammar....2012-06-17
  • 0
    @RichaSachdev if you are talking about this $$S\rightarrow aAb \\ A\rightarrow aAb|aAbb|\epsilon$$ then how will you produce $abb$ which is in the language2012-06-17
  • 0
    @RichaSachdev well you can do $S \rightarrow aAB|aAbb$ and else is same But if written in short it will be the same as I have written in my answer2012-06-17
  • 0
    @SaurabhHota: But I cannot include empty string so I can include in my definition A in addition to all also just goes to b. Then I can have abb. Do you think the definition is complete now2012-06-17
  • 0
    @RichaSachdev yes now your grammar is complete and sound.2012-06-17
  • 0
    I think that there is a simpler grammar: $S\rightarrow aSbV|\epsilon $ $V\rightarrow b|\epsilon $2015-04-20
5

It's interesting to see so many different ideas. My own hint is that we can produce words by "creating" as and bs iteratively, and each step we can do one of two things: create one a and one b, or create one a and two bs.

(be aware that each answer so far is leading towards a different presentation of the grammar: you can't combine the hints directly, and should probably only think about one at a time)

  • 0
    Well, I don't see how this is different. You may render the grammar in different ways, but the idea is the same: with every $a$ you generate one or two $b$s; only you decide on this right away, Ayman delays the substitution $B \to b$ for later, and my solution forces the order in some particular way (the multiplication of $b$s is commutative after all). For me it is exactly the same. +1 for the comment in parentheses -- I can imagine that one could easily miss that.2012-06-17
  • 0
    The actual execution of our three answers are different, and I get the vague impression that we went through very different thought processes to arrive at our answer. It looks like you had a very algebraic approach to the problem. When I read Ayman's, I get a vague impression of a general technique of inserting intermediate symbols to allow and constrain further choices in a construction. Neither idea crossed my mind when I looked at the problem.2012-06-17
4

It's not correct. Consider the word $a^4b^5$. This word belongs to the language because $m=4$, $n=5$ and $1 \le 4 \le 5 \le 2 \times 4$. However, your grammer cannot produce it, as it can produce three "$a$"s at most.

Here is a hint: Make $S$ produce one $a$, one $b$ and one $B$ recursively. Make $B$ produce one $b$ at most. This ensures that $1 \le m \le n$, and $n \le 2m$.

  • 0
    I can have this.2012-06-17
  • 0
    Whats if I change to S-> S1 S2 and S1-> ab S2-> aXbb and X-> aX|aaXb|aXbb|aXb|E. Will this be too long?2012-06-17
  • 0
    Among the words that grammar produces are ababb and abaaaaaaaaaaaaaaaabb.2012-06-17
  • 0
    @RichaSachdev - Still incorrect, I'm afraid. This grammar can generate the word $ababb$.2012-06-17
4

Hint: $$a^m b^n = a^{2m-n}a^{n-m}(bb)^{n-m}b^{2m-n}$$