Pushdown Automata (PDA) : Reading: Chapter 6

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 37

Pushdown Automata (PDA) ( )

Reading: Chapter 6

PDA - the automata for CFLs

What is?

FA to Reg Lang, Lang PDA is to CFL

PDA == [ -NFA + a stack ] Wh a stack? Why t k?


Input string -NFA Accept/reject

A stack filled with stack symbols


2

Pushdown Automata Definition

A PDA P := ( Q,,, ,q0,Z0,F ):


Q: : : : q0: Z0: F:

states of the -NFA input alphabet stack symbols transition function start state Initial stack top s symbol mbol Final/accepting states

: Q x x => Q x

old state

Stack top

input symb.

new state(s) new Stack top(s)

: 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 is unchanged iii. If Y=Z1Z2Zk: X is popped and is replaced by Y in reverse order ( (i.e., , Z1 will be the new stack top) q

a
Y=?

X p Action Pop(X) Pop(X) Push(X)

i) ii) iii)

Y= Y=X Y=Z Y Z1Z2..Z Zk

Pop(X) Push(Zk) Push(Zk-1) Push(Z2) Push(Z1)


4

Example
Let Lwwr = {wwR | w is in (0+1)*} CFG for Lwwr : S==> 0S0 | 1S1 | PDA for Lwwr : P := ( Q,, , ,q0,Z0,F ) = ( {q0, q1, q2},{0,1},{0,1,Z0},,q0,Z0,{q2})

Initial state of the PDA: Stack St k top Z0


First symbol push on stack

PDA for Lwwr


1. 2.

q0

