Conversion of CFG To PDA Conversion of PDA To CFG
Conversion of CFG To PDA Conversion of PDA To CFG
Conversion of CFG To PDA Conversion of PDA To CFG
1
Overview
When we talked about closure
properties of regular languages, it was
useful to be able to jump between RE
and DFA representations.
Similarly, CFG’s and PDA’s are both
useful to deal with properties of the
CFL’s.
2
Overview – (2)
Also, PDA’s, being “algorithmic,” are
often easier to use when arguing that a
language is a CFL.
Example: It is easy to see how a PDA
can recognize balanced parentheses;
not so easy as a grammar.
But all depends on knowing that CFG’s
and PDA’s both define the CFL’s.
3
Converting a CFG to a PDA
Let L = L(G).
Construct PDA P such that N(P) = L.
P has:
One state q.
Input symbols = terminals of G.
Stack symbols = all symbols of G.
Start symbol = start symbol of G.
4
Intuition About P
Given input w, P will step through a
leftmost derivation of w from the start
symbol S.
Since P can’t know what this derivation
is, or even what the end of w is, it uses
nondeterminism to “guess” the
production to use at each step.
5
Intuition – (2)
At each step, P represents some left-
sentential form (step of a leftmost
derivation).
If the stack of P is , and P has so far
consumed x from its input, then P
represents left-sentential form x.
At empty stack, the input consumed is a
string in L(G).
6
Transition Function of P
1. δ(q, a, a) = (q, ε). (Type 1 rules)
This step does not change the LSF
represented, but “moves” responsibility for a
from the stack to the consumed input.
2. If A -> is a production of G, then
δ(q, ε, A) contains (q, ). (Type 2 rules)
Guess a production for A, and represent the
next LSF in the derivation.
7
Proof That L(P) = L(G)
We need to show that (q, wx, S) ⊦* (q,
x, ) for any x if and only if S
=>*lm w.
Part 1: “only if” is an induction on the
number of steps made by P.
Basis: 0 steps.
Then = S, w = ε, and S =>*lm S is surely
true.
8
Induction for Part 1
Consider n moves of P: (q, wx, S) ⊦*
(q, x, ) and assume the IH for
sequences of n-1 moves.
There are two cases, depending on
whether the last move uses a Type 1 or
Type 2 rule.
9
Use of a Type 1 Rule
The move sequence must be of the form
(q, yax, S) ⊦* (q, ax, a) ⊦ (q, x, ),
where ya = w.
By the IH applied to the first n-1 steps,
S =>*lm ya.
But ya = w, so S =>*lm w.
10
Use of a Type 2 Rule
The move sequence must be of the form
(q, wx, S) ⊦* (q, x, A) ⊦ (q, x, ),
where A -> is a production and = .
By the IH applied to the first n-1 steps,
S =>*lm wA.
Thus, S =>*lm w = w.
11
Proof of Part 2 (“if”)
We also must prove that if S =>*lm w,
then (q, wx, S) ⊦* (q, x, ) for any x.
Induction on number of steps in the
leftmost derivation.
Ideas are similar; read in text.
12
Proof – Completion
We now have (q, wx, S) ⊦* (q, x, ) for
any x if and only if S =>*lm w.
In particular, let x = = ε.
Then (q, w, S) ⊦* (q, ε, ε) if and only if
S =>*lm w.
That is, w is in N(P) if and only if w is in
L(G).
13
From a PDA to a CFG
Now, assume L = N(P).
We’ll construct a CFG G such that L = L(G).
Intuition: G will have variables generating
exactly the inputs that cause P to have the
net effect of popping a stack symbol X
while going from state p to state q.
P never gets below this X while doing so.
14
Variables of G
G’s variables are of the form [pXq].
This variable generates all and only the
strings w such that
(p, w, X) ⊦*(q, ε, ε).
Also a start symbol S we’ll talk about
later.
15
Productions of G
Each production for [pXq] comes from a
move of P in state p with stack symbol X.
Simplest case: δ(p, a, X) contains (q, ε).
Then the production is [pXq] -> a.
Note a can be an input symbol or ε.
Here, [pXq] generates a, because reading
a is one way to pop X and go from p to q.
16
Productions of G – (2)
Next simplest case: δ(p, a, X) contains
(r, Y) for some state r and symbol Y.
G has production [pXq] -> a[rYq].
We can erase X and go from p to q by
reading a (entering state r and replacing
the X by Y) and then reading some w that
gets P from r to q while erasing the Y.
Note: [pXq] =>* aw whenever
[rYq] =>* w.
17
Productions of G – (3)
Third simplest case: δ(p, a, X) contains
(r, YZ) for some state r and symbols Y
and Z.
Now, P has replaced X by YZ.
To have the net effect of erasing X, P
must erase Y, going from state r to
some state s, and then erase Z, going
from s to q.
18
Picture of Action of P
a w x w x x
p r s q
X Y Z
Z
19
Third-Simplest Case – Concluded
Since we do not know state s, we must
generate a family of productions:
[pXq] -> a[rYs][sZq]
for all states s.
[pXq] =>* awx whenever [rYs] =>* w
and [sZq] =>* x.
20
Productions of G: General Case
Suppose δ(p, a, X) contains (r, Y1,…Yk)
for some state r and k > 3.
Generate family of productions
[pXq] ->
a[rY1s1][s1Y2s2]…[sk-2Yk-1sk-1][sk-1Ykq]
21
Completion of the Construction
We can prove that (q0, w, Z0)⊦*(p, ε, ε)
if and only if [q0Z0p] =>* w.
Proof is in text; it is two easy inductions.
But state p can be anything.
Thus, add to G another variable S, the
start symbol, and add productions
S -> [q0Z0p] for each state p.
22