MIT 18.404 — Lecture 5

Context-Free Pumping Lemma, Turing Machines

Problem set 2 is due on Tuesday, October 13th at noon. Quiz 1 is from 7:30 PM to 9 PM in Walker on October 6th.

§ Context-Free Languages: Miscellaneous

Recall from Lecture 4 the following theorem (for which we only proved one direction).

Theorem. (CFL == PDA) A language is context-free if and only if it is recognized by some PDA.

The PDA   ⟹  \implies CFL direction is proven by a similar strategy as used to show GNFA   ⟹  \implies Regular Expression.

It turns out that the following are also true; these are optional exercises in problem set 2.

Remark. Outside of context-free languages, one could also talk about context-sensitive languages whose rules look like:

      A ::= 'a' B 'b' | 'c'     // (1)
  A 'a' ::= B 'c'               // (2)
'c' A B ::= 'x'                 // (3)

Rules (2) and (3) depend on the surrounding context; these rules are not allowed for context-free languages.

§ Context-Free Pumping Lemma

Just as we had a pumping lemma for regular languages, we have one for CFLs too.

Theorem. (CFL Pumping Lemma) For every CFL AA, there must be some pumping length pp such that:

For all s∈As \in A with ∣s∣≥p|s| \geq p, we may write s=uvxyzs = uvxyz with vy≠ϵvy \neq \epsilon such that ∣vxy∣≤p|vxy| \leq p and uvixyiz∈Auv^ixy^iz \in A for all i∈Ni \in \mathbb{N}.

Proof: Any very long string s∈As \in A must have a very tall parse tree. In particular, this very tall parse tree must have some variable RR that appears twice along some downward branch of the tree.

So long as such a variable RR is found, we can pump as pictured above; in short, duplicate the subtree that goes R→RR \to R. There remain some details to be filled in—and, furthermore, some technical fixes our construction might need.

Question. What should pp be such that (i) some variable RR must appear twice down a single path, and (ii) ∣vxy∣≤p|vxy| \leq p must hold?

Let bb denote the length of the longest replacement string in the CFG, and let VV be its variables. We claim p=b∣V∣+1p = b^{|V| + 1} works.

Consider the longest root-to-leaf path in the parse tree. Since ∣s∣≥p=b∣V∣+1|s| \geq p = b^{|V| + 1}, this path must have height at least ∣V∣+1|V| + 1. So among the lowest ∣V∣+1|V| + 1 edges of this path, some variable RR must appear twice, as needed.

Furthermore, because the higher of the two RRs has height at most ∣V∣+1|V| + 1, it must be that ∣vxy∣≤b∣V∣+1=p|vxy| \leq b^{|V| + 1} = p.   □\square

Question. How can we guarantee that vy≠ϵvy \neq \epsilon in our construction?

Suppose that v=ϵv = \epsilon and y=ϵy = \epsilon. Then the parse tree can be shrunk (without changing ss) by replacing the subtree rooted at the upper RR with the subtree rooted at the lower RR.

So we can prevent vy=ϵvy = \epsilon by only considering the parse tree of ss with the fewest nodes.   □\square

And that's the proof.   ■\blacksquare

Problem. Prove that the language C:={akbkck∣k≥0}C := \{\str{a}^k \str{b}^k \str{c}^k \mid k \geq 0\} is not a CFL.

Solution: Suppose FTSOC that it is a CFL with pumping length pp. Then consider s=apbpcp∈Cs = \str{a}^p\str{b}^p\str{c}^p \in C.

Say it splits like s=uvxyzs = uvxyz as in the pumping lemma. Since ∣vxy∣≤p|vxy| \leq p, the substring vxyvxy can cover at most two of the three blocks {ap,bp,cp}\{\str{a}^p, \str{b}^p, \str{c}^p\}. So pumping up must increase some letter's frequency while leaving another letter's frequency unchanged. But then the resulting word is not in CC, contradiction.   ■\blacksquare

Remark. Earlier in this lecture, we said A∩BA \cap B is not necessarily a CFL because it necessitates a PDA architecture with two stacks. This example shows why two stacks are stronger than one stack—the language CC is easily recognizable given two stacks!

Problem. Prove that the language D:={w∣w has equal #s of a,b,c}D := \{w \mid w \text{ has equal \#s of } \str{a}, \str{b}, \str{c}\} is not a CFL.

Solution: You could use the same pumping-lemma contradiction apbpcp\str{a}^p\str{b}^p\str{c}^p as in the previous proof… or you could recall:

Proposition. The intersection of a CFL and a regular language is also a CFL.

So if DD were a CFL, then so would be D∩a∗b∗c∗=CD \cap \str{a}^*\str{b}^*\str{c}^* = C. But we know CC is not a CFL, contradiction.   ■\blacksquare

Here's an example where the choice of string ss is less obvious.

Problem. Prove that the language B:={ww∣w∈{0,1}∗}B := \{ww \mid w \in \{\str{0}, \str{1}\}^*\} is not a CFL.

Solution: You can check that s=0p10p1∈Bs = \str{0}^p\str{1}\str{0}^p\str{1} \in B does not raise a contradiction via the pumping lemma. Uh oh!

But the string s=0p1p0p1p∈Bs = \str{0}^p\str{1}^p\str{0}^p\str{1}^p \in B does raise a contradiction, so we win anyway.   ■\blacksquare

§ Turing Machines

A Turing machine (TM) is like a PDA, but instead of owning a stack, the machine owns a read/write tape.

Definition. A Turing machine is a 7-tuple M=(Q,Σ,Γ,δ,q0,qacc,qrej)M = (Q, \Sigma, \Gamma, \delta, q_0, q_{\mathrm{acc}}, q_{\mathrm{rej}}), where:

Warning!   Whereas PDAs and FAs read in the input one character at a time from left to right… Turing machines read in the input off of the tape itself, and may read it in any way they choose. (So in the example image above, the input ww is babaisMe\str{babaisMe}.)

Note that for any word ww, a Turing machine MM either accepts ww, rejects ww, or never halts.

Remark. There is no sense in which a Turing machine “finishes reading the input”—again, because Turing machines do not read the input from left-to-right like PDAs and FAs do. So halting can only occur by arriving at the accept or reject state.

Definition. A language AA is Turing-recognizable if A=L(M)A = L(M) for some Turing machine MM.

Definition. A language AA is decidable if A=L(M)A = L(M) for some Turing machine MM that halts on every input.