non-context-free languages
Post on 12-Feb-2016
74 Views
Preview:
DESCRIPTION
TRANSCRIPT
Non-Context-Free Languages
Lecture 10Section 2.3
Fri, Oct 13, 2006
Non-Context-Free Languages• It turns out that not all languages
are context-free.• An example of a non-context-free
language is{anbncn | n 0}.
• To process this would require two stacks.
The Pumping Lemma• The Pumping Lemma for Context-
Free Languages: If L is a context-free language then there exists an integer p such that if any string s in L has length at least p, then s may be divided into five substrings uvxyz such that…
The Pumping Lemma• |vy| > 0,• |vxy| p,• uvixyiz is in L for all i 0.
The Proof• The proof is somewhat similar to
the proof of the Pumping Lemma for Regular Languages except that it is based on a grammar rather than a machine.
An Example• But first, an example.• Let L = {anbncn | n 0}.• We will show that L is not context-
free.
An Example• Proof:
• Suppose it is.• Then let p be the p of the Pumping
Lemma.• Let s = apbpcp.• Then s = uvxyz such that |vy| > 0, |vxy| p, and uvixyiz is in L.
• We will show that this is not possible.
An Example• vxy is the “middle part” of uvxyz
and it has length at most p.• Therefore, it consists of
• All a’s,• Some a’s followed by some b’s,• All b’s,• Some b’s followed by some c’s, or• All c’s.
An Example• It is enough to consider the first
two cases.• The other three are similar.• Case 1: Suppose vxy consists of all a’s.• Then v = ak and y = am for some k, m,
not both 0.
An Example• So uv2xy2z = ap + k + mbpcp, which is not
in L.• This is a contradiction.
• Case 2: vxy consists of some a’s and some b’s.• There are three possibilities:
•v is all a’s and y is all b’s,•v is all a’s and y is some a’s, some b’s,•v is some a’s, some b’s and y is all a’s.
An Example• It doesn’t really matter because both v and y get pumped up.
• Let k be the number of a’s and m be the number of b’s in vy.
•m and k are not both 0.• So uv2xy2z = ap + kbp + mcp, which is not
in L.• This is a contradiction.
An Example• Therefore, L is not context-free.
Proof of the Pumping Lemma• Proof:
The Idea Behind the Proof• If a CFL contains a word w with a
sufficiently long derivation S * w,
then some variable A must appear more than once.
• That is, we haveS * uAz * uvAyz * uvxyz.
The Idea Behind the Proof• Thus, A * vAy and A * x.• We may repeat the derivation
A * vAy as many times as we like (including zero times), producing strings uvnxynz, for any n 0.
The Proof• Proof:
• Let b be the largest number of symbols on the right-hand side of any grammar rule.
• (Assume b 2.)• Let h be the height of the derivation
tree of a string s.• Then s can contain at most bh
symbols.
The Proof• Conversely, if s contains more than bh
symbols, then the height of the parse tree must be more h.
The Proof• Now |V| is the number of variables in
the grammar of L.• So if a string in L has a length greater
than b|V| + 1, then the height of its parse tree must be more than |V| + 1.
• So let p = b|V| + 1 and suppose that a string s in L has length at least p.
The Proof• Consider the longest path through the
tree.• It has length at least |V| + 1.• That path has |V| + 2 nodes on it,
counting the root node S and the leaf node, which is a terminal.
The Proof• Thus, |V| + 1 of the nodes are
variables.• So one of them must be repeated.• As we follow the longest path from
leaf to root, let A be the first variable that repeats.
• Now consider these two occurrences of A along the longest path.
The ProofS
A
A
x
u
v y
z
The Proof• The “middle part” of this tree, the
part that producesA * vAy,
may be repeated as many times as desired.
The ProofS
A
A
x
u
v y
z
The ProofS
A
A
u
v y
z
A
x
v y
The ProofS
A
A
u
v y
z
Av y
A
x
v y
The Proof• Therefore, the strings uv2xy2z, uv3xy3z, etc. can also be derived.
• So can the string uxz.
The Proof• Furthermore, we may assume that
this was the shortest derivation of s.• It follows that v and y cannot both be
empty strings.
The Proof• If they were, then the middle part of
the derivation would beA * A,
which could be eliminated.• Thus, |vy| > 0.
The Proof• Finally, we must show that |vxy| p.
• The subtree rooted at the second-to-last A has height at most |V| + 1.
• So the string vxy has at most b|V| + 1 = p symbols.
An Example• Let = {a, b}.• Show that the language
{ww | w *}is not context-free.
• Use s = ww, where w = ap – 1b.
top related