Examples Using the Pumping Theorem for Context-free Languages

Learn the formal statement of the pumping theorem for CFLs and explore some example applications to prove that a language is not context-free.

Just as with regular languages, we can use this pumping theorem to show that a language is not context-free. The “rules of the game” are the same as for the pumping theorem for regular languages: we get to choose any qualifying string, but then we must show that there is no possibility of satisfying this pumping theorem. As usual, if a language is “pumpable”, we can draw no conclusions—the language may or may not be context-free.

Example 1

The language anbncna^nb^nc^n is not context-free. If it were, we could partition the string apbpcpa^pb^pc^p into five substrings, uvxyzuvxyz, such that uvixyizuv^ixy^iz also has runs of equal size of each letter. Since vxyp|vxy| \leq p, there are five cases to consider:

  1. The variable vxyvxy appears wholly within the initial run of aa’s. In this case, as vv and yy are pumped, the number of aa’s increases while the number of the other letters doesn’t change, failing the pumping theorem.
  2. The variable vxyvxy contains both aa’s and bb’s. We may assume that vxy=akbm,m+kpvxy=a^kb^m, m+k\leq p. In this case, the number of aa’s and/or bb’s changes, but the cc’s do not change, again failing the pumping theorem.
  3. The variable vxyvxy appears wholly within the run of bb's. In this case, as vv and yy are pumped, the number of bb’s increases while the number of the other letters doesn’t change, failing the pumping theorem, as in case 1 above.
  4. The variable vxyvxy contains both bb’s and cc’s. As in case 2, the number of bb’s and/or cc’s changes, but the aa’s do not change, again failing the pumping theorem as in case 2 above.
  5. The variable vxyvxy appears wholly within the run of cc's. In this case, as in cases 1 and 3 above, as vv and vv are pumped, the number of cc’s increases while the number of the other letters doesn’t change, failing the pumping theorem.

Example 2

The language of two equal halves, wwww, is not context-free. Consider the string apbpapbpa^pb^pa^pb^p. Once again, since vxypvxy\leq p, it is not possible for the various parts of the string to pump while keeping the halves of the string equal. There are seven cases to consider:

  1. The variable vxyvxy appears within the initial run of aa’s. In this case, as vv and yy are pumped, the number of initial aa’s increases while the number of aa’s in the third run doesn’t change, failing the pumping theorem.
  2. The variable vxyvxy contains both aa’s and bb’s from the first two runs. We can assume that vxy=akbm,m+kpvxy=a^kb^m, m+k\leq p. In this case, the number of aa’s and/or bb’s in the first half changes, but the two trailing runs of letters do not, again failing the pumping theorem.
  3. The variable vxyvxy appears only within the first run of bb's. In this case, the number of initial bb’s changes but the second run of bb’s does not, similar to case 1 above.
  4. The variable vxyvxy contains bb’s followed by aa’s, failing in the same manner as case 2.
  5. The variable vxyvxy is contained in the second run of aa’s, which is analogous to cases 1 and 3.
  6. The variable vxyvxy contains aa’s and bb’s from the second half of the string, failing in the same way as cases 2 and 4.
  7. The variable vxyvxy is in the last run of bb’s, failing the same way as cases 1, 3, and 5.

Example 3

The language an2,Σ={a}a^{n^2}, \Sigma=\{a\} is not context-free. Consider the string s=ap2=uvxyzs=a^{p^2}=uvxyz. Since vxyp|vxy|\leq p, we know that vyp|vy|\leq p. We will show that pumping up once to uv2xy2zuv^2xy^2z does not reach the next square, a(p+1)2a^{(p+1)^2}.

The difference between the two strings of square lengths is (p+1)2p2=2p+1(p+1)^2-p^2=2p+1. However, the difference from pumping up once is:

Get hands-on with 1400+ tech skills courses.