2

I have come across two completely different answers.

One says that:

Yes, there does exist a context-free grammar for {0i1j | 1≤i≤j≤2i}, the following grammar ensures that there can be half or lesser 0’s for the 1’s that are present:

S -> 0S11 | 0S1 |  01

The other: No, proof by contradiction:

Case 1:

Suppose you push i 0s onto the stack.

Pop off j 1s.

You can’t determine if j<=2i.

Case 2:

Suppose you push 2i 0s onto the stack.

Pop off j 1s.

You can’t determine if j>=i.

Any other value pushed on the stack not equal to i or 2i is a value relative to either of these two values, so the same reasoning applies.


Are either correct? Thanks so much!

rici
  • 234,347
  • 28
  • 237
  • 341
rgs
  • 125
  • 1
  • 6

1 Answers1

3

Since a grammar exists and you can pretty clearly check it matches the whole language, the language must be context-free. So the proof by contradiction is wrong. But why?

The proof assumes the machine must be deterministic. But you need a nondeterministic pushdown automata to recognize some context-free grammars. Thus, all the second proof proves (if it is correct) is that the language isn't a deterministic context-free language, but it doesn't show that it isn't a context-free language.

Indeed, if you let the machine be nondeterministic, then basically you push i 0s, then for each 0 on the stack, nondeterministically pop 1 or 2 1s. One of the computations will accept if the string is in the language.

nneonneo
  • 171,345
  • 36
  • 312
  • 383