Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

$$\{a^i b^j c^k \mid i\ne j\text{ or }j\ne k\}$$

Is this language context free? I believe it is based on the following CFG but I would like some confirmation that I'm right.

$$\begin{align*} &S\to aSbA \mid BbSc \mid\epsilon\\ &A\to cA \mid\epsilon\\ &B\to aB \mid\epsilon \end{align*}$$

share|improve this question
2  
That grammar obviously generates $\epsilon$, which is not in the language, and it generates $S\to aSbA\to abA\to abcA\to abc$, which is not in the language. I haven't thought about it carefully, but I think you are barking up the wrong tree, and the language is not context-free. –  MJD Sep 27 '12 at 23:50
    
My reason for thinking that the language is not context-free is this: To test $i\ne j$, it has to count the as, which it can do on the stack, and then count backwards again as it sees the bs, which destroys the count that was on the stack. Then by the time it gets to the cs it has forgotten how many bs there were, so it can't test $j\ne k$. This is of course not a proof, since there might be some other strategy for recognizing the language that does work, but I think it does suggest what approach to take: try the pumping lemma, as you would for $\{a^ib^ic^i\}$. –  MJD Sep 27 '12 at 23:56

1 Answer 1

up vote 3 down vote accepted

The grammar doesn’t generate the language. First, $\epsilon=a^0b^0c^0$, so $\epsilon$ is not in the language. It also generates an unwanted $abc$ via $S\Rightarrow aSbA\Rightarrow abA\Rightarrow abcA\Rightarrow abc$. In fact, your grammar generates words of the form $a^ib^jc^k$ such that $i=j$ or $j=k$.

The language is the union of $L_1=\{a^ib^jc^k:i\ne j\}$ and $L_2=\{a^ib^jc^k:j\ne k\}$, and context-free languages are closed under union, so it suffices to show that $L_1$ and $L_2$ are context-free. It’s not hard to design context-free grammars for $L_1$ and $L_2$. For $L_1$, for instance, start by designing a context-free grammar for $\{a^ib^jc^k:i<j\}$; that’s pretty easy, and you can clearly do the same for $\{a^ib^jc^k:i>j\}$. Then just ‘paste’ them together properly to get a context-free grammar for $L_1$, and continue with $L_2$.

share|improve this answer
    
Nice job, Brian, +1. –  Rick Decker Sep 28 '12 at 0:09
    
So would something like S->Sc|aSb|Sb|b satisfy the i<j? –  Takkun Sep 28 '12 at 0:32
1  
@Takkun: You don’t want that: $S\Rightarrow aSb\Rightarrow aScb\Rightarrow abcb$. Don’t try to do everything at once. First generate $a^nb^n$, then generate at least one $b$, then generate an arbitrary number of $c$’s. –  Brian M. Scott Sep 28 '12 at 0:35
    
S->Sc|A, A->aAb|B, B->bB|b? –  Takkun Sep 28 '12 at 0:42
1  
@Takkun: Yes, that should work. –  Brian M. Scott Sep 28 '12 at 0:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.