pushdown automata

115
Pushdown Pushdown Automata Automata PDAs PDAs

Upload: lavishkaanuj

Post on 14-Dec-2014

1.175 views

Category:

Technology


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Pushdown automata

Pushdown Pushdown AutomataAutomataPDAsPDAs

Page 2: Pushdown automata

Pushdown Automaton -- Pushdown Automaton -- PDAPDA Input String

Stack

States

Page 3: Pushdown automata

Initial Stack SymbolInitial Stack Symbol

Stack

$

Stack

z

bottomspecial symbol

Page 4: Pushdown automata

The StatesThe States

q1 q2a, b c

Inputsymbol

Popsymbol

Pushsymbol

Page 5: Pushdown automata

q1 q2a, b c

a

b top

input

stack

a

Replaceeh

$eh

$

c

Page 6: Pushdown automata

q1 q2a, c

a a

Pushb

eh

$eh

$

bc

top

input

stack

Page 7: Pushdown automata

q1 q2a, b

a a

Popb

eh

$eh

$

top

input

stack

Page 8: Pushdown automata

q1 q2a,

a a

No Changeb

eh

$eh

$

btop

input

stack

Page 9: Pushdown automata

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)

Page 10: Pushdown automata

NPDA: Non-NPDA: Non-Deterministic PDADeterministic PDA

Example:

,

a, a

b, a q0 q1 q2 q3

b, a

, $ $

Page 11: Pushdown automata

a, a

b, a 0q q1 q2 q3

Execution Example:

Input

a a a b b b

currentstate

b, a

Time 0

, , $ $

Stack

$

Page 12: Pushdown automata

a, a

b, a q0 q1 q2 q3

Input

a a a b b b

b, a

Time 1

, , $ $

Stack

$

Page 13: Pushdown automata

a, a

b, a q0 q1 q2 q3

Input

Stack

a a a b b b

$

a

b, a

Time 2

, , $ $

Page 14: Pushdown automata

a, a

b, a q0 q1 q2 q3

Input

Stack

a a a b b b$

aa

b, a

Time 3

, , $ $

Page 15: Pushdown automata

a, a

b, a q0 q1 q2 q3

Input

Stack

a a a b b b

$

aaa

b, a

Time 4

, , $ $

Page 16: Pushdown automata

a, a

b, a q0 q1 q2 q3

Input

a a a b b b

Stack

$

aaa

b, a

Time 5

, , $ $

Page 17: Pushdown automata

a, a

b, a q0 q1 q2 q3

Input

a a a b b b$

a

Stack

b, a

Time 6

, , $ $

a

Page 18: Pushdown automata

a, a

b, a q0 q1 q2 q3

Input

a a a b b b$

Stack

b, a

Time 7

, , $ $

a

Page 19: Pushdown automata

a, a

b, a q0 q1 q2 q3

Input

a a a b b b

b, a

Time 8

accept, , $ $

$

Stack

Page 20: Pushdown automata

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

Page 21: Pushdown automata

Formal DefinitionFormal DefinitionNon-Deterministic Pushdown Automaton NPDA

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

States

Inputalphabet

Stackalphabet

Transitionfunction

Finalstates

Stackstartsymbol

Initialstate

Page 22: Pushdown automata

