You are on page 1of 11

Non-deterministic pushdown automata

Regular languages are recognized by nite automata, context free languages are recognized by non-deterministic pushdown automata.

Models of Computation

Non-deterministic pushdown automata

(We mostly follow the denitions in Kozens Automata and Computability.) A (non-deterministic) pushdown automaton is like an NFA, except it has a stack (pushdown store) for recording a potentially unbounded amount of information, in a last-in-rst-out (LIFO) fashion.
input string

 
stack head (push/pop)

input head (left to right, read-only)

(pushdown) stack


nite control

Models of Computation

The workings of an NPDA

In each step, the NPDA pops the top symbol off the stack; based on (1) this symbol, (2) the input symbol currently reading, and (3) its current state, it can 1. push a sequence of symbols (possibly  ) onto the stack 2. move its read head one cell to the right, and 3. enter a new state according to the transition rule

of the machine.

We allow  -transition: an NPDA can pop and push without reading the next input symbol or moving its read head. Note: an NPDA can only access the top of stack symbol in each step.

Models of Computation

Example: Balanced strings of parentheses


Intuitive description of an NPDA: 1. WHILE <input symbol is [> DO <push [ onto the stack>. 2. WHILE <input symbol is ]> and <top of stack is [> DO <pop>. 3. IF <all of input read> and <top of stack is  > THEN <accept>. (  is initial stack symbol.)

                  

      

      

      

 

        
4

   

Example: input is [ [ ] [ ] ] Think of an NPDA as (representing) an algorithm (for a decision problem) with memory access in the form of a stack.
Models of Computation

Denition of an NPDA

A non-deterministic pushdown automaton (NPDA) is a 7-tuple

#QR(-S(+T(=(=$U&(-,(GV,. where R(-S(+(= 1  is the set of states 1  1


is the input alphabet is the stack alphabet

and

are all nite sets, and

1 ,WJ 1 $U&/6 1 a6E 1 V bc

X#YNZ7[\^].!_0
is the start state

`A#Q

!  .

is the transition function

is the initial stack symbol is the set of accept states.

Note: An NPDA is strictly more powerful than a deterministic PDA. We shall not consider the latter specically here.
Models of Computation 5

Conguration
A conguration of

is an element of

!"

describing (1) the current

state, (2) the portion of the input yet unread (i.e. under and to the right of the input head) and (3) the current stack contents. The start conguration is

#%$'&)(+*,(-/. 0

. I.e.

always starts in the start state with

its input head scanning the leftmost input symbol and the stack containing only The next-conguration relation describes how

moves from one

conguration to another in one step. Formally

If

#%$2(435.678#:9;(=<>(+?/. <

then for any @A6B and CD6EF ,

#:9;(=<@>(G?BCH.I0
(The input symbol pushed, and the new state is $ .)

#%$J(+@>(43>CH.
was popped and 3 was

has been consumed; ? then for any @A6K

If

#%$2(435.678#:9;(
'(G?.

#L9M(+@>(G?BCH.N0

 and CD6E  #%$2(+@O(43PCH.

(no input symbol has been consumed.)


Models of Computation 6

gAhGi

: The language accepted by NPDA

We dene the reexive, transitive closure of

0 k l 0

, written

0

, as follows:

& 0 l krqts\u 0 l
and dene from

mRn mRn k 0 q l

kpo vxwcf 0

q w

wz0 k

l 0 l
iff

0 l

just if

for some {I|c} . I.e. .

follows

in 0 or more steps of the relation

Formally we say that

#%$ & (+de(-/. 0  where $,6EV and 3K6  are called accepting. 3~6E"
, we have The language of by

accepts an input d by nal state if for some

$,6EV and #%$J(


'(43M. . Congurations of the form #%$2(
'(43M.
, is dened to be the set of strings accepted

, written

S#%z.

Models of Computation

There is another accepting convention:

accepts an input d by empty stack if for some

$,6

#%$ & (+de(-/. 0 


N.B.

#Q$J(
'(

.-f

is irrelevant in the denition of acceptance by empty stack.

The two accepting conventions are equivalent.

Models of Computation

Example: An NPDA accepting !zP


High-level description: 1. Push the input symbols onto the stack, one at a time.

\JO

2. Non-deterministically guess that the middle of the string has been reached at some point during 1, and then change into popping off the stack for each symbol read, checking to see if they (i.e. symbols just popped and just read) are the same. 3. If they are always the same symbols, and the stack empties at the same time as the input is nished, accept.

Models of Computation

Example: An NPDA accepting !zP


Implementation-level description:

\JO

#[$ u (=$ & (=$]( [}8 ( [}8(' ) (' \] (-N] (=(=$ & (
([$e].
where

,W

#%$ & = ( }8(-. #%$U&)(')(-. #%$U&)(


'(-. #%$u(=}8(=}J. #%$u(')('. #%$ u (
'(-/. 0 0 0 0 0

[#%$ & = ( }JS.O] [#%$U&)('S.O] [#%$u(-.O] [#%$u(


-.] [#%$u(
-.] [#%$(
-.]

where

}8('

Models of Computation

10

Example: An NPDA accepting !zP


Transition graph:

\JO

X X

push

=+N

pop

Notation: In the transition graph, we represent the transition

#%$%(43M.67#%$2(=<O(-.

by an edge, labelled by <O(-0

, that joins node

to

$ .

Models of Computation

11

Example: A run accepting the input \PPP2

#%$ & ( 0 0 0 0 0 0 0 0 #%$ & ( #%$ & ( #%$ & ( #%$ u ( #%$u( #%$u( #%$u( #%$(

})))}8( )))}8( ))}8( )}8( )}8( }8( }8( U( U(

/. }/. }/. )}/. )}/. }/. }/. /. -.

X X =

push

=+X

pop

Models of Computation

12

Example: An NPDA accepting balanced strings of parentheses

Implementation-level description:

Transition diagram:

#[\$J(=$ ]([ [ ( ] ] ([H,( [ ] (=(=$2(-,([$ ].


where

#%$2( #%$2( #%$2(

(T. 0 0 0

[#%$2( [   .O] [#%$2(


[ [ .O]


[ [

[( [. ]( [.

[ [

[#%$2().O] [#%$ (

.]

#%$2(t8(T.

Models of Computation

13

Equivalence between NPDAs and context-free languages

A major result in automata theory is: Theorem. A language is context-free iff some NPDA accepts it. Proof overview We are breaking down the proof into the following steps: A Given a CFG

, there is an equivalent NPDA

. generating S#_. .

B Given an NPDA

, there is an equivalent CFG

1 Every NPDA can be simulated by an NPDA with one state 2 Every NPDA with one state has an equivalent CFG.

Models of Computation

14

Lemma. Given a CFG

, there is an equivalent NPDA

Proof idea: The stack alphabet of symbols and

consists of the terminal and variable

. We describe the action of

informally:

1. Place the start variable symbol on the stack. 2. Repeat forever: Pop top-of-stack d . Cases of: (a)

d *

is a variable

: Nondeterministically select a rule for

and replace

by the string *

(say) on the rhs of the rule (so that the leftmost symbol of

is at the top of stack).

(b)

d d

is a terminal < : Read the next input symbol and compare it with < . If they

do not match, then exit (and reject this branch of the nondeterminism). (c) Claim:

: Enter the accept state.

accepts S#Y. .

Models of Computation

15

NPDA that accepts CFL generated by

>

Implementation-level description:

Transition diagram:

#[\$ & (=$ u (=$]([<O(


M]([H,(-\(=<O(
^](=(=$ & (-,([$5].
where

is given by

#%$U&(tx(. #%$ u (tx(e. o

[2#%$u(-/.=] [2#%$ u (=<8FUF.-( #%$ u (


UH<F.-( #%$u(

.=] o o o [2#%$ u (

.=] [2#%$ u (

.=] [2#%$ (

.=] = =

2 U

; = = = -

#%$ u (<;(U<O. #%$ u ((). #%$u'(tx(.

Models of Computation

16

Example: A run accepting the input 55

#%$U&t( 0 0 0 0 0 0 0 0 0 0 0 #%$u( #%$ u ( #%$ u ( #%$u( #%$u( #%$ u ( #%$u( #%$ u ( #%$ u ( #%$ u ( #%$ (

<x<x( <x<x( <x<x( <x( <x( <x( <x( ( ( U( U( U( X0

/. /. <8F/. F/. U"<8F/. "<8F/. <8F/. F/. /. /. /. 
. <8FN0 <xUH<"UN0 <x<8FUI0 <x<xX0 <x<x
17

(1)

(1) (2) (3)

(2)

J4 J4 J4 2

; = = =

(3)

(3)

(3)

Leftmost derivation:
Models of Computation

Simulating NPDAs by CFGs

We do this in two steps: 1. Every NPDA can be simulated by an NPDA with one state 2. Every NPDA with one state has an equivalent CFG. For 2: Take a one-state NPDA empty stack. Dene

o o

#[$T](-S(+(=(=$2(-,(
t. #T(-S(G(-/. ^u''/
where

that accepts by

where

contains a rule

?
for every transition have

S#%z.

#%$2(=,u''/.678#%$2(=(G?. S#Y . .

6KIZ[\^]

. Then we

Models of Computation

18

Every NPDA can be simulated by a one-state NPDA

Idea: maintain all state information on the stack. W.l.o.g. we assume form

is of the

#QR(-S(+T(=(-)(-,([M>]. , and  can empty its stack after entering nal state . o Set e aAN! (elements are written L9?_$ ). We construct a new NPDA o  #["]J(-S(+ (= (J(Q+-(
t.

that accepts by empty stack. For each transition

#%$U&)(=u''/.67#L9M(=(G?.
where

6KIZ7[H]

, include in

the transition

#J($U&5uO$u;$u> $ ''$eu>/$+.67 #J(=(9B?~$+.


for all possible choices of
Models of Computation

$ u ('''(=$

.
19

Intuitively

simulates

, guessing non-deterministically what state

will be

in at certain future points in the computation, saving those guesses on the stack, and then verifying later that the guesses were correct. Lemma.

can scan a string d starting with only

9B?~$

on its stack and end

up with an empty stack, if and only if

can scan d starting in stack 9 with only

on its stack and end up in state

with an empty stack. I.e. we have

q #L9M(+de(+?/. 0
It then follows that

#%$J(
'(

. S#%z. o

mRn S#%.
.

q #J(+d5(9?_$+. 0 R #J(
U(
-.

Models of Computation

20

Closure Properties of Context-Free Languages


Theorem: Context-free languages are closed under the regular operations: union, concatenation and star..

o # u(-S(u(-u-. and # (-( (- . be context o free grammars with Tu\ . Consider the following context free grammars o UU
#Tu\ZA Z7[H](-S(u\Z Z7[HN0 u(-N0 ](-. o
#Tu\ZA Z7[H](-S(u\Z Z7[HN0 u ]J(-F. o Q+ #Tu\Z7[H]J(
(7u\Z[HN0 u\(-N0 ](-.
Proof idea. Let

,u

where

is a fresh variable. Then

S#YUU
. S#Y
. S#YQ++.
Models of Computation

o o o

S#Y,u.;ZS#Y . S#Y u .'S#Y . S#Y,u.G


21

You might also like