Docsity

Pushdown Automata for Context-Free Languages, Lecture notes of Theory of Automata

Partial preview of the text

Download Pushdown Automata for Context-Free Languages and more Lecture notes Theory of Automata in PDF only on Docsity!

Pushdown Automata (PDA)

Reading: Chapter 10

PDA - the automata for CFLs^ ^

What is?^ ^

FA to Reg Lang,

PDA is to CFL

^ PDA == [

^ -NFA + “a stack” ]

^ Why a stack?

-NFA

A stack filled with “stack symbols”

Inputstring

Accept/reject

δ : The Transition Function δ(q,a,X) = {(p,Y), …}^ 1.

state transition from q to p

2.^

a is the next input symbol

3.^

X is the current stack

top^

symbol

4.^

Y is the replacement for X;it is in

* (a string of stack symbols)i.

Set Y =

^ for:

Pop(X)

ii.^

If Y=X: stack top isunchanged iii.^

If Y=Z

Z…Z 1 2

: X is poppedk

and is replaced by Y inreverse order (i.e., Z

will be 1

the new stack top)

4

old state

Stack top input symb.

new state(s) new Stack top(s)

Q x ∑ x

=> Q x

q^

a^

X

p

Y
Y =?

Action

i)^

Y=^

Pop(X)

ii)^

Y=X

Pop(X)Push(X)

iii)^

Y=Z
Z..Z 1 2

k^

