… NPDAs continued

69
1 … NPDAs continued

description

… NPDAs continued. Pushing Strings. Pop symbol. Input symbol. Push string. Example:. input. pushed string. stack. top. Push. Another NPDA example. NPDA. Execution Example:. Time 0. Input. Stack. current state. Time 1. Input. Stack. Time 3. Input. Stack. Time 4. Input. - PowerPoint PPT Presentation

Transcript of … NPDAs continued

Page 1: … NPDAs continued

1

… NPDAs continued

Page 2: … NPDAs continued

2

Pushing Strings

q1 q2a, b w

Inputsymbol

Popsymbol

Pushstring

Page 3: … NPDAs continued

3

q1 q2a, b cdf

a

b top

input

stack

a

Push

eh h

e

cdf

pushedstring

Example:

$ $

Page 4: … NPDAs continued

4

Another NPDA example

$$, q1 q2

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

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

NPDA M

}:{)( ba nnwML

Page 5: … NPDAs continued

5

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 6: … NPDAs continued

6

Time 1

Input

a ab b ba

$

Stack

0

$$, q1 q2

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

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

Page 7: … NPDAs continued

7

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 8: … NPDAs continued

8

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 9: … NPDAs continued

9

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 10: … NPDAs continued

10

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 11: … NPDAs continued

11

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 12: … NPDAs continued

12

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 13: … NPDAs continued

13

Formalities for NPDAs

Page 14: … NPDAs continued

14

q1 q2a, b w

)},{(),,( 21 wqbaq

Transition function:

Page 15: … NPDAs continued

15

q1

q2wba ,

q3wba ,

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

Transition function:

Page 16: … NPDAs continued

16

Formal Definition

Non-Deterministic Pushdown Automaton NPDA

),,,,,( FzQM States

Inputalphabet

Stackalphabet

Transitionfunction

Finalstates

Stackstartsymbol

Page 17: … NPDAs continued

17

Instantaneous Description

),,( suq

Currentstate Remaining

input

Currentstackcontents

Page 18: … NPDAs continued

18

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 19: … NPDAs continued

19

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 20: … NPDAs continued

20

We write:

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

Time 4 Time 5

Page 21: … NPDAs continued

21

a, a

b, a q0 q1 q2 q3

b, a

, , $ $

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

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

,$),(,$),(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

A computation:

Page 22: … NPDAs continued

22

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

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

,$),(,$),(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

For convenience we write:

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

Page 23: … NPDAs continued

23

Formal Definition

Language of NPDA :M

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

Initial state Final state

Page 24: … NPDAs continued

24

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

a, a

b, a q0 q1 q2 q3

b, a

, , $ $

NPDA :M

)(MLaaabbb

Page 25: … NPDAs continued

25

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

a, a

b, a q0 q1 q2 q3

b, a

, , $ $

NPDA :M

)(MLba nn

Page 26: … NPDAs continued

26

a, a

b, a q0 q1 q2 q3

b, a

, , $ $

NPDA :M

}0:{)( nbaML nnTherefore:

Page 27: … NPDAs continued

27

NPDAs Accept Context-Free Languages

Page 28: … NPDAs continued

28

Context-Free

Languages(Grammars)

LanguagesAccepted

byNPDAs

Theorem:

Page 29: … NPDAs continued

29

Context-Free

Languages(Grammars)

LanguagesAccepted

byNPDAs

Proof - Step 1:

Convert any context-free grammar to a NPDA with:

GM )()( MLGL

Page 30: … NPDAs continued

30

Context-Free

Languages(Grammars)

LanguagesAccepted

byNPDAs

Proof - Step 2:

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

M)()( MLGL

Page 31: … NPDAs continued

31

Converting Context-Free Grammars

to NPDAs

Page 32: … NPDAs continued

32

An example grammar:

T

TaT

bS

aSTbS

What is the equivalent NPDA?

Page 33: … NPDAs continued

33q0 q1 2q

S, , $ $

Grammar:

NPDA:

T

TaT

bS

aSTbS

,

,

,

,

T

TaT

bS

aSTbS

bb

aa

,

,

Page 34: … NPDAs continued

34

The NPDA simulates leftmost derivations of the grammar

L(Grammar) = L(NPDA)

Page 35: … NPDAs continued

35

Grammar:

T

TaT

bS

aSTbS

A leftmost derivation:

abababTababTbaSTbS

Page 36: … NPDAs continued

36

NPDA execution:

0q q1 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab

Time 0

b

currentstate

Page 37: … NPDAs continued

37q0 q1 2q

S, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab

Time 1

b

S

Page 38: … NPDAs continued

38q0 2q

S, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab

Time 2

b

a

b

ST

q1

Page 39: … NPDAs continued

39q0 2q

S, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab

Time 3

b

a

b

ST

q1

Page 40: … NPDAs continued

40q0 2q

S, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab

Time 4

b

bTb

q1

Page 41: … NPDAs continued

41q0 2q

S, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab

Time 5

b

bTb

q1

Page 42: … NPDAs continued

42q0 2q

S, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab

Time 6

b

b

Ta

q1

Page 43: … NPDAs continued

43q0 2q

S, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab

Time 7

b

b

Ta

q1

Page 44: … NPDAs continued

44q0 2q

S, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab

Time 8

b

ba

q1

Page 45: … NPDAs continued

45q0 2q

S, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab

Time 9

b

b

q1

Page 46: … NPDAs continued

46q0 q1 2q

S, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab

Time 10

b

accept

Page 47: … NPDAs continued

47

In general:

Given any grammar G

We can construct a NPDA M

With )()( MLGL

Page 48: … NPDAs continued

48

Constructing NPDA from grammar :

q0 q1 2qS, , $ $

wA, aa,

M

For any production

wAFor any terminal

a

G

Page 49: … NPDAs continued

49

Grammar generates string G w

if and only if

NPDA acceptsM w

)()( MLGL

Page 50: … NPDAs continued

50

Therefore:

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

Page 51: … NPDAs continued

51

Converting NPDAs

toContext-Free Grammars

Page 52: … NPDAs continued

52

For any NPDA M

we will construct

a context-free grammar withG

)()( GLML

Page 53: … NPDAs continued

53

Intuition:

G

The grammar simulates the machine

A derivation in Grammar :

abcABCabcS

Current configuration in NPDA M

Page 54: … NPDAs continued

54

in NPDA M

abcABCabcS

Input processed Stack contents

terminals variables

GA derivation in Grammar :

Page 55: … NPDAs continued

55

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

Page 56: … NPDAs continued

56

Second, we modify the NPDA transitions:

all transitions will have form

iq jqBa,

or

iq jqCDBa ,

symbolsstack :,, DCB

Page 57: … NPDAs continued

57

$,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 58: … NPDAs continued

58

The Grammar Construction

)( jiBqq

In grammar :G

Terminals:Input symbols of NPDA

states

Stack symbol

Variables:

Page 59: … NPDAs continued

59

iq jqBa,For each transition

We add production aBqq ji )(

Page 60: … NPDAs continued

60

For each transition

We add production ))(()( klljki DqqCqqaBqq

iq jqCDBa ,

For all states lk qq ,

Page 61: … NPDAs continued

61

Start Variable: )$( fo qq

Stack bottom symbol

Start state final state

Page 62: … NPDAs continued

62

Example:

$,0q fq

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

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

aqq )1( 00Grammar production:

Page 63: … NPDAs continued

63

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 64: … NPDAs continued

64

Example:

$,0q fq

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

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

Grammar production: )$( 0 fqq

Page 65: … NPDAs continued

65

)$)(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 66: … NPDAs continued

66

)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 67: … NPDAs continued

67

Derivation of string abba

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

)$( 0 fqqab

)$)(1( 000 fqqqqabb

)$( 0 fqqabba abba

Page 68: … NPDAs continued

68

In general, in Grammar:

wqq f

)$( 0

if and only if

w is accepted by the NPDA

Page 69: … NPDAs continued

69

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