courtesy costas busch - rpi1 npdas accept context-free languages

100
Courtesy Costas Busch - R PI 1 NPDAs Accept Context-Free Languages

Post on 20-Dec-2015

215 views

Category:

Documents


1 download

TRANSCRIPT

Courtesy Costas Busch - RPI 1

NPDAs Accept Context-Free Languages

Courtesy Costas Busch - RPI 2

Instantaneous Description

),,( suq

Currentstate Remaining

input

Currentstackcontents

3

Properties of Instantaneous Description

• If an ID sequence is a legal computation for a PDA, then so is the sequence obtained by adding an additional string at the end of component number two.

),,(),,(

),,(),,(

*

*

ywpxwq

ypxq

P

P

4

Properties of Instantaneous Description

• If an ID sequence is a legal computation for a PDA, then so is the sequence obtained by adding an additional string at the bottom of component number three.

),,(),,(

),,(),,(

*

*

ypxq

ypxq

P

P

5

Properties of Instantaneous Description

• If an ID sequence is a legal computation for a PDA, and some tail of the input is not consumed, then removing this tail from all ID's result in a legal computation sequence.

),,(),,(

),,(),,(

*

*

ypxq

ywpxwq

P

P

6

Languages of PDAAcceptance by Final State

Language of NPDA :M

Fq

qZwqwML

f

f

)},,(),,(:{)( 00

Initial state Final state

)(ML),,,,,,( 00 FZqQM

7

Languages of PDAAcceptance by Empty Stack

Language of NPDA :M

)},,(),,(:{)( 00 qZwqwMN

Initial state Any state

)(ML),,,,,,( 00 FZqQM

8

From Empty Stack to Final State

),,,,,( 00 ZqQPN

FP

If of some PDA)( NPNL

Then there is a PDA such that

)( FPLL

9

From Empty Stack to Final StateProof: Let

10

Property # 2

11

Then there is a PDA such that

From Final State to Empty Stack

),,,,,,( 00 FZqQPF

NP

If for some PDA)( FPLL

)( NPNL

12

From Final State to Empty StackProof: Let

13

from Property # 2 of ID

Courtesy Costas Busch - RPI 14

Context-Free

Languages(Grammars)

LanguagesAccepted

byNPDAs

Theorem:

Courtesy Costas Busch - RPI 15

Context-Free

Languages(Grammars)

LanguagesAccepted

byNPDAs

Proof - Step 1:

Convert any context-free grammar to a NPDA with:

GM )()( MLGL

Courtesy Costas Busch - RPI 16

Context-Free

Languages(Grammars)

LanguagesAccepted

byNPDAs

Proof - Step 2:

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

M)()( MLGL

Courtesy Costas Busch - RPI 17

Converting Context-Free Grammars

to NPDAs

Proof - step 1

Courtesy Costas Busch - RPI 18

to an NPDA automaton M

We will convert any context-free grammar G

Such that:

M Simulates leftmost derivations of G

Courtesy Costas Busch - RPI 19

mk VVVS 2121

1 2 k 1k

$

1V2V

mV

Inputprocessed

Stackcontents

:G

:M

Input

Stack

leftmost variable

Leftmost derivation

Simulation of derivation

Courtesy Costas Busch - RPI 20

nS 21

1 2 n $

:G

:M

Input

Stack

Leftmost derivation

Simulation of derivation

string of terminals

end of input is reached

Courtesy Costas Busch - RPI 21

An example grammar:

T

TaT

bS

aSTbS

What is the equivalent NPDA?

Courtesy Costas Busch - RPI 22

q0 q1 2qS, , $ $

Grammar:

NPDA:

T

TaT

bS

aSTbS

,

,

,

,

T

TaT

bS

aSTbS

bb

aa

,

,

Courtesy Costas Busch - RPI 23

Grammar:

T

TaT

bS

aSTbS

A leftmost derivation:

abababTababTbaSTbS

Courtesy Costas Busch - RPI 240q q1 2q

S, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab

Time 0

b

Derivation:

Courtesy Costas Busch - RPI 25

q0 q1 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab b

S

Time 0

Derivation: S

Courtesy Costas Busch - RPI 26

q0 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab b

a

b

ST

q1

Time 1

Derivation: aSTbS

Courtesy Costas Busch - RPI 27

q0 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab b

a

b

ST

q1

Time 2

aSTbS Derivation:

Courtesy Costas Busch - RPI 28

q0 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab b

bTb

q1

Time 3

Derivation: abTbaSTbS

Courtesy Costas Busch - RPI 29

q0 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab b

bTb

q1

Time 4

Derivation: abTbaSTbS

Courtesy Costas Busch - RPI 30

q0 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab b

b

Ta

q1

Time 5

Derivation: abTababTbaSTbS

Courtesy Costas Busch - RPI 31

q0 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab b

b

Ta

q1

Time 6

Derivation: abababTababTbaSTbS

Courtesy Costas Busch - RPI 32

q0 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab b

ba

q1

Time 7

Derivation: abababTababTbaSTbS

Courtesy Costas Busch - RPI 33

q0 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab b

b

q1

Time 8

Derivation:

Courtesy Costas Busch - RPI 34

q0 q1 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab b

accept

Time 9

Derivation:

Courtesy Costas Busch - RPI 35

In general:

Given any grammar G

We can construct a NPDA M

With )()( MLGL

}){$,,,,,},,,({ 20210 qqTVTqqqM

),,,( PSTVG

Courtesy Costas Busch - RPI 36

Constructing NPDA from grammar :

q0 q1 2qS, , $ $

wA, aa,

M

For any production

wAFor any terminal

a

G

Courtesy Costas Busch - RPI 37

Grammar generates string G w

if and only if

NPDA acceptsM w

)()( MLGL

Courtesy Costas Busch - RPI 38

Therefore:

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

Context-Free

Languages(Grammars)

LanguagesAccepted

byNPDAs

Courtesy Costas Busch - RPI 39

Note: From CFG to PDA accepting by emptying stack

Given any grammar G

We can construct a NPDA M

With )()( MNGL

),,,( PSTVG

),,,,},({ SqTVTqM

Courtesy Costas Busch - RPI 40

Constructing NPDA from grammar :

q

wA, aa,

M

For any production

wAFor any terminal

a

G

Courtesy Costas Busch - RPI 41

Converting NPDAs

toContext-Free Grammars

Proof - step 2

Courtesy Costas Busch - RPI 42

For any NPDA M

we will construct

a context-free grammar withG

)()( GLML

Courtesy Costas Busch - RPI 43

Intuition:

G

The grammar simulates the machine

A derivation in Grammar :

Current configuration in NPDA M

abcABCabcS

Input processed Stack contents

terminals variables

Courtesy Costas Busch - RPI 44

From NPDA to CFG

Lets look at how a PDA can consumeand empty the stack.

We shall define a grammar with variables of theform [pi-1 Yi pi] that would represent going from pi-1 to pi with the net effect of popping Yi.

kxxxx 21

45

QsrrYspXraqZsZaqXYp

QrpYraqZrZaqYp

aqZpZaqp

,:]][[][),,(),(

:][][),,(),(

][),,(),(

),,(),,(*

0 pZwqo

To generate all those strings w that cause P to pop Z0 from its stack while going from q0 to p.

Courtesy Costas Busch - RPI 46

47

48

}],[],[],[],[],[],[],[],{[ 0000 SqpZppZpXqpXppqZqqZqXpqXqV

49

Courtesy Costas Busch - RPI 50

Some Necessary Modifications

Modify (if necessary) the NPDA (accepting by reaching final state) so that:

1) The stack is never empty

2) It has a single final state and empties the stack when it accepts a string

