P. 1
PDA Accept Context Free

# PDA Accept Context Free

|Views: 220|Likes:

Categories:Types, School Work

See more
See less

08/17/2014

pdf

text

original

# PDAs Accept Context-Free Languages

Fall 2004

Costas Busch - RPI

1

Theorem: Context-Free Languages (Grammars)

!

Languages Accepted by PDAs

Fall 2004

Costas Busch - RPI

2

Proof - Step 1: Context-Free Languages (Grammars)



Languages Accepted by PDAs

Convert any context-free grammar G to a PDA M with: L (G ) ! L( M )

Fall 2004

Costas Busch - RPI

3

Proof - Step 2: Context-Free Languages (Grammars)



Languages Accepted by PDAs

Convert any PDA M to a context-free grammar G with: L (G ) ! L( M )

Fall 2004

Costas Busch - RPI

4

Proof - step 1

Converting Context-Free Grammars to PDAs

Fall 2004

Costas Busch - RPI

5

Context-Free Languages (Grammars)



Languages Accepted by PDAs

Convert any context-free grammar G to a PDA M with: L (G ) ! L( M )

Fall 2004

Costas Busch - RPI

6

We will convert grammar to a PDA M such that:

G

M simulates leftmost derivations of G

Fall 2004

Costas Busch - RPI

7

Convert grammar Production in

G to PDA M
Terminal in

G

G

Apw

a

P, A p w P, P p S

a, a p P

q0
Fall 2004

q
Costas Busch - RPI

P, \$ p \$

q2
8

Grammar leftmost derivation
S

PDA computation
Simulates grammar leftmost derivations

(q0 , W 1 . W kW k 1 . W n ,\$) H( q1 , W 1 . W kW k 1 . W n , S \$) H . H( q1 , W k 1 . W n , X 1 . X m \$) H . H( q2 , P , )

.   W1 . W k X1 . X m  .   W 1 . W kW k 1 . W n
Leftmost variable
Fall 2004

Costas Busch - RPI

9

Grammar

Example
PDA

S p aSTb S pb T p Ta T pP

P , S p aSTb P, S p b a, a p P P , T p Ta b, b p P P, T p P
P, P p S

q0
Fall 2004

q1
Costas Busch - RPI

P, \$ p \$

q2
10

Grammar derivation

PDA computation
( q0 , abab,\$) H( q1 , abab, S \$) H( q1 , bab, STb\$) H( q1 , bab, bTb\$) H( q1 , ab, Tb\$) H( q1 , ab, Tab\$) H( q1 , ab, ab\$) H( q1 , b, b\$) H( q1 , P ,\$) H( q2 , P ,\$)

S

aSTb   ab b   ab ab   abab

Fall 2004

Costas Busch - RPI

11

Derivation: Input

a b

a

b

Time 0

P , S p aSTb P, S p b a, a p P P , T p Ta b, b p P P, T p P
P, P p S

Stack

q0
Fall 2004

q1
Costas Busch - RPI

P, \$ p \$

q2
12

Derivation: S Input

a b

a

b

S
Time 0

P , S p aSTb P, S p b a, a p P P , T p Ta b, b p P P, T p P
P, P p S

Stack

q0
Fall 2004

q1
Costas Busch - RPI

P, \$ p \$

q2
13

Derivation: S   aSTb Input

a b

a

b

a S T b
Stack

Time 1

P , S p aSTb P, S p b a, a p P P , T p Ta b, b p P P, T p P
P, P p S

q0
Fall 2004

q1
Costas Busch - RPI

P, \$ p \$

q2
14

Derivation: S   aSTb Input

a b

a

b

a S T b
Stack

Time 2

P , S p aSTb P, S p b a, a p P P , T p Ta b, b p P P, T p P
P, P p S

q0
Fall 2004

q1
Costas Busch - RPI

P, \$ p \$

q2
15

Derivation: S   aSTb   abTb Input

a b

a

b

b T b
Stack

Time 3

P , S p aSTb P, S p b a, a p P P , T p Ta b, b p P P, T p P
P, P p S

q0
Fall 2004

q1
Costas Busch - RPI

P, \$ p \$

q2
16

Derivation: S   aSTb   abTb Input

a b

a

b

b T b
Stack

Time 4

P , S p aSTb P, S p b a, a p P P , T p Ta b, b p P P, T p P
P, P p S

q0
Fall 2004

q1
Costas Busch - RPI

P, \$ p \$

q2
17

Derivation: S   aSTb   abTb   abTab Input

a b

a

b

T a b
Stack

Time 5

P , S p aSTb P, S p b a, a p P P , T p Ta b, b p P P, T p P
P, P p S

q0
Fall 2004

q1
Costas Busch - RPI

P, \$ p \$

q2
18

