1

We know $L_1=\{w_1 w_2 \in (a+b)^*\mid |w_1|=|w_2|, w_2 \neq w_1^{\;\mathrm{R}}\}$ is a context-free language.

Can anyone help me produce a PDA or give me any hint how I can quickly understand why this is context-free?

Patrick87
  • 12,824
  • 1
  • 44
  • 76
user3661613
  • 507
  • 4
  • 10

1 Answers1

4

The language of even-length non-palindromes is given by the following context-free grammar:

$$S \rightarrow 0S0 \mid 1S1 \mid D$$ $$D \rightarrow 1A0 \mid 0A1$$ $$A \rightarrow \lambda \mid 00A \mid 01A \mid 10A \mid 11A$$

Patrick87
  • 12,824
  • 1
  • 44
  • 76
user3661613
  • 507
  • 4
  • 10
  • This answer is actually quite close to giving a grammar which generates the language in the question. The only problem with this language is that it generates some strings of odd length. This can be fixed by eliminating the rules A -> 0A and A -> 1A and adding the rules A -> 00A, A -> 01A, A -> 10A, A -> 11A. This will guarantee the generated string has even length (since all productions add an even number of terminal symbols). – Patrick87 Aug 11 '14 at 22:03
  • Intersection of two CFGs is not CF. Get rid of the second part; it's not necessary. Once you make the changes to the first grammar I specify above, you'll have a grammar for your language. To understand why, observe that: all strings will have even length; all strings will contain at least one mismatch; any even string with a mismatch can be generated by the grammar. If you have questions about one of those specifically, let me know. – Patrick87 Aug 11 '14 at 22:05
  • 1
    Okay -- now prove correctness. (If there is a mistake, you'll find it during the proof attempt.) – Raphael Aug 11 '14 at 22:20
  • A quick sketch of the proof might be as follows: First, prove L(G) is a subset of L, that is, your grammar only generates even-length non-palindromes. To see this, note that only even strings are generated, and that generated strings cannot be palindromes because of the mismatch introduced when expanding D. Now, show that L is a subset of L(G), that is, the grammar generates all even-length non-palindromes. To do this, consider any even-length non palindrome, and show how to generate it using the grammar. The crux is that there must be an outermost mismatch; this is generated by expanding D. – Patrick87 Aug 11 '14 at 22:28
  • 1
    @user3661613 I don't know what your learning goal is, but in some scenarios being able to write down a (maybe) correct grammar is worth nothing if you can't show it's correct. Therefore, I recommend you look into proof techniques as well; this is not too hard, i.e. something that I would comfortably expect of first-year CS undergrads in an exam (just for reference). – Raphael Aug 12 '14 at 06:09
  • 1
    @user3661613 If you understand the sketch I give in the comments, just rewrite it in your own words in the answer. If you don't understand it, tell me and I'll explain myself. I assume you do want to understand why the grammar is right, yes? That's all a proof is: an explanation of why the grammar is right. It's certainly not something you'd need to know how to do before a class on the subject or, for that matter, after a class on the subject, either. – Patrick87 Aug 12 '14 at 14:47
  • @Patrick87 "certainly not something you'd need to know how to do [...] after a class on the subject" -- that would be a bad class, assuming it's about formal language theory. – Raphael Aug 12 '14 at 19:16
  • @Raphael How many undergraduate students in a typical class on formal language and automata theory need to produce proofs after graduation? My estimate would be around zero, although that might be high. – Patrick87 Aug 12 '14 at 19:27
  • @Patrick87 Frankly, I don't care. It's what they have to do for their graduation, so that they will at least know exactly how much detail they are skipping later. (And, frankly, if they build stuff for the next plane or medical robot, I hope they do some proofs and do them right.) But that's beside the point here; we can discuss this in [chat] if you want. – Raphael Aug 12 '14 at 19:29