Transcript Slide 1

PDAs accept CFGs
Theorem:
ContextFree
Languages
(Grammar
s)

Language
s
Accepted
by
PDAs
2
Proof - Step 1:
ContextFree
Languages
(Grammars)

Language
s
Accepted
by
PDAs
Convert any context-free grammar G
(
G
)

L
(
M
)
to a PDA M with: L
3
Proof - Step 2:
ContextFree
Languages
(Grammars)

Language
s
Accepte
d by
PDAs
Convert any PDA M to a context-free
(
G
)

L
(
M
)
grammar G with: L
4
Proof - step 1
Converting
Context-Free Grammars
to
PDAs
5
ContextFree
Languages
(Grammars)

Languag
es
Accepte
d by
PDAs
Convert any context-free grammar G
(
G
)

L
(
M
)
to a PDA M with: L
6
We will convert grammar
to a PDA M
G
such that:
M simulates leftmost derivations of G
7
Convert grammar G to PDA
Production in
M
Terminal in
G
A

w
G
a

,A

w a
,a


q0

,

S
q1
, $  $
q2
8
PDA computation
Grammar
leftmost
derivation
Simulates grammar
leftmost derivations
(q0,1kk1n ,$)
S

 1  k X1 X m
 (q1,1kk1n , S$)

 1  k k 1  n


 (q1,k1n , X1Xm$)
 (q2, ,$)
Leftmost
variable
9
Grammar
S  aSTb
Sb
T  Ta
T  
q0
Example
PDA
 , S  aSTb
, S  b
a, a
 , T  Ta
b, b
, T  

,

S
q1
, $  $
q2
10
Grammar
derivation
S
 aSTb
 abTb
 abTab
 abab
PDA computation
( q 0 , abab
,$)
 ( q 1 , abab
, S $)
 ( q 1 , bab , STb $)
 ( q 1 , bab , bTb $)
 ( q 1 , ab , Tb $)
 ( q 1 , ab , Tab $)
 ( q 1 , ab , ab $)
 ( q 1 , b , b $)
 ( q 1 ,  ,$)
 ( q 2 ,  ,$)
11
Derivation:
Input
Time 0
q0
a b a
b
 , S  aSTb
$
, S  b
Stack
a, a
 , T  Ta
b, b
, T  

,

S
q1
, $  $
q2
12
Derivation: S
Input
Time 0
q0
a b a
b
S
$
 , S  aSTb
, S  b
Stack
a, a
 , T  Ta
b, b
, T  

,

S
q1
, $  $
q2
13
Derivation: S

aSTb
Input
Time 1
q0
a b a
b
a
S
T
b
$
 , S  aSTb
, S  b
Stack
a, a
 , T  Ta
b, b
, T  

,

S
q1
, $  $
q2
14
Derivation: S

aSTb
Input
Time 2
q0
a b a
b
a
S
T
b
$
 , S  aSTb
, S  b
Stack
a, a
 , T  Ta
b, b
, T  

,

S
q1
, $  $
q2
15
Derivation: S

aSTb

abTb
Input
Time 3
q0
a b a
b
T
b
$
b
 , S  aSTb
, S  b
Stack
a, a
 , T  Ta
b, b
, T  

,

S
q1
, $  $
q2
16
Derivation: S

aSTb

abTb
Input
Time 4
q0
a b a
b
T
b
$
b
 , S  aSTb
, S  b
Stack
a, a
 , T  Ta
b, b
, T  

,

S
q1
, $  $
q2
17
Derivation: S

aST

abT

ab
Input
Time 5
q0
a b a
T
a
b
$
b
 , S  aSTb
, S  b
Stack
a, a
 , T  Ta
b, b
, T  

,

S
q1
, $  $
q2
18
Derivation: S

aST

ab

ab

a
Input
Time 6
q0
a b a
T
a
b
$
b
 , S  aSTb
, S  b
Stack
a, a
 , T  Ta
b, b
, T  

,

S
q1
, $  $
q2
19
Derivation: S

