2

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)?

Zhiltsoff Igor
  • 1,812
  • 8
  • 24

1 Answers1

2

Since you asked for a hint, I'll give you a hint rather than writing the whole answer out. The difficulty here is that we need X and Y to be different strings. We know that X2Y with X and Y the same is NOT context-free. This is because we have |X| = |Y| things to check - the first symbols match, the second symbols match, etc. However, if X and Y must be different, we only need to guarantee they differ in at least ONE place. If we can guarantee they differ in symbol number N, then we have guaranteed X and Y are different. Can you write a context-free grammar that generates (0+1)^n 0 (0+1)* 2 (0+1)^n 1 (0+1)*? If so, the answer to your question is the union of that CFG's language and the language of the one that has the mismatched symbols exchanged (so 1 comes first, then 0).

Patrick87
  • 27,682
  • 3
  • 38
  • 73
  • So, basically, if we have 2 grammars that guarantees that the prefixes (or postfixes in case of the 2nd grammar) which are shorter than the whole string are equal, we can combine them into the grammar we want? I’ve already written those 2 grammars, yet I failed to do this last step, for some reason. Did I describe it correctly? – Zhiltsoff Igor Dec 04 '19 at 14:02
  • @ZhiltsoffIgor We don't have to guarantee anything is equal, just that the strings differ in at least one position. So you need to make sure that after arbitrary prefixes of length N, for some N, there is a mismatched symbol. You don't have to prevent mismatched symbols from occurring EARLIER than the one you guarantee. – Patrick87 Dec 04 '19 at 14:30
  • @ZhiltsoffIgor I don't think it's wrong, I just don't know that you're going to be successful in ensuring the prefixes are equal up to the mismatched symbols... seems like that might make it hard or impossible to get a right answer. Recall that X2Y has no CFG if X=Y... neither does X02Y1 if X=Y – Patrick87 Dec 04 '19 at 15:02
  • Yes, sorry, I made a mistake. So, the grammar I've come up with: `G = ({S; N; M; E}, {0; 1; 2}, {S -> N0E; S -> M1E; N -> 0N0; N -> 0N1; N -> 1N0; N -> 1N1; M -> 0M0; M -> 0M1; M -> 1M0; M -> 1M1; N -> 1E2; M -> 0E2; E -> Λ; E -> 0E; E -> E1}; S)` (Here I use `Λ` to denote an empty string). Basically, `N` initializes a derivation where the first word would get the `1` and the second word - the `0`. Same goes for `M`, but vice versa. E simply generates a random binary word. Is this correct? – Zhiltsoff Igor Dec 04 '19 at 19:50
  • 1
    @ZhiltsoffIgor Edit - actually I think your answer is basically right. Just check edge cases like the length of LHS and/or RHS is zero and you should be good. – Patrick87 Dec 04 '19 at 20:53
  • I figured that was what your answer implied. Anyway: 01200: `S -> N0E -> 0N00E -> 01E200E -> 01200E -> 01200`; Yes, I've forgotten about the empty strings. I believe, one could just suggest that inequality to an empty string is different from our main idea, thus, derive them directly from the axiom. Taking that into account, here's the new grammar: `G = ({S; N; M; E}, {0; 1; 2}, {S-> 12; S -> 02; S -> 21; S -> 20; S -> N0E; S -> M1E; N -> 0N0; N -> 0N1; N -> 1N0; N -> 1N1; M -> 0M0; M -> 0M1; M -> 1M0; M -> 1M1; N -> 1E2; M -> 0E2; E -> Λ; E -> 0E; E -> E1}; S)` – Zhiltsoff Igor Dec 04 '19 at 20:55
  • Huge thanks for your help! I believe, I was somehow close to solving this on my own, yet I clearly lack a lot of experience. Can you, please recommend me any materials which comprise some problems of that sort and, perhaps, cover some related topics? – Zhiltsoff Igor Dec 04 '19 at 20:58
  • 1
    @ZhiltsoffIgor To tell you the truth, StackOverflow or Computer Science StackExchange are probably the best places to see and work through lots of these examples. That's why I keep coming back :) – Patrick87 Dec 04 '19 at 21:03