(q0,0, Z0)={(q0,0Z0)} (q0,1, Z0)={(q0,1Z0)} (q0,0, 0 0)={(q 0) {(q0,00)} 00)} (q0,0, 1)={(q0,01)} (q0,1, 0)={(q0,10)} (q0,1, 1)={(q0,11)} (q0, , 0)={(q1, 0)} (q0, , 1)={(q1, 1)} (q0, , Z0) )={(q {(q1, Z0)} (q1,0, 0)={(q1, )} (q1,1, 1)={(q1, )} (q1, , Z0)={(q2, Z0)}

3. 4. 5. 6.

Grow the stack by pushing new symbols on top of old ( (w-part) t) Switch to popping mode (boundary between w and wR) Shrink the stack by popping matching symbols y (w ( R-part) p ) Enter acceptance state
6

7. 8. 9 9.

10. 11.

12.

PDA as a state diagram


(qi,a, X)={(qj,Y)}

Current state

Next input symbol

Current Stack stack Top top Replacement (w/ string Y)


a, X / Y

qi

qj

Next state

PDA for Lwwr: Transition Diagram


Grow stack
0, Z0/0Z0 1, Z0/1Z0 0, 0/00 0, 1/01 1, 0/10 1, 1/11

Pop stack for matching symbols


0, 0/ 1, 1/

= {0, 1} = {Z0, 0, 1} Q = {q0,q q1,q q2}

, Z0/Z0

q0

, Z0/Z0 , 0/0 , 1/1

q1

, Z0/Z0

q2

G to acceptance Go

Switch to popping mode This would be a non-deterministic PDA

Example p 2: language g g of balanced paranthesis


Grow stack (, Z0 / ( Z0 (, ( / ( ( , Z0 / Z0
q0

Pop stack for matching symbols

= { (, ) } = {Z0, ( } Q = {q0,q q1,q q2}

), ( / ), ( / , Z0 / Z0
q1

, Z0 / Z0

q2

Switch to popping mode

(, ( / ( ( (, Z0 / ( Z0

Go to acceptance (by G (b fi final l state) ) when you see the stack bottom symbo

To allow adjacent blocks of nested paranthesis

Example 2: language of balanced paranthesis (another design)


(,Z0 / ( Z0 (,( / ( ( ), ( / start ,Z0/ Z0 ,Z0/ Z0

= { (, ) } = {Z0, ( } Q = {q0,q q1}

q0

q1

10

PDAs Instantaneous Description (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 bottom of f stack t k

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 represents one move |---* sign represents a sequence of moves
11

How does the PDA for Lwwr work on input 1111?


All moves made by the non-deterministic PDA
(q0,1111,Z0) (q0,111,1Z0) (q0,11,11Z 11 11Z0) (q0,1,111Z0) (q0,,1111Z0) ( 1, ,1111Z (q 1111Z0) Path dies (q1,1111,Z0) (q1,111,1Z 111 1Z0) (q1,11,11Z0) (q1,1,111Z0) (q1, ,11Z 11Z0) Path dies (q1,1,1Z0) (q1, ,Z Z0) (q2, ,Z0) Acceptance by final state: = empty t input i t AND final state
12

Path dies Path dies dies

Principles about IDs

Theorem 1: If for a PDA, (q, x, A) |---* (p, y, B), then for any string w * and d *, * it i is also l t true th that: t

(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 true that:

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

13

There are two types of PDAs that one can design: those that accept by final state or by empty stack

Acceptance by

PDAs that accept by final state:

For a PDA P, the language accepted by P, d denoted t db by L(P) by b final fi l state t t , is: i Checklist:

{w | (q0,w,Z0) |---* (q,, A) }, s.t., q F

- input exhausted? - in a final state?

PDAs that accept by empty stack:

For a PDA P, the language accepted by P, denoted by N(P) by empty stack, is:

{w | (q0, { ,w,Z , 0) | |---* (q, , ) }, for any yqQ Q.


- is the stack empty? 14

Checklist: Q) Does a PDA that accepts by empty stack need any final state specified in the design? - input exhausted?

Example: p L of balanced parenthesis


PDA that accepts by final state PF:
(,Z0 / ( Z0 (,( / ( ( ), ( / ,Z0/ Z0

An equivalent PDA that accepts by empty stack PN:


(,Z ( Z0 / ( Z0 (, ( / ( ( ), ( / ,Z0 / start

start ,Z Z0/ Z0

q0

q1

,Z Z0/ Z0

q0

How will these two PDAs work on the input: ( ( ( ) ) ( ) ) ( )


15

PDA for Lwwr: Proof of correctness

Theorem: The PDA for Lwwr accepts a string x by final state if and only if x is of the form wwR. Proof:

(if-part) (if part) If the string is of the form wwR then there exists a sequence of IDs that leads to a final state: (q0,wwR,Z0) |---* (q0,wR,wZ0) |---* (q1,wR,wZ0) |---* (q1, ,Z Z0) |--| * (q2, ,Z Z0) (only-if part)

Proof by induction on |x|


16

PDAs accepting by final state and empty stack are equivalent

PF <= PDA accepting by final state

PF = (QF,, , F,q0,Z0,F) PN = (QN,, , N,q0,Z0)

PN <= PDA accepting by empty stack

Th Theorem:

(PN==> PF) For every PN, there exists a PF s.t. L(PF)=L(PN) (PF==> PN) For every PF, there exists a PN s.t. L(PF)=L(PN)

17

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

PN==> PF construction

Whenever PNs stack becomes empty, make PF go to a final state without consuming any addition symbol To detect empty stack in PN: PF pushes a new stack symbol X0 (not in of PN) initially before simultating PN
PN: q0

PF:
New start , X0 / X0

, X0/Z0X0
p0

, X0/ X0 , X0/

, X0/ X0 , X0/ X0
X0 18

pf

N U {X }, , p , X , {p }) PF = (QN U {p0,pf}, , P 0 F 0 0 f

Example: Matching parenthesis ( )


PN: N : ( {q0}, {(,)}, {Z0,Z1}, N, q0, Z0 ) N(q0,(,Z0) = { (q0,Z1Z0) } N(q0,(,Z ( Z1) = { (q0, Z1Z1) } N(q0,),Z1) = { (q0, ) } N(q0, ,Z0) = { (q0, ) } Pf: f: ( {p0,q0 ,pf}, {(,)}, {X0,Z0,Z1}, f, p0, X0 , pf) f(p0, ,X0) = { (q0,Z0) } f(q0,(,Z ( Z0) = { (q0,Z Z1 Z0) } f(q0,(,Z1) = { (q0, Z1Z1) } f(q0,),Z1) = { (q0, ) } f(q0, ,Z0) = { (q0, ) } f(p0, ,X0) = { (pf, X0 ) }
(,Z0/Z1Z0 (,Z1/Z1Z1 ),Z1/ ,Z Z0/ start q0

(,Z0 /Z1Z0 (,Z1 /Z1Z1 ),Z1 / ,Z0 / start

p0

,X /Z X
0 0

q0

,X / X
0

pf

Accept by empty stack

Accept by final state

19

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

PF==> PN construction

Main idea:

Whenever PF reaches a final state, just make an -transition into a new end state, clear out the stack and accept Danger: What if PF design is such that it clears the stack midway without entering a final state? to address this, , add a new start symbol y X0 ( (not in of PF)

PN = (Q U {p0,pe}, , U {X0}, N, p0, X0)

PN:
New start

, X0/Z0X0
p0 q0

, any/ , any/ , any/

, any/
pe

PF 20

Equivalence of PDAs and CFGs

21

CFGs == PDAs ==> CFLs


PDA by final state PDA by empty stack

?
CFG

22

This is same as: implementing a CFG using a PDA

Converting CFG to PDA


Main idea: The PDA simulates the leftmost derivation on a given
w, and upon consuming it fully it either arrives at acceptance (by empty stack) or non non-acceptance. acceptance PDA ( (acceptance by empty stack) implements CFG
23

reject

OUTP PUT

INPU UT

accept

This is same as: implementing a CFG using a PDA

Converting a CFG into a PDA


Main idea: The PDA simulates the leftmost derivation on a given w, and upon consuming it fully it either arrives at acceptance (by empty p y stack) ) or non-acceptance. p

Steps:
1.

2.

3.

Push the right hand side of the production onto the stack, with leftmost symbol at the stack top If stack top is the leftmost variable, then replace it by all its productions (each possible substitution will represent a distinct path taken by the non non-deterministic deterministic PDA) If stack top has a terminal symbol, and if it matches with the next symbol in the input string, then pop it

S State i i is inconsequential i l( (only l one state i is needed) d d)


24

Formal construction of PDA Note: Initial stack symbol (S) from CFG same as the start variable

Before: A

Given: G= (V,T,P,S) Output: PN = ({q} ({q}, T T, V U T T, , q, q S) :

i th in the grammar

For all F ll A V , add dd th the f following ll i transition(s) in the PDA:

After:

Before: a

(q, (q ,A) A) = { (q, (q ) | A A ==> P}

For all a T, add the following transition(s) in the PDA:

After:

a pop

(q,a,a)= { (q, ) }

25

Example: CFG to PDA


G = ( {S,A}, {0,1}, P, S) P:

S ==> AS | A ==> 0A1 | A1 | 01

1,1 / 0,0 / ,A / 01 ,A / A1 ,A / 0A1 ,S S/ ,S / AS ,S / S q

PDA = ({q}, ({ } {0,1}, {0 1} {0 {0,1,A,S}, 1 A S} , q, S) :


(q, (q , S) = { (q (q, AS) AS), (q (q, )} (q, , A) = { (q,0A1), (q,A1), (q,01) } (q, 0, 0) = { (q, ) } (q, 1, 1) = { (q, ) } How will this new PDA work?

Lets simulate string 0011


26

Simulating g string g 0011 on the new PDA Leftmost deriv.:


PDA (): (q, , S) = { (q, AS), (q, )} (q, , A) = { (q,0A1), (q,A1), (q,01) } (q, 0, 0) = { (q, ) } (q, 1, 1) = { (q, ) }
,S / S 1,1 1 1/ 0,0 / ,A / 01 ,A / A1 ,A / 0A1 ,S / ,S / AS

S => AS => 0A1S => 0011S => 0011

Stack moves (shows only the successful path):

A S

0 A 1 S

A 1 S

0 1 1 S

1 1 S

1 S

0 S =>AS =>0A1S =>0011S

=> 0011

Accept by empty stack

27

Proof of correctness for CFG ==> PDA construction

Claim: A string is accepted by G iff it is accepted (by empty stack) by the PDA Proof:
(only-if part)

Prove by induction on the number of derivation steps

(if part)

If (q, ( wx, S) |-| * (q,x,B) ( B) then th S =>*lm wB B

28

Converting a PDA into a CFG


Main idea: Reverse engineer the productions from transitions If (q,a,Z) => (p, Y1Y2Y3Yk):

1. 2. 3.

State is changed from q to p; Terminal a is consumed; Stack top symbol Z is popped and replaced with a sequence of k variables.

Action: Create a g grammar variable called [qZp] which includes the following production:

[qZp] => a[pY1q1] [q1Y2q2] [q2Y3q3] ] [qk-1Ykqk]


29

Proof discussion (in the book)

Example: Bracket matching

To avoid confusion, we will use b=( and e=)


0. 1 1. 2. 3. 4. S => [q0Z0q0] [q0Z0q0] => b [q0Z1q0] [q0Z0q0] [q0Z1q0] => b [q0Z1q0] [q0Z1q0] [q0Z1q0] => e [q0Z0q0] =>

PN: 1. 2. 3. 4.

( {q0}, {b,e}, {Z0,Z1}, , q0, Z0 ) (q0,b,Z0) = { (q0,Z1Z0) } (q0,b,Z1) = { (q0,Z1Z1) } (q0, , ,Z0) = { (q0, ) } (q0,e,Z1) = { (q0, ) }

Let A=[q0Z0q0] Let B=[q0Z1q0] If you were to directly write a CFG: S => b S e S | 0. 1. 2. 3. 4. S => A A => b B A B => b B B B => e A =>

Simplifying, 0. 1. S => b B S | B => b B B | e

30

Two ways to build a CFG


Build a PDA Construct CFG from PDA (indirect)

Derive CFG directly Similarly Two ways to build a PDA Construct Derive a CFG PDA f from CFG Design a PDA directly

(direct)

(indirect)

(direct)
31

Deterministic PDAs

32

This PDA for Lwwr is non-deterministic


Grow stack
0, Z0/0Z0 1, Z0/1Z0 0, 0/00 0, 1/01 1, 0/10 1, 1/11

Pop stack for matching symbols 0, 0/ 1, , 1/


, Z0/Z0 , 0/0 , 1/1

Why does it have to be nondeterministic?

q0

q1

, Z0/Z0

q2

A Accepts b by fi final l state To remove guessing, impose the user to insert c in the middle

Switch to popping mode

33

Example shows that: Nondeterministic PDAs D-PDAs

D PDA for Lwcwr = {wcwR | c is some D-PDA special symbol not in w}


Note: Grow stack
0, Z0/0Z0 1, Z0/1Z0 0, 0/00 0, 1/01 1, 0/10 1, 1/11

Pop stack for matching symbols


1, 1/ 0, 0/

all transitions have become deterministic

q0

c, Z0/Z0 c, 0/0 c, 1/1

q1

, Z0/Z0 Accepts b A by final state

q2

Switch to popping mode

34

Deterministic PDA: Definition

A PDA is deterministic if and only if:


1 1.

(q,a,X) (q a X) has at most one member for any a U {}

If (q,a,X) is non-empty for some a, then (q (q, ,X) X) must be empty empty.

35

PDA vs DPDA vs Regular g languages


Lwcwr Lwwr

Regular languages

D-PDA

non-deterministic PDA

36

Summary

PDAs for CFLs and CFGs


Non-deterministic Deterministic

1. 2.

PDA acceptance types


By final state By empty stack

PDA
IDs, Transition diagram

Equivalence of CFG and PDA


CFG => PDA construction PDA => CFG construction
37

You might also like