Consider the language $$ \{ w \in \{0,1\}^* : \#_0(w) \ge \#_1(w) \} $$ consisting of all words over $\{0,1\}$ in which the number of zeroes is at least twice the number of ones.

Is this regular, context-free, Turing decidable?

My Idea

It's not regular, because we need to track the number of zeros and so it cannot be done with a finite number of states. I am not sure if this argument is a formal one.

Turing Decidable - Yes

Take a machine which reads 1 from the end, writes an X there and then traverses the tape and X's two 0's.

If after all ones are gone the tape is empty of there are still zeroes left then it's accepted. Otherwise, if there are ones left on tape after this operation, it's rejected.


I am unable to find any context free grammar for it. Neither can I prove that it's not context-free.

  • $\begingroup$ cs.stackexchange.com/q/18524/755, cs.stackexchange.com/q/101162/755, cs.stackexchange.com/q/83450/755 $\endgroup$
    – D.W.
    Commented Apr 20, 2021 at 5:39
  • $\begingroup$ Your argument for showing that the language is not regular is not formal. It's just intuition. To formalize it you can use one of the standard techniques, such as the pumping lemma or Myhill–Nerode theory. $\endgroup$ Commented Apr 20, 2021 at 6:48
  • $\begingroup$ Your language is context-free, which you can show using a pushdown automaton. Constructing a context-free grammar is more challenging. $\endgroup$ Commented Apr 20, 2021 at 6:48
  • $\begingroup$ Do you specifically need a CFG? A PDA is much more easier to construct for this particular language. $\endgroup$
    – kviiri
    Commented Apr 20, 2021 at 7:19

2 Answers 2


The language is context-free. As you read the string, you'll need a counter comparing two values: $\#_0 \cdot 2$ and $\#_1$. Such counter can be implemented using the stack of the PDA.

When a $0$ is read:

  • if there is a $1$ on top of the stack, pop it. If the stack is now empty, add a $0$, otherwise pop again.
  • else add $0$ to the stack twice

When a $1$ is read:

  • if there's a $0$ on top of the stack, pop it.
  • else add $1$ to the stack

It is easy to see* that the stack contains now the difference between $\#_0 \cdot 2$ and $\#_1$, encoded as the number of symbols on stack and the symbol used. After the entire string has been scanned, the top symbol of the stack tells which of the numbers is greater: if the top symbol is $0$ or the empty stack symbol, accept. Otherwise reject.

*If you need a more formal proof of this construction's correctness, it is fairly easy proven by induction: first establish that the symbols of the stack represent the correct counts after reading the first symbol, then perform an induction step with the assumption that the first $n$ symbols resulted a correct count on the stack and showing that the $n+1$th symbol read doesn't violate this correctness.

  • $\begingroup$ ok So push down automata is the way to go about proving that a language is context-free. From the reasoning above Any language where #zero = k* #ones for any k will be context free as we can construct a PDA. And we can use pumping lemma to prove that it's not regular and Since it's Context-Free so it must be Turing decidable. Just writing my thoughts down ... $\endgroup$
    – pensee
    Commented Apr 20, 2021 at 19:19
  • $\begingroup$ @pensee Correct on all counts. Indeed, you can prove a language to be context-free with a PDA as well as a CFG — both ways are equally valid though one might be easier for a given language than the other. $\endgroup$
    – kviiri
    Commented Apr 20, 2021 at 19:45

Let $L$ be your language

$L$ is context free since the following CFG generate all possible strings in $L$

S -> 1KK | KK1 | K1K | K | Ɛ
K -> S0S 

$L$ is decidable since $L$ is context free and all context languages are also decidable

$L$ isn't regular since it doesn't respect the Pumping Lemma:

For a lenght $k$ we can chose the word $w = 0^{2k} 1^k \in L$

Then if we divide $w$ in $xyz$ where:

$|y| > 0$

$|xy| < k$

$y$ must be only made of zeroes, it we chose the exponent $i=0$ we get $xy^0z$ that isn't in $L$, thus $L$ isn't regular.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.