# Proving that a word is *not* generated by a context-free grammar

I saw the answer in one of the solutions and I cannot figure out how they got the answer. The question is asked if the word is in the language or not for CNF...

How did they get the answer so that ab is in the language and aaaa is not in the language here ?

S -> aSb |ab

ab     Yes
aaaa   No
aabb   Yes


edit: is aaaa No because it has b in aSb and ab is yes because obviously ab is there in aSb |ab

also aabb is in the language because of aSb cause you would add same amount of a's and b's to both sides

-
To positively decide the membership of individual strings you only have to find one derivation that produces the string. Thus Proving non-membership is harder. In this case it's easy since every Production generates a b, so no derivation can result in a String of as only. –  collapsar Apr 9 '14 at 22:39
Your answer is essentially correct ... but you have to learn to say that in a more formal way. For example: the answer is yes for $ab$ because $S$ derives on $ab$ according to the second rule $S\rightarrow ab$, –  babou Apr 9 '14 at 22:40
oh so I do have to follow the 3 rules, thanks! –  Dana Apr 9 '14 at 22:49
I see only 2 rules: $S\rightarrow aSb$ and $S\rightarrow ab$. Yes you have to follow them (that is why they are called rules :-), or tio prove that following them cannot produce a given string (such as $aaaa$), –  babou Apr 9 '14 at 22:54
@babou what is exactly rule 3 ? I know rule 1 is "any number in the set" , Rule 2 is "if x and y are in then so are ...." –  Dana Apr 9 '14 at 23:08

Let's assume you want to show that some word $w$ of length $n$ is not in the language generated by some grammar context-free $G$.

You can solve the problem algorithmically. There are two approaches:

1. Enumerate all strings in $L(G)$ of length at most $n$. In order to do that,

• transform the grammar in Chomsky normal form and
• enumerate all derivations with at most $n$ rule applications.

This works since every rule application adds one terminal symbol in CNF (there are no $\varepsilon$-rules). If your original grammar has this property (as does the one in your question), you can skip the transformation in CNF.

This is tractable for small $n$ and/or grammars with few words per length. As soon as $L(G)$ grows fast with $n$, it breaks down.

2. Parse $w$, for instance with the CYK algorithm. This runs in time $\Theta(n^3)$ and feasible for most grammars and inputs of reasonable size.

Clearly, the latter is the better option in an algorithmic mindset, but the former might be prudent if you have to make a quick decision given a short word in an exam-like situation.

-
I got a lot of reading to do...It's starting to make sense... –  Dana Apr 9 '14 at 23:12

The easiest way to show that $aaaa$ is not in the grammar is to prove that in every word generated by the grammar, the number of $a$s is equal to the number of $b$s. You can prove it by induction on the number of derivation rules used, using the fact that in each derivation rule the number of $a$s is equal to the number of $b$s.

If you are more ambitious, you can prove in the same way that in every word generated by the grammar, all the $a$s precede all the $b$s. Both these results put together show that the language generated by the grammar is contained in $\{a^n b^n : n \geq 0\}$. Again using induction, you can show that the empty word is not generated by the grammar. So the language is contained in $\{a^n b^n : n \geq 1\}$. On the other hand, induction on $n$ shows that all of these words can be generated by the grammar. We have shown that the language generated by the grammar is $\{a^nb^n : n \geq 1\}$.

-
These proofs can be a hassle. –  Raphael Apr 9 '14 at 22:54
that was awesome..thank you! –  Dana Apr 9 '14 at 23:06