aST

ab

ab

a
Input
Time 7
q0
a b a
b
a
b
$
 , S  aSTb
, S  b
Stack
a, a
 , T  Ta
b, b
, T  

,

S
q1
, $  $
q2
20
Derivation: S

aST

ab

ab

a
Input
Time 8
q0
a b a
b
b
$
 , S  aSTb
, S  b
Stack
a, a
 , T  Ta
b, b
, T  

,

S
q1
, $  $
q2
21
Derivation: S

aST

ab

ab

a
Input
Time 9
q0
a b a
b
 , S  aSTb
$
, S  b
Stack
a, a
 , T  Ta
b, b
, T  

,

S
accept
q1
, $  $
q2
22
In general, it can be shown that:
Grammar
generates
string w
*
G
If and
Only if
Sw
Therefore
PDA M
accepts
w
(q
,w
,$)

(q
,
,$
0
2
L(G
)L(M)
23
Therefore:
For any context-free language
there is a PDA that accepts L
Context-Free
Languages
(Grammars)

L
Languages
Accepted by
PDAs
24
Proof - step 2
Converting
PDAs
to
Context-Free Grammars
25
Context-Free
Languages
(Grammars)

Languages
Accepted by
PDAs
Convert any PDA M to a context-free
(
G
)

L
(
M
)
grammar G with: L
26
We will convert PDA
M to
a context-free grammar
G
G such that:
simulates computations of
with leftmost derivations
M
27
Some Necessary Modifications
If necessary, modify the PDA so that:
1. The stack is never empty during computation
2. It has a single accept state
and empties the stack when it accepts a string
3. Has transitions without popping

28
1. Modify the PDA so that
the stack is never empty
during computation
Stack
a
$
$
OK
OK
NOT OK
29
Introduce the new symbol
the bottom of the stack
a
$
$
a
$
$
#
#
#
to mark
#
30
At the beginning insert
new
initial state
# into the stack
Original PDA
,$$#
original
initial state
31
Convert all transitions so that
after popping $ the automaton halts
pop $
qi

,

sq
j
x{#
}
pop
$
qi
, x  x

,

sq
j
halting state
32
2. Modify the PDA so that at end
it empties stack and
has a unique accept state
Empty stack
PDA
x{#
}

,x


, #   q
f
Old accept states
New
accept
state
33
3. Modify the PDA so that it has no
transitions popping  :
qi

,

y
q
j
qi

,

y
q
j
{#
}
34
Example of a PDA in correct form:
(modifications are not necessary)
L
(
M
)

{
w

{
a
,
b
}
:n
(
w
)

n
(
w
)}
a
b
*
a, $  0$
a, 0  00
a, 1  
b, $  1$
b, 1 11
b, 0  
q0

,$


qf
35
Grammar Construction
In grammar
G :
Variables:
A
PDA stack symbols
Terminals:
a
PDA input symbols
Start Variable:
$ or # Stack bottom symbol
36
PDA transition
Grammar production
q a, A q1
Aa
37
PDA transition
Grammar production
a
,
A

B
B

B
1
2
m q1
q
A

aB
B
B
1
2
m
38
Grammar
leftmost
derivation
PDA computation
S

 1  k X1 X m
(q1,1kk1n,$)

 1  k k 1  n


(q1,k1n, X1Xm)
(q2,,)
Leftmost
variable
39
Example PDA:
a, $  0$
a, 0  00
a, 1  
b, $  1$
b, 1 11
b, 0  
q0
Grammar:

,$


qf
$  a0$ $  b1$
0  a00 1  b11
$
1 a
0b
40
Grammar
Leftmost
derivation:
PDA
Computation:
$
( q 0 , abba ,$)
 a0$
 ( q 0 , bba , 0 $)
 ab $
 ( q 0 , ba ,$)
 abb 1 $
 ( q 0 , a ,1 $)
 abba
 ( q 0 ,  ,$)
 abba
$
 (q f ,  ,  )
41
Derivation:
a
$
Time 0
b b a
a, $  0$
a, 0  00
a, 1  
b, $  1$
b, 1 11
b, 0  
q0

,$


qf
42
Derivation:
a
$

a 0$
Time 1
b b a
a, $  0$
a, 0  00
a, 1  
b, $  1$
b, 1 11
b, 0  
q0

,$


0
$
Stack
qf
43
Derivation:
a
$
 a0$  ab$
Time 2
b b a
a, $  0$
a, 0  00
a, 1  
b, $  1$
b, 1 11
b, 0  
q0

,$


$
Stack
qf
44
Derivation:
a
$
 a0$  ab$  abb1$
Time 3
b b a
a, $  0$
a, 0  00
a, 1  
b, $  1$
b, 1 11
b, 0  
q0

,$


1
$
Stack
qf
45
Derivation:
$
 a0$  ab$  abb1$
abba$
a
Time 4
b b a
a, $  0$
a, 0  00
a, 1  
b, $  1$
b, 1 11
b, 0  
q0

,$


$
Stack
qf
46
Derivation:
a
 a0$  ab$  abb1$
abba$  abba
$
Time 5
b b a
a, $  0$
a, 0  00
a, 1  
b, $  1$
b, 1 11
b, 0  
q0

,$


empty
Stack
qf
47
However, this grammar conversion
does not work for all PDAs: (AA$??)
a, $A$
a, AA$
q0
b, A 
b, A 
q1
, $ 
qf
L
(
M
)
{
ab:n

1
}
nn
48
a, $A$
a, AA$
q0
b, A 
b, A 
Grammar:
$  aA$
$
q1
, $ 
qf
AaA$
Ab
49
Bad Derivation:
S

aA
$

aaA
$

aab
$

aab

L
(
M
)
Grammar:
$  aA$
$
AaA$
Ab
50
The Correct Grammar Construction
In grammar
G :
PDA stack symbol
Variables:
(qi Aq j )
PDA states
Terminals: Input symbols of PDA
51
PDA transition
q a, A q1
Grammar production
(qAq
a
1)
52
PDA transition
q
a
,A

B
B
B
1
2
m q
1
Grammar production
(
qAq
)

a
(
q
B
q
)(
q
B
q
)

(
q
B
q
)
m
1
1
2
2
2
3
m
m
m

1
m+1, not m
For all possible states q2,, qm1
in PDA
53
Stack bottom symbol
$ or #
Start Variable:
(qo Zq f )
Start state
accept state
54
Example:
a, $  0$
a, 0  00
a, 1  
b, $  1$
b, 1 11
b, 0  
q0
Grammar production:

,$


qf
(
q
1
q
)

a
0
0
55
Example:
a, $  0$
a, 0  00
a, 1  
b, $  1$
b, 1 11
b, 0  
q0

,$


qf
Grammar productions:
(
q
$
q
)

b
(
q
1
q
)(
q
$
q
)
|
b
(
q
1
q
)(
q
$
q
)
0
0
0
0
0
0
0
f
f
0
(
q
$
q
)

b
(
q
1
q
)(
q
$
q
)
|
b
(
q
1
q
)(
q
$
q
)
0
f
0
0
0
f
0
f
f
f
56
Example:
a, $  0$
a, 0  00
a, 1  
b, $  1$
b, 1 11
b, 0  
q0

,$


qf

q
$
q
)