Derivation: S   aSTb   abTb   abTab   abab Input

a b

a

b

T a b
Stack

Time 6

P , S p aSTb P, S p b a, a p P P , T p Ta b, b p P P, T p P
P, P p S

q0
Fall 2004

q1
Costas Busch - RPI

P, \$ p \$

q2
19

Derivation: S   aSTb   abTb   abTab   abab Input

a b

a

b

a b
Stack

Time 7

P , S p aSTb P, S p b a, a p P P , T p Ta b, b p P P, T p P
P, P p S

q0
Fall 2004

q1
Costas Busch - RPI

P, \$ p \$

q2
20

Derivation: S   aSTb   abTb   abTab   abab Input

a b

a

b

b
Time 8

P , S p aSTb P, S p b a, a p P P , T p Ta b, b p P P, T p P
P, P p S

Stack

q0
Fall 2004

q1
Costas Busch - RPI

P, \$ p \$

q2
21

Derivation: S   aSTb   abTb   abTab   abab Input

a b

a

b

Time 9

P , S p aSTb P, S p b a, a p P P , T p Ta b, b p P P, T p P
P, P p S

Stack

accept

q0
Fall 2004

q1
Costas Busch - RPI

P, \$ p \$

q2
22

In general, it can be shown that: Grammar generates string w
*

G
If and Only if

PDA M accepts

w

S w
Therefore
Fall 2004

(q0 , w,\$) H(q2 , P ,\$)

L(G ) ! L( M )
Costas Busch - RPI 23

Therefore:

For any context-free language there is a PDA that accepts L

L

Context-Free Languages (Grammars)
Fall 2004


Costas Busch - RPI

Languages Accepted by PDAs
24

Proof - step 2

Converting PDAs to Context-Free Grammars

Fall 2004

Costas Busch - RPI

25

Context-Free Languages (Grammars)



Languages Accepted by PDAs

Convert any PDA M to a context-free grammar G with: L (G ) ! L( M )

Fall 2004

Costas Busch - RPI

26

We will convert PDA

M to
G such that:

a context-free grammar

G

simulates computations of with leftmost derivations

M

Fall 2004

Costas Busch - RPI

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

P

Fall 2004

Costas Busch - RPI

28

1. Modify the PDA so that the stack is never empty during computation Stack

a \$
OK
Fall 2004

\$
OK
Costas Busch - RPI

NOT OK
29

Introduce the new symbol the bottom of the stack

#

to mark

a \$

\$

a \$

\$

#
Fall 2004

#
Costas Busch - RPI

#
30

At the beginning insert

# into the stack

Original PDA
new initial state

P,\$ p \$#

original initial state
Fall 2004 Costas Busch - RPI 31

Convert all transitions so that after popping \$ the automaton halts pop \$

