Pushdown automata

Preview:

DESCRIPTION

 

Citation preview

Pushdown Pushdown AutomataAutomataPDAsPDAs

Pushdown Automaton -- Pushdown Automaton -- PDAPDA Input String

Stack

States

Initial Stack SymbolInitial Stack Symbol

Stack

$

Stack

z

bottomspecial symbol

The StatesThe States

q1 q2a, b c

Inputsymbol

Popsymbol

Pushsymbol

q1 q2a, b c

a

b top

input

stack

a

Replaceeh

$eh

$

c

q1 q2a, c

a a

Pushb

eh

$eh

$

bc

top

input

stack

q1 q2a, b

a a

Popb

eh

$eh

$

top

input

stack

q1 q2a,

a a

No Changeb

eh

$eh

$

btop

input

stack

Non-DeterminismNon-Determinism

q1

q2a, b c

q3a, b c

q1 q2, b c

transition

These are allowed transitions in a Non-deterministic PDA (NPDA)

NPDA: Non-NPDA: Non-Deterministic PDADeterministic PDA

Example:

,

a, a

b, a q0 q1 q2 q3

b, a

, $ $

a, a

b, a 0q q1 q2 q3

Execution Example:

Input

a a a b b b

currentstate

b, a

Time 0

, , $ $

Stack

$

a, a

b, a q0 q1 q2 q3

Input

a a a b b b

b, a

Time 1

, , $ $

Stack

$

a, a

b, a q0 q1 q2 q3

Input

Stack

a a a b b b

$

a

b, a

Time 2

, , $ $

a, a

b, a q0 q1 q2 q3

Input

Stack

a a a b b b$

aa

b, a

Time 3

, , $ $

a, a

b, a q0 q1 q2 q3

Input

Stack

a a a b b b

$

aaa

b, a

Time 4

, , $ $

a, a

b, a q0 q1 q2 q3

Input

a a a b b b

Stack

$

aaa

b, a

Time 5

, , $ $

a, a

b, a q0 q1 q2 q3

Input

a a a b b b$

a

Stack

b, a

Time 6

, , $ $

a

a, a

b, a q0 q1 q2 q3

Input

a a a b b b$

Stack

b, a

Time 7

, , $ $

a

a, a

b, a q0 q1 q2 q3

Input

a a a b b b

b, a

Time 8

accept, , $ $

$

Stack

A string is accepted if there is a computation such that:

• All the input is consumed

• The last state is a final state

At the end of the computation,we do not care about the stack contents

The Language of PDA