Pop(X)Push(Z

)k Push(Z

)k- …Push(Z

Push(Z

ExampleLet L

wwr

= {ww

R^ | w is in (0+1)*}

^ CFG for L

wwr

:^

S==> 0S0 | 1S1 |

^ PDA for L

wwr

^ P := ( Q,∑,

, δ,q

,Z 0

,F ) 0

= ( {q

, q 0

, q 1

},{0,1},{0,1,Z 2

},δ,q 0

,Z 0

,{q 0

PDA as a state diagram

q^ i^

q^ j

a, X^

/ Y

Nextinputsymbol

Currentstate

Currentstacktop

StackTopReplacement(w/ string Y)

Nextstate

δ(q

,a, X)={(qi

,Y)}j

PDA for L

wwr

: Transition Diagram

q^0

q^1

q^2

0, Z^0

/0Z^0 1, Z^0

/1Z^0 0, 0/000, 1/011, 0/101, 1/

0, 0/^

1, 1/^

, Z^0

/Z^0 , 0/0 , 1/

, Z^0

/Z^0

Grow stack

Switch topopping mode

Pop stack formatching symbols

Go to acceptance

∑ = {0, 1} = {Z

Q = {q

,q 0 1

,q}^2

, Z^0
/Z 0

This would be a non-deterministic PDA

Example 2: language of balancedparanthesis (another design)

∑ = { (, ) } = {Z

Q = {q

,q 0 1

(,Z/ ( Z^0 q^0

0 (,( / ( (), ( /

start

q^1 ,Z/ Z^0

0

,Z/ Z^0

0

PDA’s InstantaneousDescription (ID)A PDA has a configuration at any given instance:

(q,w,y)

^ q - current state ^ w - remainder of the input (i.e., unconsumed part) ^ y - current stack contents as a string from top to bottomof stack

If δ(q,a, X)={(p, A)} is a transition, then the following are also true:

^ (q, a, X ) |--- (p,

,A)

^ (q, aw, XB ) |--- (p,w,AB)

|--- sign is called a “turnstile notation” and representsone move|---* sign represents a sequence of moves

Principles about IDs^ ^

Theorem 1: If for a PDA,(q, x, A) |---* (p, y, B), then for any stringw^

^ ∑* and

*, it is also true that:

^ (q, x w, A

) |---* (p, y w, B

^ Theorem 2: If for a PDA,(q, x w, A) |---* (p, y w, B), then it is also truethat:

^ (q, x, A) |---* (p, y, B)

Acceptance by…^ ^

PDAs that accept by

final state

^ For a PDA P, the language accepted by P,denoted by L(P) by

final state

, is:

^ {w | (q

,w,Z 0

) |---* (q, 0

, A) }, s.t., q

^ F

^ PDAs that accept by

empty stack

^ For a PDA P, the language accepted by P,denoted by N(P) by

empty stack

, is:

^ {w | (q

,w,Z 0

) |---* (q, 0

,^ 

) }, for any q

^ Q.

Checklist:- input exhausted?- in a final state? Checklist:- input exhausted?- is the stack empty?

There are two types of PDAs that one can design:

those that accept by final state or by empty stack

Q) Does a PDA that accepts by empty stack

need any final state specified in the design?

PDA for L

wwr

: Proof of

correctness^ ^

Theorem: The PDA for L

wwr

accepts a string x

by final state if and only if x is of the formww

R^.

^ Proof:

^ (if-part)

If the string is of the form ww

R^ then there

exists a sequence of IDs that leads to a final state:(q^0

,ww

R^ ,Z

) |---* (q 0

,w 0

R^ ,wZ

) |---* (q 0

,w 1

R^ ,wZ

(q^1

,^ ,Z

q^ ,^2

,Z

^ (only-if part)

^ Proof by induction on |x|

PDAs accepting by final state and emptystack are equivalent^ ^

PF

<= PDA accepting by final state (^)  PF^ = (Q

,∑,F

, δ

,qF ,Z 0

,F) 0

^ P

<= PDA accepting by empty stackN (^)  P = (QN

,∑,N

, δ

,qN ,Z 0

) 0

^ Theorem:

^ (P

==> PN

)^ For every P F

, there exists a PN

s.t. L(PF^

)=L(PF

)N

^ (P

==> PF

)^ For every P N

, there exists a PF

s.t. L(PN^

)=L(PF

)N

19

Example: Matching parenthesis “(” “)” P:^ N

( {q^ }, {(,)}, {Z^0

,Z}, δ 01

, q^ N^0 , Z)^0

δ:^ N

δ(q^ N^0

,(,Z^ ) = { (q^0

,ZZ 01

) } 0

δ(q^ N^0

,(,Z^ ) = { (q^1

, ZZ 01

) } 1

δ(q^ N^0

,),Z^ ) = { (q^1

,^ ) } 0

δ(q^ N^0

,^ ,Z

) = { (q 0

,^ ) } 0

q^0 start

(,Z/Z^0

Z 1 0 (,Z/Z^1

Z 1 1 ),Z/^1  ,Z/^0

(,Z/Z^ Z^0 1 q^0

0 (,Z/Z^1

Z 1 1 ),Z/^ ^1 ^ ,Z/^0

start

p^0

p^ f

 ,X^ /Z^0

X^0

 ,X^ / X^0

0

P:^ f^

( {p^ ,q^0

,p^ }, {(,)}, {X 0 f^

,Z,Z 00

}, δ 1 f , p^ , X 0

p^ )0 , f^

δ:^ f^

δ(p^ f^0 ,^ ,X^0

) = { (q

,Z) } 00

δ(q^ f^0 ,(,Z) = { (q^0

,ZZ 01

) } 0

δ(q^ f^0 ,(,Z) = { (q^1

, ZZ 01

) } 1

δ(q^ f^0 ,),Z) = { (q^1

,^ ) } 0

δ(q^ f^0 ,^ ,Z^0

) = { (q

,^ ) } 0

δ(p^ f^0 ,^ ,X^0

) = { (p

, X) }f^0

Accept by empty stack

Accept by final state

20

P^ F

==> P

N^

construction

^ Main idea:

^ Whenever P

reaches a final state, just make anF^

^ -transition into a

new end state, clear out the stack and accept  Danger: What if P

design is such that it clears the stack midwayF

without

entering a final state?

^ to address this, add a new start symbol X

(not in 0

^ of P

)F

P= (Q U {pN^

,p^0 e

^ U {X

, pN^

, X 0

, X p 0

/ZX 00 0

Newstart

, any/

 , any/

, any/

q^0

…^

p^ e

, any/

PF

P^ :N

How to convert an final state PDA into an empty stack PDA?