This problem was taken from A. Shen's book "Algorithms and Programming. Problems and Solutions". The problem itself was communicated by M. Sipser.
The author asks the reader to define a context-free grammar which generates the following language:
{X2Y | X ∈ {0, 1}*, Y ∈ {0, 1}*, X ≠ Y}
.
First of all, I cannot understand how such a language could be context-free (looking from my newbie-perspective):
Both X
and Y
can be any sequence whatsoever, but they cannot be one sequence at the same time. This seems like a context-sensitive property to me.
What is the real meaning behind the terms "context-free" and "context-sensitive", which does not contradict the language above being context-free? How can one construct such a grammar (I would really appreciate a hint instead of a full solution)?