4

Is the language $L_1 = \{w_1cw_2 ~|~ w_1,w_2 \in \{a,b\}^{\ast} \text{ and } w_1 \neq w_2\}$ a context-free language?

It certainly isn't regular, but is it context free?

I'm having trouble creating a grammar that creates terminal symbols from the outside-in; Is there anything to look for explicitly that tells me it is/isn't CF?

And if it was in fact context-free, how would I go about proving that?

John L.
  • 38,985
  • 4
  • 33
  • 90
Humdrum
  • 41
  • 2

2 Answers2

6

The idea is that $w_1 \neq w_2$ if and only if either (1) $|w_1| \neq |w_2|$ or (2) the $i$th letters of $w_1,w_2$ are different. This leads to the following partition of $L_1$: $$ \begin{align*} L_1 &= (a+b)^+(a+b)^nc(a+b)^n \\ &\cup (a+b)^nc(a+b)^n(a+b)^+ \\ &\cup (a+b)^na(a+b)^*c(a+b)^nb(a+b)^* \\ &\cup (a+b)^nb(a+b)^*c(a+b)^na(a+b)^* \end{align*} $$ Each of the summands is clearly context-free, hence so is their union.

Yuval Filmus
  • 276,994
  • 27
  • 311
  • 503
  • I'm having trouble understanding how you partitioned the language. What do the 4 seperate cases represent? Would you be able to make it into a CFG if it isn't too much trouble? – Humdrum May 26 '19 at 06:21
  • 1
    It would be too much trouble. It’s your exercise. – Yuval Filmus May 26 '19 at 06:38
0

Grammar for this language based on @Yuval Filmus's answer:

$S \rightarrow X_{1}$| $X_{2}$|$X_{3}b \Sigma^{*}$|$X_{4}a \Sigma^{*}$

$X_{1} \rightarrow \Sigma X_{1} \Sigma$|$\Sigma^{+} c$

$X_{2} \rightarrow \Sigma X_{2} \Sigma$|$c \Sigma^{+}$

$X_{3} \rightarrow \Sigma X_{3} \Sigma$|$a \Sigma^{*} c$

$X_{4} \rightarrow \Sigma X_{4} \Sigma$|$b \Sigma^{*} c$

$\Sigma^{+} \rightarrow \Sigma \Sigma^{*}$

$\Sigma^{*} \rightarrow a \Sigma^{*}$|$b \Sigma^{*}$|$\epsilon$

$\Sigma \rightarrow a$|$b$

Arun Madhav
  • 108
  • 1
  • 7