CSC 341: February 16, 2007
From CSWiki
Contents |
Instructor's notes for the class session
Theorem 2.20: A language is context-free if, and only if, some pushdown automaton recognizes it.
The theorem is an implicit universal quantification ("For every language, ...") enclosing a biconditional. For the universal quantification, we use the pattern of showing that the enclosed proposition holds for an arbitrary language about which we start with no assumptions. For the enclosed biconditional, we use the "first left to right, then right to left" pattern, breaking it into two conditionals (Lemmas 2.21 and 2.27). For each conditional, we use the pattern of assuming the protasis and deducing the apodosis from it. And each of those deductions is a proof by construction of an existential quantification.
Lemma 2.21: If a language is context-free, then some pushdown automaton recognizes it.
Suppose A is context-free. By definition (an unnumbered definition on page 101 of Sipser), there is a context-free grammar (V,Σ,R,S) that generates A. The goal is to construct a pushdown automaton that somehow simulates or reflects the capabilities of this grammar so closely that it recognizes the same language, A. We can design the PDA incrementally as follows:
- The input alphabet for the pushdown automaton is Σ.
- Its stack alphabet is
.
- Begin with a start state, qstart.
- Add a new state (unlabelled in Sipser) and a transition to it from qstart, reading no input, popping nothing from the stack, and pushing a bottom-of-stack marker $ onto the stack.
- Add another new state, qloop, and a transition to it from the unlabelled state just mentioned, reading no input, popping nothing from the stack, and pushing the start symbol onto the stack.
- For each symbol
, add a transition from qloop to qloop that reads a from the input, pops a from the stack, and pushes nothing onto the stack. This transition cannot be used unless the same symbol appears both in the input and on top of the stack, so in effect it matches the input symbol to the stack symbol, discarding both.
- Now go through the rules of the grammar. For each rule, our objective is to give the automaton the option of replacing the variable on the left-hand side of the rule, when it appears on top of the stack, with the symbols on the right-hand side of the rule.
- If the right-hand side of the rule is
, add a transition from qloop to qloop that reads nothing from the input, pops the left-hand-side variable from the stack, and pushes nothing onto the stack.
- If the right-hand side of the rule is a single symbol, either a variable or a terminal, add a transition from qloop to qloop that reads nothing from the input, pops the left-hand-side variable from the stack, and pushes the right-hand-side symbol onto the stack.
- Otherwise, let n be the number of symbols on the right-hand side. We'll need to add n − 1 extra states
to the automaton, since we'll have to push those symbols onto the stack one by one. Also, add a transition from qloop to q1, reading nothing from the input, popping the left-hand-side variable from the stack, and pushing the last of the right-hand-side symbols onto the stack; we push the last symbol to be matched onto the stack first so that it will be the one farthest down when we get back to qloop. Then we add transitions from q1 to q2, from q2 to q3, and so on, each time reading nothing from the input, popping nothing from the stack, and pushing the rightmost remaining symbol from the right-hand side onto the stack. Finally, to deal with the first of the symbols from the right-hand side, we add a transition from qn − 1 to qloop.
- If the right-hand side of the rule is
- Finally, we add one more state, qaccept, which is the only accept state, and one more transition, from qloop to qaccept, reading nothing from the input, popping the bottom-of-stack marker from the stack, and pushing nothing onto the stack.
Sipser, exercise 2.11.
Any leftmost derivation in the grammar can now be simulated on the PDA that we have constructed, and any accepting computation in the PDA can be simulated by a derivation in the grammar, so the language generated by the grammar and the language recognized by the PDA are identical.
Lemma 2.27: For any pushdown automaton P, the language recognized by P is context-free.
Without loss of generality ... This handy phrase typically occurs near the beginning of a proof and signals that some kind of preprocessing may be needed to establish some assertion that is subsequently assumed. There may be some previously proven theorem or lemma that establishes that the preprocessing can always be done, or in some cases in may be obvious.
Question: Is it obvious that, for any pushdown automaton, there is an equivalent pushdown automaton that has a single accept state, empties its stack before accepting, and has only transitions that pop without pushing or push without popping?
Given such a PDA, (Q,Σ,Γ,δ,q0,F), we construct an equivalent grammar:
- The variables are Apq for all
.
- The start symbol is
.
- The terminals are the elements of the PDA's input alphabet.
- Go through the transition function of the PDA and find all the cases in which
and
, for any
, any
, and any
. Each time this configuration is found, add the rule
to the grammar.
- For all
, add the rule
to the grammar.
- For all
, add the rule
to the grammar.
The idea is that a string of terminals can be derived from a variable Apq in this grammar if, and only if, it can drive the pushdown automaton from state p (and an empty stack) to state q (leaving the stack empty). The fact that the grammar generates the same language as the PDA is then the special case in which p = q0 and q = qaccept.
Now we can simulate any computation in the PDA in the grammar (Claim 2.31) and any computation in the grammar in the PDA (Claim 2.30), so again the language recognized by the PDA is identical with the language generated by the grammar.
Study questions
- Sipser says that "every finite automaton is automatically a pushdown automaton that ignores its stack" (page 122), but that is technically not quite correct, because of the differences between the formal definitions of the two categories. Given a deterministic finite automaton M = (Q,Σ,δ,q0,F), how would you construct a pushdown automaton P = (Q',Σ,Γ,δ',q0',F') that recognizes the same language?
- Sipser's solution (page 133) to Problem 2.18 omits a key part of the construction: the transition function for the pushdown automaton P'. Remedy this defect by giving a complete definition of that function.
- Using the construction plan that Sipser presents in his proof of Lemma 2.27, construct a context-free grammar that generates the language that the pushdown automaton M3 (in Example 2.18) recognizes.
Proposed Solutions
- Q' = Q; Γ = {}; If δ(qi,a) = qj for some
, and some
, then
.
-
-