W,\ p s q qi j 
x  +  {# }

pop

\$

qi

P, x p x

W,\ p s

qj

halting state
Fall 2004 Costas Busch - RPI 32

2. Modify the PDA so that at end it empties stack and has a unique accept state Empty stack PDA 
x  +  {# }

P, x p P
P, # p P q f
New accept state
33

Old accept states
Fall 2004 Costas Busch - RPI

3. Modify the PDA so that it has no transitions popping P :

qi

W, P p y

qj

qi

W , X p yX

qj 

X  +  {# }
Fall 2004 Costas Busch - RPI 34

Example of a PDA in correct form:
(modifications are not necessary)

L( M ) ! {w  {a, b} : na ( w) ! nb ( w)}
a, \$ p 0\$ a, 0 p 00 a, 1 p P b, \$ p 1\$ b, 1 p 11 b, 0 p P P, \$ p P
Costas Busch - RPI

*

q0
Fall 2004

qf
35

Grammar Construction
In grammar

G:

Variables: Terminals:

A

PDA stack symbols PDA input symbols

a

Start Variable:

\$ or # Stack bottom symbol
Costas Busch - RPI 36

Fall 2004

PDA transition

q a, A p P q1

Grammar production

Apa

Fall 2004

Costas Busch - RPI

37

PDA transition

q a, A p B1 B2 . Bm q1

Grammar production

A p aB1 B2 . Bm

Fall 2004

Costas Busch - RPI

38

Grammar leftmost derivation
S

PDA computation
H( q1 , W 1 . W kW k 1 . W n ,\$) H . H( q1 , W k 1 . W n , X 1 . X m ) H . H( q2 , P , P )

.   W1 . W k X1 . X m  .   W 1 . W kW k 1 . W n
Leftmost variable
Fall 2004

Costas Busch - RPI

39

Example PDA:

, \$ p 0\$ a, 0 p 00 a, 1 p P

b, p 1 b, 1 p 11 b, 0 p P P, \$ p P
qf

q0
Grammar:

\$ p a0\$ \$ p b1\$ 0 p a 00 1 p b11 \$pP 1p a 0pb
Costas Busch - RPI 40

Fall 2004

Grammar Leftmost derivation:

PDA Computation:

\$   a 0\$   ab\$   abb1\$   abba\$   abba
Fall 2004 Costas Busch - RPI

(q0 , abba,\$)

H(q0 , bba,0\$) H(q0 , ba,\$) H(q0 , a,1\$) H(q0 , P ,\$) H(q f , P , P )
41

Derivation:

\$

a b b

a

Time 0

, \$ p 0\$ a, 0 p 00 a, 1 p P

b, p 1 b, 1 p 11 b, 0 p P P, \$ p P
Costas Busch - RPI

q0
Fall 2004

qf
42

Derivation:

\$

a 0\$

a b b

a

Time 1

a, \$ p 0\$ a, 0 p 00 a, 1 p P

b, p 1 b, 1 p 11 b, 0 p P P, \$ p P
Costas Busch - RPI

0 \$
Stack

q0
Fall 2004

qf
43

Derivation:

\$

a0\$   ab\$

a b b

a

Time 2

a, \$ p 0\$ a, 0 p 00 a, 1 p P

b, p 1 b, 1 p 11 b, 0 p P P, \$ p P
Costas Busch - RPI

\$
Stack

q0
Fall 2004

qf
44

Derivation:

\$

a0\$   ab\$   abb1\$

a b b

a

Time 3

a, \$ p 0\$ a, 0 p 00 a, 1 p P

b, p 1 b, 1 p 11 b, 0 p P P, \$ p P
Costas Busch - RPI

1 \$
Stack

q0
Fall 2004

qf
45

Derivation:

\$

a0\$   ab\$   abb1\$

abba\$

a b b

a

Time 4

a, \$ p 0\$ a, 0 p 00 a, 1 p P

b, p 1 b, 1 p 11 b, 0 p P P, \$ p P
Costas Busch - RPI

\$
Stack

q0
Fall 2004

qf
46

Derivation:

\$

a0\$   ab\$   abb1\$ abba\$   abba
Time 5

a b b

a

a, \$ p 0\$ a, 0 p 00 a, 1 p P

b, p 1 b, 1 p 11 b, 0 p P P, \$ p P
Costas Busch - RPI

empty Stack

q0
Fall 2004

qf
47

However, this grammar conversion does not work for all PDAs:

a, \$ p A\$ a, A p A\$ b, A p P

b, A p P
q1
n n

q0

P, \$ p P

qf

L( M ) ! {a b : n u 1}
Fall 2004 Costas Busch - RPI 48

a, \$ p A\$ a, A p A\$ b, A p P

b, A p P
q1

q0

P, \$ p P

qf

Grammar:

\$ p aA\$ \$pP
Costas Busch - RPI

A p aA\$ Apb
49

Fall 2004

S   aA\$   aaA\$   aab\$   aab  L( M )

Grammar:

\$ p aA\$ \$pP
Costas Busch - RPI

A p aA\$ Apb
50

Fall 2004

The Correct Grammar Construction
In grammar

G :

PDA stack symbol

Variables:

( qi Aq j )
PDA states

Terminals: Input symbols of PDA
Fall 2004 Costas Busch - RPI 51

PDA transition

q a, A p P q1

Grammar production

(qAq1 ) p a

Fall 2004

Costas Busch - RPI

52

PDA transition

q

a, A p B1 B2 . Bm

q1

Grammar production

(qAqm ) p a (q1 B1q2 )(q2 B2 q3 ) . (qm Bm qm 1 )
For all possible states q2 , - , qm 1
Fall 2004 Costas Busch - RPI

in PDA
53

Stack bottom symbol

\$ or #
Start Variable:

(qo Zq f )
accept state

Start state

Fall 2004

Costas Busch - RPI

54

Example:

a, \$ p 0\$ a, 0 p 00 a, 1 p P

b, p 1 b, 1 p 11 b, 0 p P P, \$ p P
qf

q0

Grammar production:
Fall 2004

( q01q0 ) p a
55

Costas Busch - RPI

Example:

a, \$ p 0\$ a, 0 p 00 a, 1 p P

b, p 1 b, 1 p 11 b, 0 p P P, \$ p P
qf

q0

Grammar productions:

( q0 \$q0 ) p b( q01q0 )(q0 \$q0 ) | b( q01q f )(q f \$q0 ) ( q0 \$q f ) p b( q01q0 )(q0 \$q f ) | b( q01q f )(q f \$q f )
Fall 2004 Costas Busch - RPI 56

Example:

a, \$ p 0\$ a, 0 p 00 a, 1 p P

b, p 1 b, 1 p 11 b, 0 p P P, \$ p P
qf

q0

Grammar production: ( q0 \$q f ) p P
Fall 2004 Costas Busch - RPI 57

Resulting Grammar:

( q0 \$q f ) : start variable

( q0 \$q0 ) p b( q01q0 )(q0 \$q0 ) | b( q01q f )(q f \$q0 ) ( q0 \$q f ) p b( q01q0 )(q0 \$q f ) | b( q01q f )(q f \$q f ) ( q01q0 ) p b( q01q0 )(q01q0 ) | b( q01q f )(q f 1q0 ) ( q01q f ) p b( q01q0 )(q01q f ) | b( q01q f )(q f 1q f ) ( q0 \$q0 ) p a ( q0 0q0 )(q0 \$q0 ) a ( q0 0q f )(q f \$q0 ) ( q0 \$q f ) p a ( q0 0q0 )(q0 \$q f ) a ( q0 0q f )(q f \$q f )
Fall 2004 Costas Busch - RPI 58

( q0 0q0 ) p a( q0 0q0 )(q0 0q0 ) a ( q0 0q f )(q f 0q0 ) ( q0 0q f ) p a ( q0 0q0 )(q0 0q f ) a ( q0 0q f )(q f 0q f ) ( q01q0 ) p a ( q0 0q0 ) p b ( q0 \$q f ) p P

Fall 2004

Costas Busch - RPI

59

Grammar Leftmost derivation

PDA computation

( q0 \$ q f )
a (q0 0q0 )(q0 \$q f )

(q0 , abba,\$) H(q0 , bba,0\$) H(q0 , ba,\$) H(q0 , a,1\$) H(q0 , P ,\$) H(q f , P , P )
Costas Busch - RPI 60

ab(q0 \$q f )
abb( q01q0 )(q0 \$q f )

abba (q0 \$q f )
abba
Fall 2004

Derivation:

( q0 \$q f )

a b b

a

Time 0

a, \$ p 0\$ a, 0 p 00 a, 1 p P

b, p 1 b, 1 p 11 b, 0 p P P, \$ p P
Costas Busch - RPI

q0
Fall 2004

qf
61

Derivation: ( q0 \$q f )   a (q0 0q0 )(q0 \$q f )

a b b

a

Time 1

a, \$ p 0\$ a, 0 p 00 a, 1 p P

b, p 1 b, 1 p 11 b, 0 p P P, \$ p P
Costas Busch - RPI

0 \$
Stack

q0
Fall 2004

qf
62

Derivation: ( q0 \$q f )   a (q0 0q0 )(q0 \$q f )   ab(q0 \$q f )

a b b

a

Time 2

a, \$ p 0\$ a, 0 p 00 a, 1 p P

b, p 1 b, 1 p 11 b, 0 p P P, \$ p P
Costas Busch - RPI

\$
Stack

q0
Fall 2004

qf
63

Derivation: ( q0 \$q f )   a (q0 0q0 )(q0 \$q f )   ab(q0 \$q f )

abb(q01q0 )(q0 \$q f )
a b b a
Time 3

a, \$ p 0\$ a, 0 p 00 a, 1 p P

b, p 1 b, 1 p 11 b, 0 p P P, \$ p P
Costas Busch - RPI

1 \$
Stack

q0
Fall 2004

qf
64

Derivation: ( q0 \$q f )   a (q0 0q0 )(q0 \$q f )   ab(q0 \$q f )

abb(q01q0 )(q0 \$q f )   abba(q0 \$q f )
a b b a
Time 4

a, \$ p 0\$ a, 0 p 00 a, 1 p P

b, p 1 b, 1 p 11 b, 0 p P P, \$ p P
Costas Busch - RPI

\$
Stack

q0
Fall 2004

qf
65

Derivation: ( q0 \$q f )   a (q0 0q0 )(q0 \$q f )   ab(q0 \$q f )

abb(q01q0 )(q0 \$q f )   abba(q0 \$q f )   abba
a b b a
Time 5

a, \$ p 0\$ a, 0 p 00 a, 1 p P

b, p 1 b, 1 p 11 b, 0 p P P, \$ p P
Costas Busch - RPI

empty Stack

q0
Fall 2004

qf
66

In general:

Grammar 

( qi Aq j )   wB

If and Only if

PDA 

(qi , w, A) Hq j , P , B ) (

Fall 2004

Costas Busch - RPI

67

Thus:

Grammar generates 

w

( q0 \$ q f )   w

If and Only if

PDA accepts 

w

(q0 , w,\$) Hq f , P , P ) (

Fall 2004

Costas Busch - RPI

68

Therefore: For any PDA there is a context-free grammar that accepts the same language

Context-Free Languages (Grammars)
Fall 2004


Costas Busch - RPI

Languages Accepted by PDAs
69

scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->