Formal DefinitionFormal DefinitionNon-Deterministic Pushdown Automaton NPDA

),,,δ,Γ,Σ,( 0 FzqQM

States

Inputalphabet

Stackalphabet

Transitionfunction

Finalstates

Stackstartsymbol

Initialstate

Instantaneous Instantaneous DescriptionDescription

),,( suq

Currentstate Remaining

input

Currentstackcontents

Acceptance by Final Acceptance by Final StateState

L(M)= set of all strings w such that starting L(M)= set of all strings w such that starting from initial ID machine consumes w from from initial ID machine consumes w from input and enters an accepting state.input and enters an accepting state.

The contents of the stack at that time is The contents of the stack at that time is irrelevant.irrelevant.

{w | (q0, w, Z){w | (q0, w, Z)** (q, (q, , , ))For some final state q of F and any stack For some final state q of F and any stack

staring staring ..

Acceptance by empty Acceptance by empty stack stack

N(M) = (w | (q0,w,Z) N(M) = (w | (q0,w,Z) * * (q,(q, ,, ))

That is N(M) is the set of all strings w such That is N(M) is the set of all strings w such that M can consume w and at the same that M can consume w and at the same time empties its stack.Here we do not time empties its stack.Here we do not care whether q is a final state or not.care whether q is a final state or not.

The input stringis accepted by the NPDA:

aaabbb

a, a

b, a q0 q1 q2 q3

b, a

, , $ $

}0:{ nbaL nn

is the language accepted by the NPDA:

a, a

b, a q0 q1 q2 q3

b, a

In general,

, , $ $

Another NPDA exampleAnother NPDA example

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

NPDA M

}{)( RwwML

Execution Example:

Input

Time 0

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

a ab b

Input

a ab

Time 1

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

ab

Input

Time 2

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

aa ab b

b

Input

Time 3

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

aa ab b

b

Guess the middle of string

Input

Time 4

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

aa ab b

b

Input

Time 5

Stack

$

, $ $1q q2

bb

aa

,

,

, q0

bb

aa

,

,

a ab b a

Input

Time 6

Stack

$

, $ $q1

bb

aa

,

,

, q0

bb

aa

,

,

a ab b

accept

q2

Rejection Example:

Input

Time 0

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

a b b b

Input

Time 1

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

aa b b b

Input

Time 2

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

ab

a b b b

Input

Time 3

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

ab

Guess the middle of string

a b b b

Input

Time 4

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

ab

a b b b

Input

Time 5

Stack

$

, $ $1q q2

bb

aa

,

,

, q0

bb

aa

,

,

aa b b b

There is no possible transition.

Input is not consumed

Another computation on same string:

Input Time 0

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

a b b b

Input

Time 1

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

aa b b b

Input

Time 2

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

ab

a b b b

Input

Time 3

Stack

$ab

a b b b

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

b

Input

Time 4

Stack

a b b b

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

$abbb

Input

Time 5

Stack

a b b b

$abbb

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

No final state is reached

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

There is no computation that accepts string abbb

)(MLabbb

Pushing StringsPushing Strings

q1 q2a, b w

Inputsymbol

Popsymbol

Pushstring

q1 q2a, b cdf

a

b top

input

stack

a

Push

eh h

e

cdf

pushedstring

Example:

$ $

Another NPDA exampleAnother NPDA example

$$, q1 q2

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

NPDA M

}:{)( ba nnwML

Time 0

Input

a ab b b

currentstate

a

$

Stack

$$, q1 q2

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

Execution Example:

Time 1

Input

a ab b ba

$

Stack

0

$$, q1 q2

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

Time 3

Input

a bb b a

$

Stack

a

$

$$, q1 q2

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

0

Time 4

Input

a bb b a

$

Stack

a

1

$$, q1 q2

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

Time 5

Input

a bb b a

$

Stack

a

11

$$, q1 q2

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

Time 6

Input

a bb b a

$

Stack

a

1

$$, q1 q2

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

1

Time 7

Input

a bb b a

$

Stack

a

1

$$, q1 q2

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

Time 8

Input

a bb b a a

$

Stack

$$, q1 q2

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

accept

Formalities for Formalities for NPDAsNPDAs

q1 q2a, b w

)},{(),,( 21 wqbaq

Transition function:

q1

q2wba ,

q3wba ,

)},(),,{(),,( 321 wqwqbaq

Transition function:

Formal DefinitionFormal DefinitionNon-Deterministic Pushdown Automaton NPDA

),,,δ,Γ,Σ,( 0 FzqQM

States

Inputalphabet

Stackalphabet

Transitionfunction

Finalstates

Stackstartsymbol

Initialstate

Instantaneous Instantaneous DescriptionDescription

),,( suq

Currentstate Remaining

input

Currentstackcontents

a, a

b, a q0 q1 q2 q3

Input

Stack

a a a b b b$

aa

b, a

Time 4:

, , $ $

Example: Instantaneous Description

$),,( 1 aaabbbq

a

a, a

b, a q0 q1 q2 q3

Input

Stack

a a a b b b$

aa

b, a

Time 5:

, , $ $

Example: Instantaneous Description

$),,( 2 aabbq

a

We write:

$),,($),,( 21 aabbqaaabbbq

Time 4 Time 5

a, a

b, a q0 q1 q2 q3

b, a

, , $ $

,$),(,$),($),,($),,(

$),,($),,($),,(

,$),(,$),(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

A computation:

,$),(,$),($),,($),,(

$),,($),,($),,(

,$),(,$),(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

For convenience we write:

,$),(,$),( 30 qaaabbbq

Formal DefinitionFormal DefinitionLanguage of NPDA :M

)}',,(),,(:{)( 0 sqswqwML f

Initial state Final state

Example:,$),(,$),( 30 qaaabbbq

a, a

b, a q0 q1 q2 q3

b, a

, , $ $

NPDA :M

)(MLaaabbb

,$),(,$),( 30 qbaq nn

a, a

b, a q0 q1 q2 q3

b, a

, , $ $

NPDA :M

)(MLba nn

a, a

b, a q0 q1 q2 q3

b, a

, , $ $

NPDA :M

}0:{)( nbaML nnTherefore:

NPDAs Accept NPDAs Accept Context-Free Context-Free LanguagesLanguages

Context-Free

Languages(Grammars)

LanguagesAccepted

byNPDAs

Theorem:

Context-Free

Languages(Grammars)

LanguagesAccepted

byNPDAs

Proof - Step 1:

Convert any context-free grammar to a NPDA with:

GM )()( MLGL

Context-Free

Languages(Grammars)

LanguagesAccepted

byNPDAs

Proof - Step 2:

Convert any NPDA to a context-free grammar with: G

M)()( MLGL

ConvertingConverting Context-Free Context-Free GrammarsGrammarsto to NPDAsNPDAs

An example grammar:

T

TaT

bS

aSTbS

What is the equivalent NPDA?

q0 q1 2qS, , $ $

Grammar:

NPDA:

T

TaT

bS

aSTbS

,

,

,

,

T

TaT

bS

aSTbS

bb

aa

,

,

The NPDA simulates leftmost derivations of the grammar

L(Grammar) = L(NPDA)

Grammar:

T

TaT

bS

aSTbS

A leftmost derivation:

abababTababTbaSTbS

NPDA execution:

0q q1 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab

Time 0

b

currentstate

q0 q1 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab

Time 1

b

S

q0 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab

Time 2

b

a

b

ST

q1

q0 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab

Time 3

b

a

b

ST

q1

q0 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab

Time 4

b

bTb

q1

q0 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab

Time 5

b

bTb

q1

q0 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab

Time 6

b

b

Ta

q1

q0 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab

Time 7

b

b

Ta

q1

q0 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab

Time 8

b

ba

q1

q0 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab

Time 9

b

b

q1

q0 q1 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab

Time 10

b

accept

In general:

Given any grammar G

We can construct a NPDA M

With )()( MLGL

Constructing NPDA from grammar :

q0 q1 2qS, , $ $

wA, aa,

M

For any production

wAFor any terminal

a

G

Grammar generates string G w

if and only if

NPDA acceptsM w

)()( MLGL

Therefore:

For any context-free languagethere is an NPDAthat accepts the same language

ConvertingConverting NPDAsNPDAstotoContext-Free Context-Free GrammarsGrammars

For any NPDA M

we will construct

a context-free grammar withG

)()( GLML

Intuition:

G

The grammar simulates the machine

A derivation in Grammar :

abcABCabcS

Current configuration in NPDA M

in NPDA M

abcABCabcS

Input processed Stack contents

terminals variables

GA derivation in Grammar :

Some Necessary Some Necessary Modifications Modifications First, we modify the NPDA:

• It has a single final state• It empties the stack when it accepts the input

Original NPDA Empty Stack

fq

fq

Second, we modify the NPDA transitions:

all transitions will have form

iq jqBa,

or

iq jqCDBa ,

symbolsstack :,, DCB

$,0q fq

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

}:{)( ba nnwML

Example of a NPDA in correct form:

symbolstack initial :$

The Grammar The Grammar ConstructionConstruction

)( jiBqq

In grammar :G

Terminals:Input symbols of NPDA

states

Stack symbol

Variables:

iq jqBa,For each transition

We add production aBqq ji )(

For each transition

We add production ))(()( klljki DqqCqqaBqq

iq jqCDBa ,

For all states lk qq ,

Start Variable: )$( fo qq

Stack bottom symbol

Start state final state

Example:

$,0q fq

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

aqq )1( 00Grammar production:

Example:

$,0q fq

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

)$)(1(|)$)(1()$(

)$)(1(|)$)(1()$(

00000

00000000

fffff

ff

qqqqbqqqqbqq

qqqqbqqqqbqq

Grammar productions:

Example:

$,0q fq

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

Grammar production: )$( 0 fqq

)$)(1(|)$)(1()$(

)$)(1(|)$)(1()$(

00000

00000000

fffff

ff

qqqqbqqqqbqq

qqqqbqqqqbqq

)1)(1(|)1)(1()1(

)1)(1(|)1)(1()1(

00000

00000000

fffff

ff

qqqqbqqqqbqq

qqqqbqqqqbqq

)$)(0(|)$)(0()$(

)$)(0(|)$)(0()$(

00000

00000000

fffff

ff

qqqqaqqqqaqq

qqqqaqqqqaqq

Resulting Grammar: ablestart vari:)$( 0 fqq

)0)(0(|)0)(0()0(

)0)(0(|)0)(0()0(

00000

00000000

fffff

ff

qqqqaqqqqaqq

qqqqaqqqqaqq

bqq

aqq

)0(

)1(

00

00

)$( 0 fqq

Derivation of string abba

)$( 0 fqq )$)(0( 000 fqqqqa

)$( 0 fqqab

)$)(1( 000 fqqqqabb

)$( 0 fqqabba abba

In general, in Grammar:

wqq f

)$( 0

if and only if

w is accepted by the NPDA

Explanation:

By construction of Grammar:

wAqq ji

)(

if and only if

in the NPDA going from tothe stack doesn’t change belowand is removed from stack

iq jq

AA

Recommended