I'm trying to understand the approach to constructing an grammar which accepts the language ${a^ib^j \mid i>0\ and \hspace{2.5mm}i\leq j \leq(2*i)}$ } Thanks.
-
$\begingroup$ What have you tried? Where did you get stuck? We do not want to just hand you the solution; we want you to gain understanding. However, as it is we do not know what your underlying problem is, so we can not begin to help. See here for tips on asking questions about exercise problems. If you are uncertain how to improve your question, why not ask around in Computer Science Chat? $\endgroup$– RaphaelCommented Feb 3, 2019 at 20:50
1 Answer
Short answer: unfortunately, it can't be done.
It can be proven mathematically that this language isn't regular. The easiest way is a fooling set argument: the set $\{a^{3^i} \mid i \in \mathbb{N}\}$ is infinite, and for any two distinct strings $x$ and $y$ you take from this set, I can construct a suffix $z$ such that $xz$ is in your language and $yz$ is not.
Thus, it's utterly impossible to make a regular grammar for this language.
Note that this language is context-free; the following context-free grammar (CFG) recognizes it:
$$S \rightarrow aXb \mid aXbb \\ X \rightarrow aXb \mid aXbb \mid \varepsilon$$
(The distinction between $S$ and $X$ is just to ensure that $i > 0$; if you drop that requirement, you can do this in one line.)
EDIT: Thanks to Apass.Jack, here's a one-liner for you anyway!
$$S \rightarrow aSb \mid aSbb \mid ab \mid abb$$
-
$\begingroup$ Nice answer. Here is a oneliner. $S\rightarrow aSb \mid aSbb \mid ab\mid abb$ $\endgroup$– John L.Commented Jan 30, 2019 at 3:32
-
$\begingroup$ @Apass.Jack True, that works too! $\endgroup$– DraconisCommented Jan 30, 2019 at 3:32
-
$\begingroup$ Note that "problem dumps" such as this can often be answered by referring the asker to one of our reference questions. $\endgroup$– RaphaelCommented Feb 3, 2019 at 20:50