Instantaneous Instantaneous DescriptionDescription

),,( suq

Currentstate Remaining

input

Currentstackcontents

Page 23: Pushdown automata

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 ..

Page 24: Pushdown automata

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.

Page 25: Pushdown automata

The input stringis accepted by the NPDA:

aaabbb

a, a

b, a q0 q1 q2 q3

b, a

, , $ $

Page 26: Pushdown automata

}0:{ nbaL nn

is the language accepted by the NPDA:

a, a

b, a q0 q1 q2 q3

b, a

In general,

, , $ $

Page 27: Pushdown automata

Another NPDA exampleAnother NPDA example

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

NPDA M

}{)( RwwML

Page 28: Pushdown automata

Execution Example:

Input

Time 0

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

a ab b

Page 29: Pushdown automata

Input

a ab

Time 1

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

ab

Page 30: Pushdown automata

Input

Time 2

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

aa ab b

b

Page 31: Pushdown automata

Input

Time 3

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

aa ab b

b

Guess the middle of string

Page 32: Pushdown automata

Input

Time 4

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

aa ab b

b

Page 33: Pushdown automata

Input

Time 5

Stack

$

, $ $1q q2

bb

aa

,

,

, q0

bb

aa

,

,

a ab b a

Page 34: Pushdown automata

Input

Time 6

Stack

$

, $ $q1

bb

aa

,

,

, q0

bb

aa

,

,

a ab b

accept

q2

Page 35: Pushdown automata

Rejection Example:

Input

Time 0

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

a b b b

Page 36: Pushdown automata

Input

Time 1

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

aa b b b

Page 37: Pushdown automata

Input

Time 2

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

ab

a b b b

Page 38: Pushdown automata

Input

Time 3

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

ab

Guess the middle of string

a b b b

Page 39: Pushdown automata

Input

Time 4

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

ab

a b b b

Page 40: Pushdown automata

Input

Time 5

Stack

$

, $ $1q q2

bb

aa

,

,

, q0

bb

aa

,

,

aa b b b

There is no possible transition.

Input is not consumed

Page 41: Pushdown automata

Another computation on same string:

Input Time 0

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

a b b b

Page 42: Pushdown automata

Input

Time 1

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

aa b b b

Page 43: Pushdown automata

Input

Time 2

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

ab

a b b b

Page 44: Pushdown automata

Input

Time 3

Stack

$ab

a b b b

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

b

Page 45: Pushdown automata

Input

Time 4

Stack

a b b b

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

$abbb

Page 46: Pushdown automata

Input

Time 5

Stack

a b b b

$abbb

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

No final state is reached

Page 47: Pushdown automata

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

There is no computation that accepts string abbb

)(MLabbb

Page 48: Pushdown automata

Pushing StringsPushing Strings

q1 q2a, b w

Inputsymbol

Popsymbol

Pushstring

Page 49: Pushdown automata

q1 q2a, b cdf

a

b top

input

stack

a

Push

eh h

e

cdf

pushedstring

Example:

$ $

Page 50: Pushdown automata

Another NPDA exampleAnother NPDA example

$$, q1 q2

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

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

NPDA M

}:{)( ba nnwML

Page 51: Pushdown automata

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:

Page 52: Pushdown automata

Time 1

Input

a ab b ba

$

Stack

0

$$, q1 q2

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

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

Page 53: Pushdown automata

Time 3

Input

a bb b a

$

Stack

a

$

$$, q1 q2

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

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

0

Page 54: Pushdown automata

Time 4

Input

a bb b a

$

Stack

a

1

$$, q1 q2

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

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

Page 55: Pushdown automata

Time 5

Input

a bb b a

$

Stack

a

11

$$, q1 q2

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

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

Page 56: Pushdown automata

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

Page 57: Pushdown automata

Time 7

Input

a bb b a

$

Stack

a

1

$$, q1 q2

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

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

Page 58: Pushdown automata

Time 8

Input

a bb b a a

$

Stack

$$, q1 q2

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

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

accept

Page 59: Pushdown automata

Formalities for Formalities for NPDAsNPDAs

Page 60: Pushdown automata

q1 q2a, b w

)},{(),,( 21 wqbaq

Transition function:

Page 61: Pushdown automata

q1

q2wba ,

q3wba ,

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

Transition function:

Page 62: Pushdown automata

Formal DefinitionFormal DefinitionNon-Deterministic Pushdown Automaton NPDA

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

States

Inputalphabet

Stackalphabet

Transitionfunction

Finalstates

Stackstartsymbol

Initialstate

Page 63: Pushdown automata

Instantaneous Instantaneous DescriptionDescription

),,( suq

Currentstate Remaining

input

Currentstackcontents

Page 64: Pushdown automata

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

Page 65: Pushdown automata

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

Page 66: Pushdown automata

We write:

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

Time 4 Time 5

Page 67: Pushdown automata

a, a

b, a q0 q1 q2 q3

b, a

, , $ $

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

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

,$),(,$),(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

A computation:

Page 68: Pushdown automata

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

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

,$),(,$),(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

For convenience we write:

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

Page 69: Pushdown automata

Formal DefinitionFormal DefinitionLanguage of NPDA :M

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

Initial state Final state

Page 70: Pushdown automata

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

a, a

b, a q0 q1 q2 q3

b, a

, , $ $

NPDA :M

)(MLaaabbb

Page 71: Pushdown automata

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

a, a

b, a q0 q1 q2 q3

b, a

, , $ $

NPDA :M

)(MLba nn

Page 72: Pushdown automata

a, a

b, a q0 q1 q2 q3

b, a

, , $ $

NPDA :M

}0:{)( nbaML nnTherefore:

Page 73: Pushdown automata

NPDAs Accept NPDAs Accept Context-Free Context-Free LanguagesLanguages

Page 74: Pushdown automata

Context-Free

Languages(Grammars)

LanguagesAccepted

byNPDAs

Theorem:

Page 75: Pushdown automata

Context-Free

Languages(Grammars)

LanguagesAccepted

byNPDAs

Proof - Step 1:

Convert any context-free grammar to a NPDA with:

GM )()( MLGL

Page 76: Pushdown automata

Context-Free

Languages(Grammars)

LanguagesAccepted

byNPDAs

Proof - Step 2:

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

M)()( MLGL

Page 77: Pushdown automata

ConvertingConverting Context-Free Context-Free GrammarsGrammarsto to NPDAsNPDAs

Page 78: Pushdown automata

An example grammar:

T

TaT

bS

aSTbS

What is the equivalent NPDA?

Page 79: Pushdown automata

q0 q1 2qS, , $ $

Grammar:

NPDA:

T

TaT

bS

aSTbS

,

,

,

,

T

TaT

bS

aSTbS

bb

aa

,

,

Page 80: Pushdown automata

The NPDA simulates leftmost derivations of the grammar

L(Grammar) = L(NPDA)

Page 81: Pushdown automata

Grammar:

T

TaT

bS

aSTbS

A leftmost derivation:

abababTababTbaSTbS

Page 82: Pushdown automata

NPDA execution:

0q q1 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab

Time 0

b

currentstate

Page 83: Pushdown automata

q0 q1 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab

Time 1

b

S

Page 84: Pushdown automata

q0 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab

Time 2

b

a

b

ST

q1

Page 85: Pushdown automata

q0 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab

Time 3

b

a

b

ST

q1

Page 86: Pushdown automata

q0 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab

Time 4

b

bTb

q1

Page 87: Pushdown automata

q0 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab

Time 5

b

bTb

q1

Page 88: Pushdown automata

q0 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab

Time 6

b

b

Ta

q1

Page 89: Pushdown automata

q0 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab

Time 7

b

b

Ta

q1

Page 90: Pushdown automata

q0 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab

Time 8

b

ba

q1

Page 91: Pushdown automata

q0 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab

Time 9

b

b

q1

Page 92: Pushdown automata

q0 q1 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab

Time 10

b

accept

Page 93: Pushdown automata

In general:

Given any grammar G

We can construct a NPDA M

With )()( MLGL

Page 94: Pushdown automata

Constructing NPDA from grammar :

q0 q1 2qS, , $ $

wA, aa,

M

For any production

wAFor any terminal

a

G

Page 95: Pushdown automata

Grammar generates string G w

if and only if

NPDA acceptsM w

)()( MLGL

Page 96: Pushdown automata

Therefore:

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

Page 97: Pushdown automata

ConvertingConverting NPDAsNPDAstotoContext-Free Context-Free GrammarsGrammars

Page 98: Pushdown automata

For any NPDA M

we will construct

a context-free grammar withG

)()( GLML

Page 99: Pushdown automata

Intuition:

G

The grammar simulates the machine

A derivation in Grammar :

abcABCabcS

Current configuration in NPDA M

Page 100: Pushdown automata

in NPDA M

abcABCabcS

Input processed Stack contents

terminals variables

GA derivation in Grammar :

Page 101: Pushdown automata

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

Page 102: Pushdown automata

Second, we modify the NPDA transitions:

all transitions will have form

iq jqBa,

or

iq jqCDBa ,

symbolsstack :,, DCB

Page 103: Pushdown automata

$,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 :$

Page 104: Pushdown automata

The Grammar The Grammar ConstructionConstruction

)( jiBqq

In grammar :G

Terminals:Input symbols of NPDA

states

Stack symbol

Variables:

Page 105: Pushdown automata

iq jqBa,For each transition

We add production aBqq ji )(

Page 106: Pushdown automata

For each transition

We add production ))(()( klljki DqqCqqaBqq

iq jqCDBa ,

For all states lk qq ,

Page 107: Pushdown automata

Start Variable: )$( fo qq

Stack bottom symbol

Start state final state

Page 108: Pushdown automata

Example:

$,0q fq

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

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

aqq )1( 00Grammar production:

Page 109: Pushdown automata

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:

Page 110: Pushdown automata

Example:

$,0q fq

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

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

Grammar production: )$( 0 fqq

Page 111: Pushdown automata

)$)(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

Page 112: Pushdown automata

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

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

00000

00000000

fffff

ff

qqqqaqqqqaqq

qqqqaqqqqaqq

bqq

aqq

)0(

)1(

00

00

)$( 0 fqq

Page 113: Pushdown automata

Derivation of string abba

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

)$( 0 fqqab

)$)(1( 000 fqqqqabb

)$( 0 fqqabba abba

Page 114: Pushdown automata

In general, in Grammar:

wqq f

)$( 0

if and only if

w is accepted by the NPDA

Page 115: Pushdown automata

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