You are on page 1of 34

Theory of Computation (With Automata Theory)

NON-CONTEXT-FREE
LANGUAGES
Non-Context-Free Languages

Introduction

Recall the following:


A regular language has either
a nondeterministic automaton
that recognizes it or a regular
expression that can generate
its strings.
A context-free language has
either a pushdown automaton
that recognizes it or a contextfree
grammar
that
can
generate its strings.

Non-Context-Free Languages

* Property of STI
Page 1 of 34

Theory of Computation (With Automata Theory)

Questions:
1. Are there non-context-free
languages?
2. How can a language be
proven to be non-contextfree?

The language L= {anbncn n


1} is non-context-free since
there is no PDA that can
recognize it.

To prove that a language is


non-context-free, it must be
shown that there is no PDA or
CFG for that language (difficult
to do).

Non-Context-Free Languages

* Property of STI
Page 2 of 34

Theory of Computation (With Automata Theory)

Recall that the pumping


lemma was used to prove that
a language is not regular.
The pumping lemma for
regular languages exploited a
property of regular languages
to prove the non-regularity of
languages.
Strings of regular languages
have the property that if they
are long enough, then they
have a substring that can be
repeated an arbitrary number
of times and the resulting
strings will still be in language.

Languages which have strings


that do not have this property
are considered not regular.

Non-Context-Free Languages

* Property of STI
Page 3 of 34

Theory of Computation (With Automata Theory)

To prove that a language is


non-context-free, there must
be a pumping lemma for
context-free languages.
This pumping lemma should
also exploit a certain property
of context-free languages.
Languages whose strings do
not have this property are
considered non-context-free.

Non-Context-Free Languages

* Property of STI
Page 4 of 34

Theory of Computation (With Automata Theory)

Context-Free Grammars Revisited

Assume the following contextfree grammar G for language


L:
S A1
A 0A1

The leftmost derivation for the


string 0001111 is

S A1
0A11
00A111
000A1111
0001111

Non-Context-Free Languages

* Property of STI
Page 5 of 34

Theory of Computation (With Automata Theory)

The parse tree for the leftmost


derivation for the string
0001111 is
S

Non-Context-Free Languages

* Property of STI
Page 6 of 34

Theory of Computation (With Automata Theory)

Notice that because the string


being derived is long, some
variables were used more than
once.

In the pumping lemma for


regular languages, strings
whose length is greater than or
equal to the number of DFA
states (the pumping length)
have a substring that can be
repeated an arbitrary number
of times, and the resulting
strings will still be in the
language.

For the pumping lemma for


context-free languages, what
is then the pumping length?

Non-Context-Free Languages

* Property of STI
Page 7 of 34

Theory of Computation (With Automata Theory)

Deriving the Pumping Length

Assume that for a certain


parse tree, the non-leaf nodes
have a maximum number of
children equal to c (this implies
that the right-hand side of the
rules of the grammar has a
string whose length is c).
Assume also that in the
succeeding discussions c 2.

Assume further that the height


of the parse tree for the
derivation of a certain string w
is h.

A tree with height h can have a


maximum number of leaf
nodes equal to ch.

Non-Context-Free Languages

* Property of STI
Page 8 of 34

Theory of Computation (With Automata Theory)

Since the leaf nodes of the


parse tree represent the
symbols of the string w being
derived, then this means that
w ch

Assume now that the length of


a certain string w is
w cv + 1
where v is the number of
variables in the grammar.
This implies that the height of
the parse tree will be
hv+1

Non-Context-Free Languages

* Property of STI
Page 9 of 34

Theory of Computation (With Automata Theory)

If h v + 1, then there is a
path from the root node to a
leaf node that is greater than
or equal to v + 1.

This path will have at least v +


2 nodes. If the last node is a
terminal, then the remaining
nodes are variables.
Since the derivation used
more than v + 1 variables, then
at least one variable was
repeated (by the pigeonhole
principle).

Non-Context-Free Languages

* Property of STI
Page 10 of 34

Theory of Computation (With Automata Theory)

This is illustrated below:


S

...

u
...

...
z

...
y

S uAz uvAyz uvxyz


Assume that this parse tree
represents
the
shortest
derivation for the string S.
Non-Context-Free Languages

* Property of STI
Page 11 of 34

Theory of Computation (With Automata Theory)

If the derivation A vAy is


repeated before the derivation
A x, then the parse tree
would look like:
S

...

u
...

v
...

...

...
y

...
y

S uAz uvAyz uvvAyyz


uvvxyyz uv2xy2z
Non-Context-Free Languages

* Property of STI
Page 12 of 34

Theory of Computation (With Automata Theory)

If the derivation A vAy is


repeated several times, then
the string derived will look like
uvixyiz
and this string would still
belong to the language
represented by the grammar.

In the pumping lemma for


