In this section we prove a pumping lemma for CFL that is similar, but as expected from the richer structure of the PDA, more complex than the one for regular languages. We will benefit in our proof from the concept of parse tree for a CFG. The reader has most likely encountered trees in courses on discrete mathematics, data structures, etc. A tree is a structure like the one drawn below:
Since by default all edges point “downwards”, one does not need to emphasize so by drawing them as arrows. The round nodes may or may not have names. Our parse trees will have all their nodes named. The precise angle or slope of the edges does not matter—meaning, we allow variation in drawings without changing the tree we want to depict. However, left to right order matters , thus the following is a different tree from the one above. Our trees are ordered .
Of course, the reader has encountered many aspects of discrete mathematics before, but this did not stop me from reintroducing some such concepts in Chapter 1 . So, let us revisit the definition of trees. A tree has both a “data” component (the node “contents”, whatever they may be) and a structural component or geometry, that is, how the nodes are interconnected. This motivates us to opt for an abstraction toward a mathematical definition that wants a tree to be an ordered pair of a “data” part and a “structure” part,
S , T
, where S is a set of objects and T is the “geometry” imposed upon them. To get quickly to our pumping lemma we present only as much dendrology 134 as necessary, thus we will rather define directly those trees that matter to us: labeled trees that are parse trees of some CFG G .
4.4.0.9 Definition. (Parse Trees) Given a CFG G =
, ∑, S ,
. The set of all parse trees for G , and the associated concepts of root , support , and yield , are defined inductively.
In what follows we assume an infinite supply of (unspecified) objects , a set N of nodes . We may visualize N as an infinite supply of “circles”, such as the ones employed in the two illustrations above.
(1) Basis — “smallest” parse trees .
= n is a parse trees, if n
N . This n is labeled by either some A
or some a
∑ ∪ {
}.
We say that n is the root of
, sp (
) = { n } is its support , and the string A (respectively a ) is the yield of the tree.
Note that we use the labels in the definition of yield, but use the node name in the definition of root !
As the recursive definition unfolds, we will note at once that the support of a tree is simply the set of all nodes from N that we utilized to build it.
(2) Suppose that
i are all parse trees, for i = 1, . . . , k , where for each i ≠ j we have sp (
i ) ∩ sp (
j ) =
135 —i.e., the trees do not “share nodes”.
They may share labels though!
Assume further that
i has as root r i , and yield α i
(
∪ ∑)*.
Let r
N be new relative to the
i [i.e., r ∉
sp (
i )]. Then we can build a new parse tree
=
r , T 1 , . . . , T k
with sp (
) = { r } ∪
sp (
i )—this updates sp recursively and verifies the claim that we made about it above.
Building
has a restriction: if we want to use a
i with root r i , that is labeled
, then it must be that this is the only tree we use to build
.
We can label the root r with any A
, provided the rule A → β is in the grammar, where β is formed by concatenating the labels of the r i in the left to right order i = 1, 2, 3, . . . , k .
The yield of the new parse tree is α 1 α 2 . . . α k .
The
i are called (the) subtrees of r but also subtrees of
.
In what follows we will often say tree , meaning parse tree . Parse trees are the only trees of interest in this volume. The root of any parse tree which has more than one node (in its support) must be labeled by a nonterminal, since only construction step (2) of the preceding inductive definition applies.
The following connects Definition 4.4.0.9 with the usual depiction of trees as drawings of nodes connected by edges (“graphs”).
4.4.0.10 Informal Definition. We will draw a “physical” tree that corresponds to the inductive Definition 4.4.0.9 . Via a recursive construction we associate a figure composed of nodes (circles) and edges (straight lines sloping downwards), the latter connecting pairs of nodes.
(1′) A parse tree of type (1) in Definition 4.4.0.9 has no edges. It is drawn as a single circle, labeled by the root label (written normally close to, or inside the circle).
(2′) Assuming that we have associated a (labeled) drawing with each of the subtrees
i occurring in case (2) of Definition 4.4.0.9 , we draw the parse tree for
as follows:
(a) We draw a circle for r and label it A [cf. 4.4.0.9 , (2)]
(b) We introduce precisely k edges from the circle representing r (and named A ) to each of the roots of the subtrees
i , which are drawn below r and from left to right in the order i = 1, 2, 3, . . . , k .
4.4.0.11 Example. Here are some parse trees for the CFG in 4.0.0.4 .
All three have roots labeled S . The yields of the trees, from left to right, are S ,
, and 00 S 11.
4.4.0.12 Example. Often we want to discuss a parse tree without drawing a specific one. We draw generic trees by essentially drawing a root and then connecting it to its subtrees, the latter drawn as triangles with or without names (below we have called the subtrees
1 ,
2 and
3 ). We gave label A to the root.
The following is easy but fundamental.
4.4.0.13 Proposition. If
is a (parse) tree, then the root node is the only one pointed to by no edges. Every other node in
—that is, _sp (
)—is pointed to by precisely one edge_ .
Proof . We do induction over the set of parse trees ( 4.4.0.9 )—or induction on trees . If
= n
N , then the validity of the claim is immediate, since there is just a root node and nothing else. Let then
=
r ,
1 , . . . ,
k
and assume the claim (I.H.) for all the
i . By 4.4.0.9 , the root, r , of
will have no edges “coming in”, and the only edges added are those from r to the r i —roots of the
i ( 4.4.0.10 ). Viewed from within
, each of the r i will now have precisely one edge pointing to them by I.H. Moreover, since there have been no other edges added, the I.H. covers the case for all other non root nodes as well.
4.4.0.14 Definition. We introduce some standard terminology from graph theory: The number of edges pointing to a node in a tree is called the indegree of the node.
We say that a node n in a tree is a child of a node m iff there is an edge from m to n . Distinct children of m are called sibling nodes . We call m the parent node of n .
By 4.4.0.13 , every non root node in a tree
has precisely one parent.
A sequence of nodes a 1 , a 2 , . . . , a n , in a tree is a path or a chain iff, for all i = 1, 2, . . . , n − 1, we have an edge from a i to a i +1 . We say that this is a path from a 1 to a n , and that a n is a descendant of a 1 while a 1 is an ancestor of a n . The path is said to have length n − 1, that is, one less than the number of its nodes. This number coincides with the number of edges along the path.
A node is a leaf iff it has no children. A non leaf node is called an internal node .
We note that case (2) in Definition 4.4.0.9 dictates that every internal node must be labeled by a nonterminal.
We have the following corollaries of 4.4.0.13 :
4.4.0.15 Corollary. The root of a tree is an ancestor of every non root node of the tree.
Proof . Induction on trees. For the basis, let
= n
N . The result is vacuously true.
Let us look at the case of
=
r ,
1 , . . . ,
k ), where we take as I.H. the truth of the claim for all
i . Let n be an arbitrary non root node in
, and say, without loss of generality, that it is in
1 . By the I.H. there is a path from r 1 (notation as in 4.4.0.9 ) to n within
1 . The case rests since there is an edge from r to r 1 .
4.4.0.16 Remark. (1) The path from r to n that establishes the above mentioned ancestry is unique, for if a 1 , . . . , a q and b 1 , . . . , b m , are two distinct such paths where a 1 = b 1 = r and a q \= b m \= n , then the paths have a maximal common part at the tail-end—they certainly share n — say, c 1 , . . . , c p where c p \= n and each c j is both an a q and a b t . Now, by assumption, c 1 ≠ r . Thus (by maximality), it has indegree equal to ≥ 2. A contradiction.
(2) More generally, for a non root a 1 , there is at most one path a 1 , . . . , a n that connects any a 1 to any a n . Indeed, if there are two, we augment each by the path that connects the root r to a 1 and thus have contradicted what we just established above.
(3) A path a 1 , . . . , a n cannot contain points in more than one subtree,
i , of r , for, say, that distinct a i , and a j are in distinct
q , and
p , in that order. But then we have two distinct paths from r to a n , one via a i , and the other via a j . These paths differ already in their first edge: One is from r to r q while the other is from r to r p .
(4) A cycle is a path a 1 , . . . , a n of at least two distinct nodes such that a 1 \= a n . A tree can have no cycles . By way of contradiction: If the above path is a cycle, then it cannot contain the root, since there can be no edge pointing to the root. Consider then a path from r to a n :
b 1 , . . . , b m (*)
where b 1 = r and b m \= a n . Without loss of generality, the node b m −1 is not one of the a i —otherwise, we just walk backwards along the path (*) until we find the first b j that is not an a i —such a b j
exists (why?)—and use this b j instead. But then the indegree of a n (respectively, of b j +1 ) is at least 2.
4.4.0.17 Definition. (Levels) We define levels for every node in a tree sequentially (iteratively): The root is assigned level 0. If b is any child of a and a has level i assigned, then b is assigned level i + 1.
Remark 4.4.0.16 ensures that in the process of assigning levels every node is processed, and hence assigned a level, only once, and therefore levels are uniquely determined.
4.4.0.18 Remark. The length of a path from the root of a parse tree to a leaf equals the maximum level possible along this path, for the path cannot be extended at the leaf, and hence this is the node with the the maximum possible level along the path .
4.4.0.19 Definition. (Height) The one-node tree of case (1) in 4.4.0.9 has height equal to 0. If the root of the tree
has subtrees
i of heights h i respectively, then the height of
is 1 + max{ h i : i = 1, . . . , k } [cf. 4.4.0.9 , (2)].
4.4.0.20 Proposition. The height of a tree equals the maximum level observed in the tree .
The maximum level observed in the tree is the length of the longest path (from the root to a leaf) in the tree (cf. 4.4.0.18 ). Despite “the”, a path that scores maximum length is not unique. For example, in 4.4.0.11 , the third tree has three longest paths; all have length 2: (a) S , S , 0; (b) S , S , S ; and (c) S , S , 1.
Thus, another way to state the proposition is that the height of a tree equals the maximum path length among all paths from the root to a leaf .
Proof . Induction on trees. A one-node tree has height 0 and the maximum observed level in it is 0.
Assume now the claim for the “small” trees
i , for i = 1, . . . , k , of heights h i , respectively, for i = 1, . . . , k . Consider
=
r ,
1 , . . . ,
k
of height h . Let l be the length of the longest path in
. We want to show that h = l .
Now the maximum among all longest path lengths observed in the
i —of a path from r i to a leaf—is one less that l since all r i are children of r . Since this l − 1 equals max { h i : i = 1, . . . , k } by the I.H., we have that l = 1 + max{h i : i = 1, . . . , k } = h .
4.4.0.21 Proposition. Given a CFG G whose rules have right hand sides with length bounded by the number n. Then any parse tree of height h has a yield of length at most n h .
Proof . Induction on trees. For a one-node tree the height is 0. The yield has length 0 or 1 according as the label is
or a
∑. That is, at most n 0 .
Assume the claim for all subtrees
i , i = 1, . . . , k of the root. By assumption on the grammar rules, k ≤ n . The yields of these subtrees satisfy
The yield of the entire tree is α 1 α 2 . . . α k and the tree height is 1 + max( h i ). Let, without loss of generality, h 1 = max( h i ). Notice that
4.4.0.22 Remark. What if n = 1 in 4.4.0.21 ? The analysis above works, noting k = 1 = n . In particular, the result means that such a grammar generates a finite set, indeed, a subset of ∑ ∪ {
}.
4.4.0.23 Corollary. Given a CFG G whose rules have right hand sides with length bounded by the number n. If the yield α of a parse tree of G has length > n C , then the height of the parse tree is > C .
Proof . A height h ≤ C leads to yields of length at most n h , but n h ≤ n C .
Here is the import of parse trees: they depict derivations in a two-dimensional manner and help to simplify the analysis of derivations.
4.4.0.24 Theorem. Let a CFG G be given. Then we have a parse tree with root label A and yield α iff we have A
* α in G .
Proof . For the only if , let us have a parse tree as described. We proceed by induction on parse trees (cf. 4.4.0.9 ). The one-node case, combined with the assumption, gives us a tree with just one node labeled A . The yield is A . On the other hand, A
* A .
By taking the I.H. on subtrees, let our parse tree have subtrees
i , i = 1, . . . , k , with root labels Ξ i , i = 1, . . . k , where each Ξ i is in V ∪ ∑ ∪ {
}. 136 Let the respective yields be α i . By 4.4.0.9 ,
By the I.H. we have
where if Ξ i
∑ ∪ {
}, then (2) holds as equality without needing any help from the I.H.
By 4.4.0.9 , G contains the rule A → Ξ 1 · · · Ξ k , hence, from (1) and (2), we obtain A
Ξ 1 · · · Ξ k
* α.
For the if we do induction on n in the hypothesis A
n α. For n = 0 we have α = A . Thus the one-node parse tree with root label A will do. We assume the claim (I.H.) for all m < n . Thus we look at
We have from above
where α = α 1 · · · α k . Note that if k > 1, then no Ξ i can be
—we just don’t write rules like this: A →
Ba ; we simply write A → Ba [cf. also the restriction in case (2) of 4.4.0.9 ].
With this understanding noted, we analyze (4) to see what kinds of parse subtrees we can obtain with root labels Ξ i : If k = 1 and Ξ 1 =
, then we have a tree such as the first one in 4.4.0.11 with root label
—that is, Ξ 1 , and hence yield
= α 1 ; the I.H. was not needed in this subcase. Similarly, if Ξ i = a
∑, then again, without invoking the I.H., we have at once a two-node parse tree with root label a (this is Ξ i ) and with yield a = α i .
The I.H. implies that, in general, we have k parse trees
i , with roots labeled Ξ i and yields α i , for i = 1, . . . , k . By 4.4.0.9 and the first
in (3), we select a new root node, we label it A , and we connect this root, from left to right, with the roots of the
1 , . . . ,
k . The yield of the tree we have just built is α 1 . . . α k ( 4.4.0.9 ). We are done.
We now turn to the main result of this section, a pumping lemma for the CFL.
4.4.0.25 Theorem. ( The uvwxy - Theorem ) For any infinite CFL, L, there is a constant C (not uniquely determined by L !) such that if z
L and | z | ≥ C then we can partition z as z = uvwxy so that the following hold
(1) uv i wx i y
L for all i ≥ 0
(2) vx ≠
(3) | vwx | ≤ C
Proof . First, let us fix a CFG G =
, ∑, S ,
such that L = L ( G ). Let n represent the maximum length of the right hand side of any rule of G . The assumption that L is infinite implies n ≥ 2 ( 4.4.0.22 ). We will take C = n | V |+1 , where by the symbol |
| we denote the number of nonterminals in
. We next fix a z
L such that | z | ≥ C . By the choice of z , we have S
* z . By Theorem 4.4.0.24 , there is a parse tree with root S and yield z .
Of all such parse trees we pick one with the least number of nodes . (*)
We depict this parse tree—in a stylized manner, cf. 4.4.0.12 —in the figure below. We have denoted some of its nodes (one labeled S and two labeled A ) by big dots rather than by circles. The node labels are near these dots. Now, by 4.4.0.23 , the height h of the tree that we have chosen satisfies h > |
|. Moreover, by 4.4.0.20 , there is a path of longest possible length that has length precisely equal to the height of the tree . We depict this path by the wavy line that goes from S , through the two A -labels, to the unlabeled dot that is part of the yield of the bottom “triangle” with root A . This unlabeled dot is labeled by a terminal, since the entire yield, z , is in ∑*.
The longest path that we are referring to thus has length greater than |
|, and therefore has more than |
| + 1 nodes on it. Since only the dot in the yield is labeled by a terminal, this path has utilized more than |
| nonterminal labels. Thus a nonterminal label repeats .
We depict label A as repeating. Moreover, we assume that there is no label B , between the bottommost A and the last dot (on the yield) on the wavy path, that also repeats. That is, the pair of the two A s is bottommost.
With these preliminaries out of the way, we draw attention to the substrings of z marked on the yield. Notice that if we remove the middle parse tree—of root (labeled) A and yield vAx — and join the first and last tree at A we obtain a parse tree with root S and yield uwy . Thus S
* uwy and we have proved (1), case i = 0.
Let us next form a chain of triangles (parse trees),
1 , . . . ,
k using a copy of the middle triangle k > 1 times. For each i , we join
i with
i +1 , using as connecting point the node labeled A in the yield of the former with the root (also labeled A ) of the latter. By 4.4.0.9 , the chain is a parse tree with yield v k Ax k . We can now replace the single copy of the middle triangle of the preceding figure by the k -long triangle chain that we have just constructed. The root of the chain is glued to the A -node in the yield of the top triangle. The bottom triangle’s root is attached to the bottom A of the chain (the one in the yield of the chain). By 4.4.0.9 we obtained a parse tree with a root labeled S and yield uv k wx k y . Thus S
* uv k wx k y , and have just proved (1) for i > 1. For i = 1, of course, we need do nothing.
Regarding (2), if vx =
, then the tree we got for i = 0 still has yield z . Since this has at least one node less than the original, we have contradicted our stipulation (*). Thus, at least one of v or x is not
.
Finally, the wavy path from the first A , via the second A that ends on the yield of the bottom triangle, has the longest length in the parse tree,
′, formed by the last two triangles (root labeled A , yield equal to vwy ). If not, then there is a longer path in
′, from the root, labeled A , to the tree’s yield. Appending this path to the path in the top triangle that goes from S to A , we get an overall path in the original tree—from S to the yield z —that is longer than the original, and this contradicts our assumptions!
Now, omitting the first A , the resulting truncated path has length l ≤ |
|— otherwise we have a repeating nonterminal on it; 137 impossible by the way we chose the pair of A . Since l + 1 is the longest path length in
′, it equals the height of
′ ( 4.4.0.20 ). Thus, l + 1 ≤ |
| + 1 and hence the yield, vwx , satisfies | vwx | ≤ n l +1 ≤ n | v |+1 \= C —that is, (3) is true.
The condition “For any infinite CFL, L , etc.” in the statement of the theorem was used to guarantee that a CFG for the grammar cannot have all its rules have right hand sides of length one. The proof is applicable for any CFL L that can be obtained as L = L ( G ) where the CFG G has rules with right hand sides of length ≥ 2.
4.4.0.26 Example. We show that L = {0 n 1 n 2 n : n ≥ 0} is not a CFL. If it is, then the uvwxy -theorem applies. Let then C be as in the 4.4.0.25 , and consider z = 0 C 1 C 2 C . This z must pump, i.e., there must be a partition as z = uvwxy , so that vx ≠
and the uv i wx i y are in L for all i . Seeing that | vwx | ≤ C , we have two cases: v contains a 0. Then vwx contains no 2, hence pumping leaves 2 C invariant and the pumped string cannot possibly be in L . On the other hand, if v contains no 0, then the part 0 C remains invariant during pumping, and again the string cannot be in L .
4.4.0.27 Example. The language L = { zz : z
∑*} over ∑ = {0, 1} is not a CFL either.
Assume by way of contradiction that it is. So let C be chosen as in the uvwxy -theorem. We will show that certain strings of L that are longer than C “cannot pump”, contradicting 4.4.0.25 ( we do not expect this contradiction to be demonstrated for all strings of L! ).
“Cannot pump” means that if we do pump them as in (1) of 4.4.0.25 , then we end up with strings outside L .
So look at t = 0 C 1 C 0 C 1 C . Indeed, if we went along with the italicized hypothesis, then we should be able to write t = uvwxy with | vwx | ≤ C , and uv i wx i y would all be in L , for i ≥ 0. The latter cannot happen:
In view of the maximum length of vwx , this string can have three general positions as a substring of t :
(1) It has no overlap with the last 0 C 1 C . This is inconsistent with the specification of L , since pumping t down ( i = 0) will change at least one of the leftmost 0 C or the leftmost 1 C but will leave the the last 0 C 1 C invariant.
(2) It has no overlap with either the first 0 C or the last 1 C . This is again inconsistent with the specification of L , since pumping t down will change at least one of the leftmost 1 C or the second 0 C but will leave the first 0 C and the last 1 C invariant.
(3) It has no overlap with the first 0 C 1 C . For one last time, this clashes with the specification of L , since pumping t down will change at least one of the rightmost 0 C or the last 1 C but will leave the first 0 C 1 C invariant.
These being the only possible placements of vwx we showed that t “cannot pump”, hence L is not a CFL.
The following is analogous to 3.4.2.3 .
4.4.0.28 Example. The CFL version of the pumping lemma gives us a characterization of infinite (and hence of finite) CFL: Let L be a CFL given by some CFG G . If all the rules of G have right hand side lengths equal to one, then the language is finite, as we have already observed.
Let then this not be the case, and let C be the constant used in the proof of 4.4.0.25 . Then if there is a string z
L such that C ≤ | z | < 2 C , L will be infinite.
This is hardly surprising, as it directly follows from “pumping” the string. However, we have a converse! If L is infinite, then there will be a z ′
L such that C ≤ | z ′| < 2 C .
So, let L be infinite, and let z —no prime—be in L , such that its length is smallest such that | z | ≥ 2 C . Let z = uvwxy as in 4.4.0.25 . We have that uwy
L . By smallest length property of z , we obtain (recall, vx ≠
)
Moreover,
| uwy | = | z | − | vx | ≥ | z | − | vwx | ≥ 2 C − C = C
We set z ′ = uwy and we are done. The contrapositive of the characterization says that L is finite iff there are no z
L in the length-range C ≤ ··· < 2 C .