3) Has transitions in a special form

Courtesy Costas Busch - RPI 51

1)Modify the NPDA so that the stack is never empty

$

Stack

a

OK

$

OK NOT OK

Courtesy Costas Busch - RPI 52

$

a$

Introduce the new symbol to denote the bottom of the stack

#

$

a#

$#

#

Courtesy Costas Busch - RPI 53

Original NPDA

#,

At the beginning push into the stack#

original initial state

new initial state

Courtesy Costas Busch - RPI 54

iq jqba $,

iq jqba #,

In transitions: replace every instance of with $ #

Example:

Courtesy Costas Busch - RPI 55

if the automaton attempts to popor replace it will halt $

Convert all transitions so that:

Courtesy Costas Busch - RPI 56

iq jqs ,

iq jqxx , s ,

$$ ,

{$}x

Convert transitions as follows:

halting state

Courtesy Costas Busch - RPI 57

NPDA

fq

x,

,

Empty the stack{$}x

2) Modify the NPDA so that it empties the stack and has a unique final state

,

,

Old final states

$,

Courtesy Costas Busch - RPI 58

3) modify the NPDA so that transitions have the following forms:

iq jq B,

OR

iq jqCDB ,

symbolsstack :,, DCB

Courtesy Costas Busch - RPI 59