regular languages, the input
string w can then be written as
w = xyiz
where the middle substring y
can be pumped several times
and the resulting string will still
belong to the language
concerned.

Non-Context-Free Languages

* Property of STI
Page 13 of 34

Theory of Computation (With Automata Theory)

In the pumping lemma for


context-free languages, the
input string w can then be
written as
w = uvixyiz
where the 2nd and 4th
substrings v and y can be
pumped several times and the
resulting string will still belong
to the language concerned.

Non-Context-Free Languages

* Property of STI
Page 14 of 34

Theory of Computation (With Automata Theory)

In the pumping lemma for


regular languages, the pumping
length p refers to the number of
states of the DFA recognizing
the language.
Hence, w p for the pumping
lemma to be used.
In the pumping lemma for
context-free languages, the
pumping length p is equal to the
term cv + 1 where c is the length
of the longest right-hand side
string of any rule in the
grammar and v is the number of
variables of the grammar.

Similarly, the length of the string


w being analyzed must be
greater than cv + 1.

Non-Context-Free Languages

* Property of STI
Page 15 of 34

Theory of Computation (With Automata Theory)

The Pumping Lemma for ContextFree Languages

If L is a context-free language,
then there is a number p (the
pumping length) where, if w
is any string in L whose length
is greater than or equal to p,
then w may be divided into five
parts, w = uvxyz, satisfying the
following conditions:
1. for each i 0, uvixyiz
L,

2. vy 1, and
3. vxy

Non-Context-Free Languages

p.

* Property of STI
Page 16 of 34

Theory of Computation (With Automata Theory)

Major points:
1. The string w = uvixyiz
for each i 0.

This
condition
simply
states
that
strings
belonging to language L
that are longer than the
pumping length can be
divided into 5 parts u, v, x,
y, and z.
The 2nd and 4th parts can
be repeated several times
and the resulting string will
still belong to language L.

Non-Context-Free Languages

* Property of STI
Page 17 of 34

Theory of Computation (With Automata Theory)

2.

vy 1 refers to the fact


that substrings v and y
cannot both be empty
strings.

If v = y = , then the
derivation
A

vAy
becomes A A. This is a
useless derivation and
therefore can be removed.
This, however, violates the
assumption that the parse
tree presented depicts the
shortest derivation for the
string S.
Therefore vy 1.

Non-Context-Free Languages

* Property of STI
Page 18 of 34

Theory of Computation (With Automata Theory)

3.

vxy

Consider
again
following parse tree:

the

...
hv+1

u
...

...
z

...
y

If w p, then the height


of the tree h v + 1.

Non-Context-Free Languages

* Property of STI
Page 19 of 34

Theory of Computation (With Automata Theory)

Consider now the following


subtree (rooted at the
upper variable A) of the
given parse tree:

root of
the
subtree
A

...

...

hv+1

Non-Context-Free Languages

* Property of STI
Page 20 of 34

Theory of Computation (With Automata Theory)

Take note that no other


variable is repeated below
the root node of this
subtree.
It follows that this subtree
has a height:
hv+1

The string formed by this


subtree then has a length:

Non-Context-Free Languages

vxy

cv + 1

vxy

* Property of STI
Page 21 of 34

Theory of Computation (With Automata Theory)

As in the pumping lemma for


regular languages, proving a
language is non-context-free
will use proof by contradiction:
1. First, assume that the
language L is context-free.

2. Since L is context-free, the


pumping lemma states that
all strings of L that are long
enough can be pumped.
3. Find a string w that is long
enough that cannot be
pumped.
4. If there is such a string,
then the pumping lemma
was contradicted. Hence,
language L is non-contextfree.
Non-Context-Free Languages

* Property of STI
Page 22 of 34

Theory of Computation (With Automata Theory)

Example 1: Prove that the


language L1 = {anbncn n 1}
is not context-free.

Assume first that L1 is contextfree.


Let p be the pumping length of
L 1.
Let the string to be pumped be
w = a pb p c p.
Since w p, the string chosen
is long enough to be pumped.
Consider now the various
ways in which w can be
divided into the u, v, x, y, and z
substrings.

Non-Context-Free Languages

* Property of STI
Page 23 of 34

Theory of Computation (With Automata Theory)

Option 1: Let vxy be all as.


Pumping v and y would
create a string where there
are more as than bs and
cs. Hence, the resulting
string will not be in L1.
This violates condition 1 of
the pumping lemma.
Option 2: Let vxy be some
as followed by some bs.
Pumping v and y would
create a string where there
are more as and bs than
cs. Hence, the resulting
string will not be in L1.
This violates condition 1 of
the pumping lemma.

Non-Context-Free Languages

* Property of STI
Page 24 of 34

Theory of Computation (With Automata Theory)

Option 3: Let vxy be all bs.


