1 converting npdas to context-free grammars. 2 for any npda we will construct a context-free grammar...

43
1 Converting NPDAs to Context-Free Grammars

Post on 20-Dec-2015

222 views

Category:

Documents


0 download

TRANSCRIPT

1

Converting NPDAsto

Context-Free Grammars

2

For any NPDA M

we will construct

a context-free grammar withG

)()( GLML

3

Intuition:

G

The grammar simulates the machine

A derivation in Grammar :

abcABCabcS

Current configuration in NPDA M

4

G

in NPDA M

A derivation in Grammar :

abcABCabcS

Input processed Stack contents

terminals variables

5

Some Necessary 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

6

Second, we modify the NPDA transitions:

all transitions will have form

iq jqBa,

or

iq jqCDBa ,

symbolsstack :,, DCB

7

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

8

The Grammar Construction

)( jiBqq

In grammar

variables have form

G

Terminals are input symbols

states

stacksymbol

9

iq jqBa,

For each transition

We add production

aBqq ji )(

For all states kq

10

For each transition

We add production

))(()( klljki DqqCqqaBqq

iq jqCDBa ,

For all states in the NPDA lk qq ,

11

Start Variable: )$( fo qq

Stack bottom symbol

Start state final state

12

Example:

$,0q fq

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

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

aqq )1( 00

Grammar production:

13

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:

14

Example:

$,0q fq

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

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

Grammar production:

)$( 0 fqq

15

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

16

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

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

00000

00000000

fffff

ff

qqqqaqqqqaqq

qqqqaqqqqaqq

bqq

aqq

)0(

)1(

00

00

)$( 0 fqq

17

Derivation of string abba

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

)$( 0 fqqab

)$)(1( 000 fqqqqabb

)$( 0 fqqabba abba

18

In general, in grammar

wqq f

)$( 0

if and only if

w is accepted by the NPDA

19

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

20

Deterministic PDAs(DPDAs)

21

DPDAs

Allowed Transitions: q1 q2wba ,

q1

q21, wba

q32, wca

22

q1

q21, wba

q32, wba

Not allowed:

Not allowed even when is b

23

Allowed:1q 2q

wb,

q1

q21, wb

q32, wc

24

Not allowed:

q1

q21, wb

q32, wba

Not allowed even when is b

25

DPDA example

,

a, a

b, a q0 q1 q2 q3

b, a

, $ $

}0:{)( nbaML nn

26

}0:{)( nbaML nnThe language

is deterministic context-free

27

In general:

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

28

Example of Non-DPDA

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

}{)( RwwML

29

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

Not allowed transitions for DPDAs

30

NPDAs have more power than

DPDAs

31

We will show:

There is

which is not

a context-free language

deterministic context-free

(accepted by a NPDA)

(not accepted by a DPDA)

L

32

The language is:

}{}{ 2nnnn babaL

0n

33

}{}{ 2nnnn babaL

The language is context-freeL

Context-free grammar for : L

21 | SSS

|11 baSS

|22 bbaSS

(there is an NPDA that accepts ) L

34

}{}{ 2nnnn babaL

is not deterministic context-free

Theorem:

The language

(there is no DPDA that accepts ) L

35

Proof:

Assume for contradiction that

}{}{ 2nnnn babaL

is deterministic context free

Therefore:

There exists a DPDAthat accepts

ML

36

The DPDA with M

nnba nb

acceptsnnba 2

acceptsnnba

}{}{)( 2nnnn babaML

37

The language }{ nnn cba

is not context-free

(we will prove it at the next class)

A fact:

38

Another fact:

The language }{ nnn cbaLL

is not context-free

}){}{( 2nnnn babaL

39

We will construct a NPDA that accepts

}{ nnn cbaLL

}){}{( 2nnnn babaL

40

First, we modify :M

nc

Modified M

Replace with cb

nnca

41

The NPDA that accepts }{ nnn cbaLL

nnba nb

nc

Modified M

Original M

nnca

42

is accepted by a NPDA }{ nnn cbaLL

Therefore: L is context-free

Contradiction!

( is not context-free)L

43

Therefore:

}{}{ 2nnnn babaL

There is no DPDA that accepts

End of Proof