iq jqy ,

Convert:

iq jq y,

{$}

Courtesy Costas Busch - RPI 60

iq jqBA,

Convert:

iq jqXBA, X,

{$}X

symbols

Courtesy Costas Busch - RPI 61

iq jqByA,

Convert:

iq jqyA, BXX ,

{$}X

symbols2|| y

Convert recursively

Courtesy Costas Busch - RPI 62

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

Courtesy Costas Busch - RPI 63

The Grammar Construction

)( jiBqq

In grammar :G

Terminals:Input symbols of NPDA

states

Stack symbol

Variables:

Courtesy Costas Busch - RPI 64

iq jqBa,For each transition

We add production aBqq ji )(

Courtesy Costas Busch - RPI 65

For each transition

We add productions

))(()( klljki DqqCqqaBqq

iq jqCDBa ,

For all possible statesin the automaton

lk qq ,

Courtesy Costas Busch - RPI 66

Start Variable: )$( fo qq

Stack bottom symbol

Start state final state

Courtesy Costas Busch - RPI 67

Example:

$,0q fq

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

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

aqq )1( 00Grammar production:

Courtesy Costas Busch - RPI 68

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:

Courtesy Costas Busch - RPI 69

Example:

$,0q fq

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

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

Grammar production: )$( 0 fqq

Courtesy Costas Busch - RPI 70

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

Courtesy Costas Busch - RPI 71

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

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

00000

00000000

fffff

ff

qqqqaqqqqaqq

qqqqaqqqqaqq

bqq

aqq

)0(

)1(

00

00

)$( 0 fqq

Courtesy Costas Busch - RPI 72

Derivation of string abba

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

)$( 0 fqqab

)$)(1( 000 fqqqqabb

)$( 0 fqqabba abba

Courtesy Costas Busch - RPI 73

In general:

wAqq ji

)(

if and only if

the NPDA goes from toby reading string andthe stack doesn’t change belowand then is removed from stack

iq jq

AA

w

Courtesy Costas Busch - RPI 74

Therefore:

wqq f

)$( 0

if and only if

w is accepted by the NPDA

Courtesy Costas Busch - RPI 75

Therefore:

For any NPDAthere is a context-free grammarthat accepts the same language

Context-Free

Languages(Grammars)

LanguagesAccepted

byNPDAs

Courtesy Costas Busch - RPI 76

Deterministic PDA

DPDA

Courtesy Costas Busch - RPI 77

Deterministic PDA: DPDA

q1 q2wba ,

1q 2qwb,

Allowed transitions:

(deterministic choices)

Courtesy Costas Busch - RPI 78

Allowed transitions:

q1

q21, wb

q32, wc

q1

q21, wba

q32, wca

(deterministic choices)

Courtesy Costas Busch - RPI 79

q1

q21, wba

q32, wba

Not allowed:

(non deterministic choices)

q1

q21, wb

q32, wba

Courtesy Costas Busch - RPI 80

DPDA example

a, a

b, a q0 q1 q2 q3

b, a

, $ $

}0:{)( nbaML nn

a, a

Courtesy Costas Busch - RPI 81

}0:{)( nbaML nnThe language

is deterministic context-free

Courtesy Costas Busch - RPI 82

Definition:

A language is deterministic context-freeif there exists some DPDA that accepts it

L

Courtesy Costas Busch - RPI 83

Example of Non-DPDA (NPDA)

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

}{)( RwwML

Courtesy Costas Busch - RPI 84

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

Not allowed in DPDAs

Courtesy Costas Busch - RPI 85

NPDAs

Have More Power than

DPDAs

Courtesy Costas Busch - RPI 86

Deterministic

Context-FreeLanguages

(DPDA)

Context-Free

LanguagesNPDAs

Since every DPDA is also a NPDA

It holds that:

Courtesy Costas Busch - RPI 87

We will actually show:

We will show that there existsa context-free language which is notaccepted by any DPDA

L

LL

Deterministic

Context-FreeLanguages

(DPDA)

Context-Free

Languages(NPDA)

Courtesy Costas Busch - RPI 88

The language is:

}{}{ 2nnnn babaL 0n

We will show:

L• is context-free

L• is not deterministic context-free

Courtesy Costas Busch - RPI 89

}{}{ 2nnnn babaL

Language is context-freeL

Context-free grammar for : L

21 | SSS

|11 baSS

|22 bbaSS

}{ nnba

}{ 2nnba

}{}{ 2nnnn baba

Courtesy Costas Busch - RPI 90

}{}{ 2nnnn babaL

is not deterministic context-free

Theorem:

The language

(there is no DPDA that accepts ) L

Courtesy Costas Busch - RPI 91

Proof: Assume for contradiction that

}{}{ 2nnnn babaL

is deterministic context free

Therefore:

there is a DPDA that accepts M L

Courtesy Costas Busch - RPI 92

DPDA with M

nnba nb

acceptsnnba 2

acceptsnnba

}{}{)( 2nnnn babaML

Courtesy Costas Busch - RPI 93

DPDA with M

nnba nb

}{}{)( 2nnnn babaML

Such a path exists because of the determinism

M

Courtesy Costas Busch - RPI 94

The language is not context-free

}{ nnn cba

(we will prove this at a later class usingpumping lemma for context-free languages)

Fact 1:

Regular languages**ba

Context-free languagesnnba

Courtesy Costas Busch - RPI 95

The language is not context-free

}{ nnn cbaL

}){}{( 2nnnn babaL

Fact 2:

(we can prove this using pumping lemma for context-free languages)

Courtesy Costas Busch - RPI 96

We will construct a NPDA that accepts:

}{ nnn cbaL

}){}{( 2nnnn babaL

which is a contradiction!

Courtesy Costas Busch - RPI 97

Modify M

M ncnnca

}{}{)( 2nnnn babaML

}{}{)( 2nnnn cacaML

Replacewith

bc

nnba nb

M

Courtesy Costas Busch - RPI 98

The NPDA that accepts }{ nnn cbaL

nnba nb

nc

M

M

nnca

Connect final states of with final states of

M

M

Courtesy Costas Busch - RPI 99

Since is accepted by a NPDA }{ nnn cbaL

it is context-free

Contradiction!

(since is not context-free)}{ nnn cbaL

Courtesy Costas Busch - RPI 100

Therefore:

}{}{ 2nnnn babaL

There is no DPDA that accepts

End of Proof

Not deterministic context free