non-context-free languages
DESCRIPTION
Non-Context-Free Languages. Lecture 10 Section 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 { a n b n c n | n 0}. To process this would require two stacks. The Pumping Lemma. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Non-Context-Free Languages](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815dd8550346895dcc0331/html5/thumbnails/1.jpg)
Non-Context-Free Languages
Lecture 10Section 2.3
Fri, Oct 13, 2006
![Page 2: Non-Context-Free Languages](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815dd8550346895dcc0331/html5/thumbnails/2.jpg)
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.
![Page 3: Non-Context-Free Languages](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815dd8550346895dcc0331/html5/thumbnails/3.jpg)
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…
![Page 4: Non-Context-Free Languages](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815dd8550346895dcc0331/html5/thumbnails/4.jpg)
The Pumping Lemma• |vy| > 0,• |vxy| p,• uvixyiz is in L for all i 0.
![Page 5: Non-Context-Free Languages](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815dd8550346895dcc0331/html5/thumbnails/5.jpg)
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.
![Page 6: Non-Context-Free Languages](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815dd8550346895dcc0331/html5/thumbnails/6.jpg)
An Example• But first, an example.• Let L = {anbncn | n 0}.• We will show that L is not context-
free.
![Page 7: Non-Context-Free Languages](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815dd8550346895dcc0331/html5/thumbnails/7.jpg)
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.
![Page 8: Non-Context-Free Languages](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815dd8550346895dcc0331/html5/thumbnails/8.jpg)
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.
![Page 9: Non-Context-Free Languages](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815dd8550346895dcc0331/html5/thumbnails/9.jpg)
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.
![Page 10: Non-Context-Free Languages](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815dd8550346895dcc0331/html5/thumbnails/10.jpg)
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.
![Page 11: Non-Context-Free Languages](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815dd8550346895dcc0331/html5/thumbnails/11.jpg)
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.
![Page 12: Non-Context-Free Languages](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815dd8550346895dcc0331/html5/thumbnails/12.jpg)
An Example• Therefore, L is not context-free.
![Page 13: Non-Context-Free Languages](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815dd8550346895dcc0331/html5/thumbnails/13.jpg)
Proof of the Pumping Lemma• Proof:
![Page 14: Non-Context-Free Languages](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815dd8550346895dcc0331/html5/thumbnails/14.jpg)
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.
![Page 15: Non-Context-Free Languages](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815dd8550346895dcc0331/html5/thumbnails/15.jpg)
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.
![Page 16: Non-Context-Free Languages](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815dd8550346895dcc0331/html5/thumbnails/16.jpg)
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.
![Page 17: Non-Context-Free Languages](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815dd8550346895dcc0331/html5/thumbnails/17.jpg)
The Proof• Conversely, if s contains more than bh
symbols, then the height of the parse tree must be more h.
![Page 18: Non-Context-Free Languages](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815dd8550346895dcc0331/html5/thumbnails/18.jpg)
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.
![Page 19: Non-Context-Free Languages](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815dd8550346895dcc0331/html5/thumbnails/19.jpg)
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.
![Page 20: Non-Context-Free Languages](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815dd8550346895dcc0331/html5/thumbnails/20.jpg)
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.
![Page 21: Non-Context-Free Languages](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815dd8550346895dcc0331/html5/thumbnails/21.jpg)
The ProofS
A
A
x
u
v y
z
![Page 22: Non-Context-Free Languages](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815dd8550346895dcc0331/html5/thumbnails/22.jpg)
The Proof• The “middle part” of this tree, the
part that producesA * vAy,
may be repeated as many times as desired.
![Page 23: Non-Context-Free Languages](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815dd8550346895dcc0331/html5/thumbnails/23.jpg)
The ProofS
A
A
x
u
v y
z
![Page 24: Non-Context-Free Languages](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815dd8550346895dcc0331/html5/thumbnails/24.jpg)
The ProofS
A
A
u
v y
z
A
x
v y
![Page 25: Non-Context-Free Languages](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815dd8550346895dcc0331/html5/thumbnails/25.jpg)
The ProofS
A
A
u
v y
z
Av y
A
x
v y
![Page 26: Non-Context-Free Languages](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815dd8550346895dcc0331/html5/thumbnails/26.jpg)
The Proof• Therefore, the strings uv2xy2z, uv3xy3z, etc. can also be derived.
• So can the string uxz.
![Page 27: Non-Context-Free Languages](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815dd8550346895dcc0331/html5/thumbnails/27.jpg)
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.
![Page 28: Non-Context-Free Languages](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815dd8550346895dcc0331/html5/thumbnails/28.jpg)
The Proof• If they were, then the middle part of
the derivation would beA * A,
which could be eliminated.• Thus, |vy| > 0.
![Page 29: Non-Context-Free Languages](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815dd8550346895dcc0331/html5/thumbnails/29.jpg)
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.
![Page 30: Non-Context-Free Languages](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815dd8550346895dcc0331/html5/thumbnails/30.jpg)
An Example• Let = {a, b}.• Show that the language
{ww | w *}is not context-free.
• Use s = ww, where w = ap – 1b.