Grammar production: (
0f
57
Resulting Grammar:
(
q
$
q
)
:
sta
ab
0
f
(
q
$
q
)

b
(
q
1
q
)(
q
$
q
)
|
b
(
q
1
q
)(
q
$
q
)
0
0
0
0
0
0
0
f
f
0
(
q
$
q
)

b
(
q
1
q
)(
q
$
q
)
|
b
(
q
1
q
)(
q
$
q
)
0
f
0
0
0
f
0
f
f
f
(
q
1
q
)

b
(
q
1
q
)(
q
1
q
)
|
b
(
q
1
q
)(
q
1
q
)
0
0
0
0
0
0
0
f
f
0
(
q
1
q
)

b
(
q
1
q
)(
q
1
q
)
|
b
(
q
1
q
)(
q
1
q
)
0
f
0
0
0
f
0
f
f
f
(
q
$
q
)

a
(
q
0
q
)(
q
$
q
)
|
a
(
q
0
q
)(
q
$
q
)
0
0
0
0
0
0
0
f
f
0
(
q
$
q
)

a
(
q
0
q
)(
q
$
q
)
|
a
(
q
0
q
)(
q
$
q
)
0
f
0
0
0
f
0
f
f
f
58
(
q
0
q
)

a
(
q
0
q
)(
q
0
q
)
|
a
(
q
0
q
)(
q
0
q
)
0
0
0
0
0
0
0
f
f
0
(
q
0
q
)

a
(
q
0
q
)(
q
0
q
)
|
a
(
q
0
q
)(
q
0
q
)
0
f
0
0
0
f
0
f
f
f
(q01
q0)
a
(q00q0)
b

(
q
$
q
)

0f
59
Grammar
Leftmost
derivation
PDA
computation
(q0 $q f )
( q 0 , abba ,$)

a
(q
0
q
)(
q
$
q
0
0
0
f)

ab
(q0$qf )

abb
(
q
1
q
)(
q
$
q
0
0
0
f)
 ( q 0 , bba , 0 $)
 ( q 0 , ba ,$)
 ( q 0 , a ,1 $)

abba
(q
qf )
0$
 ( q 0 ,  ,$)
 abba
 (q f ,  ,  )
60
Derivation:
a
(q0 $q f )
Time 0
b b a
a, $  0$
a, 0  00
a, 1  
b, $  1$
b, 1 11
b, 0  
q0

,$


qf
61
Derivation: (q0 $q f )  a
(q
q
q
qf )
00
0)(
0$
a
Time 1
b b a
a, $  0$
a, 0  00
a, 1  
b, $  1$
b, 1 11
b, 0  
q0

,$


0
$
Stack
qf
62
Derivation: (q0 $q f )  a
(q
q
q
qf ) ab(q0$qf )
00
0)(
0$
a
Time 2
b b a
a, $  0$
a, 0  00
a, 1  
b, $  1$
b, 1 11
b, 0  
q0

,$


$
Stack
qf
63
Derivation: (q0 $q f )  a
(q
q
q
qf ) ab(q0$qf )
00
0)(
0$
(q
1
q
)(
q
$
q
 abb
0
0
0
f)
a
Time 3
b b a
a, $  0$
a, 0  00
a, 1  
b, $  1$
b, 1 11
b, 0  
q0

,$


1
$
Stack
qf
64
Derivation: (q0 $q f )  a
(q
q
q
qf ) ab(q0$qf )
00
0)(
0$
(q
1
q
)(
q
$
q
(q0$qf )
 abb
0
0
0
f ) abba
a
Time 4
b b a
a, $  0$
a, 0  00
a, 1  
b, $  1$
b, 1 11
b, 0  
q0

,$


$
Stack
qf
65
Derivation: (q0 $q f )  a
(q
q
q
qf ) ab(q0$qf )
00
0)(
0$
(q
1
q
)(
q
$
q
(q0$qf )  abba
 abb
0
0
0
f ) abba
a
Time 5
b b a
a, $  0$
a, 0  00
a, 1  
b, $  1$
b, 1 11
b, 0  
q0

,$


empty
Stack
qf
66
In general:
Grammar
If and

Only
if
(qiAq
)

wB
j
PDA

(q
,A
)
(q
,B
)
i,w
j,
67
Thus:
Grammar
generates

w
(q0$qf )w
If and
Only if
PDA accepts
w

(q
,w
,$)

(q
,
)
0
f,
68
Therefore:
For any PDA
there is a context-free grammar
that accepts the same language
Context-Free
Languages
(Grammars)

Languages
Accepted by
PDAs
69