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 CFL direction is proven by a similar strategy as used to show GNFA Regular Expression.
It turns out that the following are also true; these are optional exercises in problem set 2.
The class of CFLs is closed under union , concatenation , and star .
The class of CFLs is not closed under intersection and complementation .
Roughly, is not necessarily a CFL because simulating the PDA of and the PDA of requires two stacks.
The intersection of a CFL and a regular language is also a CFL.
Roughly, is a CFL because simulating the PDA of and the NFA of still only requires one stack.
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 , there must be some pumping length such that:
For all with , we may write with such that and for all .
Proof: Any very long string must have a very tall parse tree. In particular, this very tall parse tree must have some variable that appears twice along some downward branch of the tree.

So long as such a variable is found, we can pump as pictured above; in short, duplicate the subtree that goes . There remain some details to be filled in—and, furthermore, some technical fixes our construction might need.
Question. What should be such that (i) some variable must appear twice down a single path, and (ii) must hold?
Let denote the length of the longest replacement string in the CFG, and let be its variables. We claim works.
Consider the longest root-to-leaf path in the parse tree. Since , this path must have height at least . So among the lowest edges of this path, some variable must appear twice, as needed.
Furthermore, because the higher of the two s has height at most , it must be that .
Question. How can we guarantee that in our construction?
Suppose that and . Then the parse tree can be shrunk (without changing ) by replacing the subtree rooted at the upper with the subtree rooted at the lower .
So we can prevent by only considering the parse tree of with the fewest nodes.
And that's the proof.
Problem. Prove that the language is not a CFL.
Solution: Suppose FTSOC that it is a CFL with pumping length . Then consider .
Say it splits like as in the pumping lemma. Since , the substring can cover at most two of the three blocks . So pumping up must increase some letter's frequency while leaving another letter's frequency unchanged. But then the resulting word is not in , contradiction.
Remark. Earlier in this lecture, we said 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 is easily recognizable given two stacks!
Problem. Prove that the language is not a CFL.
Solution: You could use the same pumping-lemma contradiction 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 were a CFL, then so would be . But we know is not a CFL, contradiction.
Here's an example where the choice of string is less obvious.
Problem. Prove that the language is not a CFL.
Solution: You can check that does not raise a contradiction via the pumping lemma. Uh oh!
But the string does raise a contradiction, so we win anyway.
§ 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 , where:
The usual NFA components have the same meanings as before… except for and .
The set of states is .
The input alphabet (the set of atoms accepted as input) is .
The start state is .
The accept state is . It is a halt state, meaning the machine halts upon reaching .
The reject state is . It is a halt state, meaning the machine halts upon reaching .
The tape alphabet (the set of atoms accepted by the tape) is , which must contain a blank symbol and everything in .
The tape initially holds the input word , followed by infinitely many instances of the blank symbol .
The transition function has type . So at each step, the Turing machine…
… reads in the current state and the atom at its tape position …
… then moves to a new state , writes an atom at its tape position, and moves left or right along the tape.
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 is .)
Note that for any word , a Turing machine either accepts , rejects , 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 is Turing-recognizable if for some Turing machine .
Definition. A language is decidable if for some Turing machine that halts on every input.