-1

I have the language:

$$ L = \{0^m1^n \mid 0 ≤ m ≤ n\text{ or }0 ≤ n ≤ 2m\}. $$

My goal is to give an equivalent context-free grammar for this language, but I am unsure if I am going about it the right way. So far this is what I've come up with:

$$ \begin{align*} &S \to A \mid X \\ &A \to 0A1 \mid A1 \mid \varepsilon \\ &X \to 00A1 \mid 00A \mid \varepsilon \end{align*} $$ Is this anywhere close?

Raphael
  • 72,336
  • 29
  • 179
  • 389
bob afro
  • 117
  • 1
  • 7

1 Answers1

0

Yes it is pretty close but a little far, correct solution will be (if you go about writing grammar methodically)

$S \rightarrow A \ | \ B$
$A \rightarrow 0A1\ |\ A1 \ |\ \epsilon$
$B \rightarrow 0B11\ |\ 0B \ |\ \epsilon$

Otherwise, if you go by logic, condition $0 \leq m \leq n$ OR $0 \leq n \leq 2m$ is true for every non-negative integer pair $m$ and $n$ (because $m \leq n$ OR $n \leq m$ translates to $m \leq n$ OR $n \leq m \leq 2m$, which is always true). Hence your language is really $\{0^m1^n\ |\ m\geq 0, n\geq 0\}$ and the grammar is:

$S \rightarrow 0S \ | \ S1 \ | \ \epsilon$

Sarvottamananda
  • 4,817
  • 1
  • 13
  • 19