Pumping v and y would
create a string where there
are more bs than as and
cs. Hence, the resulting
string will not be in L1.
This violates condition 1 of
the pumping lemma.
Option 4: Let vxy be some
bs followed by some cs.
Pumping v and y would
create a string where there
are more bs and cs than
as. Hence, the resulting
string will not be in L1.
This violates condition 1 of
the pumping lemma.

Non-Context-Free Languages

* Property of STI
Page 25 of 34

Theory of Computation (With Automata Theory)

Option 5: Let vxy be all cs.


Pumping v and y would
create a string where there
are more cs than as and
bs. Hence, the resulting
string will not be in L1.
This violates condition 1 of
the pumping lemma.

Since L1 has a string that


cannot be pumped, then this
contradicts
the
original
assumption that L1 is contextfree. Therefore, L1 is noncontext-free.

Non-Context-Free Languages

* Property of STI
Page 26 of 34

Theory of Computation (With Automata Theory)

Example 2: Prove that the


language L2 = {ww w
{0,
1}*} is not context-free.

Assume first that L2 is contextfree.


Let p be the pumping length of
L 2.
Let the string to be pumped be
w = 0 p1 p 0 p1 p.
Since w p, the string chosen
is long enough to be pumped.
Consider now the various
ways in which w can be
divided into the u, v, x, y, and z
substrings.

Non-Context-Free Languages

* Property of STI
Page 27 of 34

Theory of Computation (With Automata Theory)

Option 1: Let vxy be all 0s


from the first or second 0p of
the string.
Pumping v and y would
create a string where the
first half is not equal to the
second half. Hence, the
resulting string will not be
in L2.
This violates
condition 1 of the pumping
lemma.

Option 2: Let vxy be all 1s


from the first or second 1p of
the string.
Pumping v and y would
create a string where the
first half is not equal to the
second half. Hence, the
resulting string will not be
in L2.
Non-Context-Free Languages

* Property of STI
Page 28 of 34

Theory of Computation (With Automata Theory)

Option 3: Let vxy be some 0s


followed by some 1s from the
first or second half of the
string.
Pumping v and y would
create a string where the
first half is not equal to the
second half. Hence, the
resulting string will not be
in L2.
This violates
condition 1 of the pumping
lemma.

Non-Context-Free Languages

* Property of STI
Page 29 of 34

Theory of Computation (With Automata Theory)

Option 4: Let vxy be some 1s


followed
by
some
0s
somewhere in the middle
portion of the string.

Pumping v and y would


create a string where the
first half is not equal to the
second half. Hence, the
resulting string will not be
in L2.
This violates
condition 1 of the pumping
lemma.
Since L2 has a string that
cannot be pumped, then this
contradicts
the
original
assumption that L2 is contextfree. Therefore, L2 is noncontext-free.

Non-Context-Free Languages

* Property of STI
Page 30 of 34

Theory of Computation (With Automata Theory)

Example 3: Prove that the


language L3 = {aibjck k j i
0} is not context-free.

Assume first that L3 is contextfree.


Let p be the pumping length of
L 3.
Let the string to be pumped be
w = a pb p c p.
Since w p, the string chosen
is long enough to be pumped.
Consider now the various
ways in which w can be
divided into the u, v, x, y, and z
substrings.

Non-Context-Free Languages

* Property of STI
Page 31 of 34

Theory of Computation (With Automata Theory)

Option 1: Let vxy be all as.


Pumping v and y would
create a string where there
are more as than bs and
cs. Hence, the resulting
string will not be in L3.
This violates condition 1 of
the pumping lemma.
Option 2: Let vxy be some
as followed by some bs.
Pumping v and y would
create a string where there
are more as and bs than
cs. Hence, the resulting
string will not be in L3.
This violates condition 1 of
the pumping lemma.

Non-Context-Free Languages

* Property of STI
Page 32 of 34

Theory of Computation (With Automata Theory)

Option 3: Let vxy be all bs.


Pumping v and y would
create a string where there
are more bs than cs.
Hence, the resulting string
will not be in L3. This
violates condition 1 of the
pumping lemma.
Option 4: Let vxy be some
bs followed by some cs.
Pumping down v and y
would create a string
where there are more as
than bs and cs. Hence,
the resulting string will not
be in L3.
This violates
condition 1 of the pumping
lemma.

Non-Context-Free Languages

* Property of STI
Page 33 of 34

Theory of Computation (With Automata Theory)

Option 5: Let vxy be all cs.


Pumping down v and y
would create a string
where there are more as
and bs than cs. Hence,
the resulting string will not
be in L3.
This violates
condition 1 of the pumping
lemma.
Since L3 has a string that
cannot be pumped, then this
contradicts
the
original
assumption that L3 is contextfree. Therefore, L3 is noncontext-free.

Non-Context-Free Languages

* Property of STI
Page 34 of 34