COMS W3261
Computer Science Theory
Lecture 13: October 21, 2009
PDA's and CFG's
1. Outline
Review
Deterministic pushdown automata (DPDA)
Constructing a PDA from a CFG
Constructing a CFG from a PDA
2. Review
Pushdown automata (PDA)
Instantaneous descriptions of PDA's
The language of a PDA
Language accepted by final state
Language accepted by empty stack
3. Deterministic Pushdown Automata
A PDA is deterministic (DPDA) if there is never a choice for a next move
in any instantaneous description.
A PDA
(Q, Σ, Γ, δ, q0, Z0, F)
is deterministic if:
δ(q, a, X) has at most one member for any q in Q,
a in Σ ∪ {ε} and X in Γ.
If δ(q, a, X) is nonempty for some a in Σ,
then δ(q, ε, X) must be empty.
A DPDA can recognize { wcwR | w
is any string of a's and b's }.
A PDA can recognize { wwR | w
is any string of a's and b's }, but no DPDA
can recognize this language.
If L is a regular language, then L can be recognized by a DPDA.
4. Constructing a PDA from a CFG
Given a CFG G, we can construct a PDA P such that
N(P) = L(G).
The PDA will simulate leftmost derivations of G.
Algorithm to construct a PDA for a CFG
Input: a CFG G = (V, T, Q, S).
Output: a PDA P such that N(P) = L(G).
Method: Let P = ({q}, T, V ∪ T, δ, q, S) where
δ(q, ε, A) =
{(q, β) | A → β is in Q }
for each nonterminal A in V.
δ(q, a, a) = {(q, ε)}
for each terminal a in T.
For a given input string w, the PDA simulates a leftmost derivation
for w in G.
We can prove that N(P) = L(G) by showing that
w is in N(P) iff w is in L(G):
If part: If w is in L(G), then there is a leftmost derivation
S = γ1 ⇒ γ2 ⇒ ... ⇒ γn = w
We show by induction on i that P simulates this leftmost
derivation by the sequence of moves
(q, w, S) |–*
(q, yi, αi)
such that if γi =
xiαi,
then xiyi = w.
Only-if part: If
(q, x, A) |–*
(q, ε, ε), then
A ⇒* x.
We can prove this statement by induction on the number of moves made
by P.
5. Constructing a CFG from a PDA
Given a PDA P, we can construct a CFG G such that
L(G) = N(P).
The basic idea of the proof is to generate the strings that cause
P to go from state q to state p,
popping a symbol X off the
stack, by a nonterminal of the form [qXp].
Algorithm to construct a CFG for a PDA
Input: a PDA P =
(Q, Σ, Γ, δ, q0, Z0, F).
Output: a CFG G = (V, Σ, R, S)
such that L(G) = N(P).
Method:
Let the nonterminal S be the start symbol of G.
The other nonterminals in V will be symbols of the form
[pXq] where p and q are states in Q,
and X is a stack symbol in Γ.
The set of productions R is constructed as follows:
For all states p, R has the production
S → [q0Z0p].
If δ(q, a, X) contains
(r, Y1Y2 … Yk),
then R has the productions
[qXrk] →
a[rY1r1]
[r1Y2r2] …
[rk-1Ykrk]
for all lists of states r1, r2, … ,
rk.
We can prove that [qXp] ⇒* w iff
(q, w, X) |–* (p, ε, ε).
From this, we have
[q0Z0p] ⇒* w iff
(q0, w, Z0) |–*
(p, ε, ε), so we can conclude
L(G) = N(P).
6. Practice Problems
Construct a PDA P such that N(P) = L(G)
where G is S → (S)S | ε.
Construct a DPDA P such that N(P) = L(G)
for the grammar above.
Construct a PDA P such that N(P) = L(G)
where G is S → aSb | bSb | ε.
Construct a DPDA P such that N(P) = L(G)
where G is S → aSb | bSb | c.