1

Please have look at following language :

(a, b, c)* − {anbncn|n≥0}

My question is: How do I write a Context-Free Grammar for it ?

In general how can I write a grammar when something is excludes from it (i.e. has "-" sign) ?

2 Answers2

1

There is no algorithm to determine whether a language is context-free. So it is not surprising that there is no general solution to the question you pose.

Context-free languages are not closed under complementation, set difference or intersection. (But they are closed under concatenation, set union, and Kleene star.) In any event

{anbncn|n≥0}

is not a context-free language, but its complement (as in your question) is context-free. The proof of this fact (by constructing a context-free grammar for the complement) is a standard example for the non-closure of CFGs under complementation.

In outline, your language L can be composed from the union of:

  • All strings over the alphabet {a,b,c} where the letters are not in order. In other words, all strings which contain the substring ba, cb or ca.

  • {aibjck|i,j,k≥0∧i≠j}

  • {aibjck|i,j,k≥0∧j≠k}

rici
  • 234,347
  • 28
  • 237
  • 341
  • Thanks for your replay. The problem here is that also "bac" or "caaaab" and etc... are true and the languages which you described are not completely describe the L in my question. What do you think ? – Mohammad Amin Apr 03 '16 at 21:52
  • @MohammadAmin: `bac` and `caaaab` are in the first set of strings in my answer. The three languages together describe the L in your question. What is the problem? (Perhaps it would better to discuss this with your professor.) – rici Apr 03 '16 at 21:55
  • Oh I get it, sorry I didn't notice that. So now my problem is how to write a CFG for these 3 languages (especially the 1st one)? Also how can I use "union" in a grammar ? – Mohammad Amin Apr 03 '16 at 22:02
  • I found the solution how to union grammars here : [link](http://stackoverflow.com/questions/10377438/union-in-context-free-languages). Now the problem is how to write the grammar? – Mohammad Amin Apr 03 '16 at 22:07
0

(a, b, c)* − {a^nb^nc^n|n≥0}

This says you can choose any of a, b or c, and have any of them repeated e.g. abccbaabaab or abca or bccc

For a*, you use A->aA |e or alternatively A->AA | a | e.

Using that rule, you can do:

S -> A | B | C A->aA |e | AS B->bB |e | BS C->cC |e | CS

The inclusion of the S in each of A, B and C is what can be used if the whole thing has a Kleene star on it (....)*, and allows you to go back to the start and add another symbol.

I don't know how to make a grammar where a symbol is excluded, but logically if - is not an available terminal symbol, you have already excluded it.

Dhruv Ghulati
  • 2,976
  • 3
  • 35
  • 51