CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 8 Mälardalen University

108
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 8 Mälardalen University 2006

description

CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 8 Mälardalen University 2006. - PowerPoint PPT Presentation

Transcript of CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 8 Mälardalen University

Page 1: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

1

CD5560

FABER

Formal Languages, Automata and Models of Computation

Lecture 8

Mälardalen University

2006

Page 2: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

2

Content

Context-Free LanguagesPush-Down Automata, PDANPDA: Non-Deterministic PDAFormal Definitions for NPDAs NPDAs Accept Context-Free LanguagesConverting NPDA to Context-Free Grammar

Page 3: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

3

Regular Languages

}{ nnba }{ RwwContext-Free Languages

Non-regular languages

}0:{ ! nan}0,:{ lncba lnln

Page 4: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

4

Context-Free Languages

Page 5: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

5

Context-Free Languages

Pushdown

Automata

Context-Free

Grammars

stack

automaton(CF grammars are

defined as generalized Regular Grammars)

Page 6: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

6

Definition: Context-Free Grammars

Grammar

Productions of the form:xA

x is string of variables and terminals

),,,( PSTVG

Variables Terminal

symbols

Start

variables

Page 7: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

7

Definition: Regular Grammars

xBA

Grammar

Right or Left Linear Grammars. Productions of the form:

x is string of terminals

),,,( PSTVG

Variables Terminal

symbols

Start

variables

BxA C xor

Page 8: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

8

Pushdown AutomataPDAs

Page 9: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

9

Pushdown Automaton - PDA

Input String

Stack

States

Page 10: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

10

The Stack

The stack allows pushdown automata to recognize some non-regular languages.

All access to the stack only on the top!(Stack top is written leftmost in the string, e.g. yxz)

A PDA can write symbols on stack and read them later on.

A stack is valuable as it can hold an unlimited amount of information.

zxy

POP reading symbol PUSH writing symbol

Page 11: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

11

The States

Input

symbol

Pop oldreading

stack symbol

Push new

writing

stack symbol

q1 q2cba /,

Page 12: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

12

a

b top

input

stack

a

Replace

eh

$eh

$

c

q1 q2cba /,

(An alternative is to start and finish with empty stack)

Page 13: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

13

a a

input

Pushb

eh

$eh

$

bc

top

stack

q1 q2ca /,

Page 14: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

14

a a input

Popb

eh

$eh

$

top

stack

q1 q2/,ba

Page 15: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

15

a a input

No Changeb

eh

$eh

$

btop

stack

q1 q2 /,a

Page 16: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

16

Input Stack

( )

Example 3.7 Salling: A PDA for simple nested parenthesis strings

sstart q(/),

( ( ) )

end

(/,( /(,)

Time 0

Page 17: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

17

Input

Stack

( )

Example 3.7

sstart q(/),

(/,(

( ( ) )

/(,)

(

end

Time 1

Page 18: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

18

Input

Stack

( )

sstart q(/), end

( ( ) ) (

(

Example 3.7

(/,( /(,)

Time 2

Page 19: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

19

Input

( )

sstart q(/),

( ( ) )

Stack

(

((

end

Example 3.7

(/,( /(,)

Time 3

Page 20: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

20

Input

( )

sstart q(/),

( ( ) )

Stack

(

((

end

Example 3.7

(/,( /(,)

Time 4

Page 21: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

21

Input

( )

sstart (/),

( ( ) )

Stack

(

(

q

Example 3.7

end

(/,( /(,)

Time 5

Page 22: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

22

Input

Stack

( )

sstart q(/),

( ( ) )(

end

Example 3.7

(/,( /(,)

Time 6

Page 23: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

23

Input

( )

sstart q(/),

( ( ) )

Example 3.7

end(/),

(/,( /(,)

Time 7

Stack

Page 24: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

24

NPDAsNon-deterministic Push-Down Automata

Page 25: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

25

Non-Determinism

q1

q2cba /,

q3

q1 q2

transitioncba /,

cb /,

Page 26: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

26

A string is accepted if:

• All the input is consumed

• The last state is a final state

• Stack is in the initial condition(either: empty (when we started with empty stack), or: bottom symbol reached, or similar)

Page 27: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

27

}0:{ nbaL nn

is the language accepted by the NPDA:

Example NPDA

q0 q1 q2 q3 /, /,ab $/$,

/,abaa /,

},{ ba

Page 28: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

28

/,

/,

bb

aa

q1 q2 /,q0

NPDA M}{)( RwwML

bb

aa

/,

/,

$/$,

(Even-length palindromes)

},{ ba

Example NPDA

Page 29: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

29

Pushing Strings

q1 q2wba /,

Input

symbol

Pop

symbolPush

string

Page 30: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

30

q1 q2cdfba /,

a

b top

input

stack

a

Push

eh h

e

cdf

pushed

string

Example:

$ $

Page 31: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

31

Another NPDA example

$0/$,a00/0,a/1,a

$1/$,b11/1,b/0,b

$/$,q1 q2

NPDA M}:{)( ba nnwML

},{ ba

Page 32: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

32

Time 0

Input

a ab b ba

$

Stack

Current state

q1 q2

Execution Example:

$0/$,a00/0,a/1,a

$1/$,b11/1,b/0,b

$/$,

Page 33: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

33

Input

a ab b ba

Stack

$0

q1 q2

$0/$,a00/0,a/1,a

$1/$,b11/1,b/0,b

$/$,

Time 1

Page 34: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

34

Input

a bb b a

Stack

a

q1 q2

$0

$0/$,a00/0,a/1,a

$1/$,b11/1,b/0,b

$/$,

Time 2

Page 35: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

35

Input

a bb b a

Stack

a

$1

q1 q2

$0/$,a00/0,a/1,a

$1/$,b11/1,b/0,b

$/$,

Time 3

Page 36: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

36

Input

a bb b a

Stack

a

$11

q1 q2

$0/$,a00/0,a/1,a

$1/$,b11/1,b/0,b

$/$,

Time 4

Page 37: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

37

Input

a bb b a

Stack

a

q1 q2

$11

$0/$,a00/0,a/1,a

$1/$,b11/1,b/0,b

$/$,

Time 5

Page 38: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

38

Input

a bb b a

Stack

a

q1 q2

$1

$0/$,a00/0,a/1,a

$1/$,b11/1,b/0,b

$/$,

Time 6

Page 39: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

39

Input

a bb b a a

$

Stack

q1 q2

accept

$0/$,a00/0,a/1,a

$1/$,b11/1,b/0,b

$/$,

Time 7

Page 40: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

40

Formal Definitions for NPDAs

Page 41: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

41

q1 q2wba /,

)},{(),,( 21 wqbaq

Transition function:

Page 42: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

42

Transition function:

q1

q2wba /,

q3

wba /,

current state

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

current input symbolcurrent stack top

new state

new stack top

An unspecified transition function is to the null set and represents a dead configuration for the NPDA.

Page 43: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

43

Formal Definition

Non-Deterministic Pushdown Automaton NPDA

),,,,,( FzQM States

Input

alphabetStack

alphabet

Transition

function

Final

states

Stack

start

symbol

Page 44: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

44

Instantaneous Description

),,( suq

Current

state Remaining

input

Current

stack

contents

Page 45: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

45q0 q1 q2 q3

Input

Stack

a a a b b b$

aaTime 4:

Example Instantaneous Description

$),,( 1 aaabbbq

a

/, /,ab $/$,

/,abaa /,

),,( stackinputremainingstateurrentc

Page 46: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

46q0 q1 q2 q3

Input

Stack

a a a b b b$

aaTime 5:

Example Instantaneous Description

$),,( 2 aabbq

a

/, /,ab $/$,

/,abaa /,

),,( stackinputremainingstateurrentc

Page 47: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

47

We write

$),,($),,( 21 aabbqaaabbbq Time 4 Time 5

),,( contentstackcurrentinputremainingstatecurrent

Page 48: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

48

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

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

$),,($),,(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

A computation

q0 q1 q2 q3 /, /,ab $/$,

/,abaa /,

),,( contentstackcurrentinputremainingstatecurrent

Page 49: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

49

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

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

$),,($),,(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

A computation

q0 q1 q2 q3 /, /,ab $/$,

/,abaa /,

),,( contentstackcurrentinputremainingstatecurrent

Page 50: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

50

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

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

$),,($),,(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

A computation

q0 q1 q2 q3 /, /,ab $/$,

/,abaa /,

),,( contentstackcurrentinputremainingstatecurrent

Page 51: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

51

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

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

$),,($),,(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

A computation

q0 q1 q2 q3 /, /,ab $/$,

/,abaa /,

),,( contentstackcurrentinputremainingstatecurrent

Page 52: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

52

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

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

$),,($),,(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

A computation

q0 q1 q2 q3 /, /,ab $/$,

/,abaa /,

),,( contentstackcurrentinputremainingstatecurrent

Page 53: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

53

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

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

$),,($),,(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

A computation

q0 q1 q2 q3 /, /,ab $/$,

/,abaa /,

),,( contentstackcurrentinputremainingstatecurrent

Page 54: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

54

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

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

$),,($),,(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

A computation

q0 q1 q2 q3 /, /,ab $/$,

/,abaa /,

),,( contentstackcurrentinputremainingstatecurrent

Page 55: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

55

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

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

$),,($),,(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

A computation

q0 q1 q2 q3 /, /,ab $/$,

/,abaa /,

),,( contentstackcurrentinputremainingstatecurrent

Page 56: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

56

For convenience we write

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

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

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

$),,($),,(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

),,( contentstackcurrentinputremainingstatecurrent

Page 57: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

57

Formal Definition

Language of NPDA M

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

Initial state Final state

Page 58: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

58

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

q0 q1 q2 q3

NPDA :M

)(MLaaabbb

/, /,ab $/$,

/,abaa /,

Page 59: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

59

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

NPDA :M

)(MLba nn

q0 q1 q2 q3 /, /,ab $/$,

/,abaa /,

Page 60: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

60

NPDA :M

}0:{)( nbaML nnTherefore:

q0 q1 q2 q3 /, /,ab $/$,

/,abaa /,

Page 61: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

61

NPDAs Accept Context-Free Languages

Page 62: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

62

Context-Free

Languages

(Grammars)

Languages

Accepted by

NPDAs

Theorem

Page 63: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

63

Context-Free

Languages

(Grammars)

Languages

Accepted by

NPDAs

Proof - Step 1:

Convert any context-free grammar

to a NPDA with

GM )()( MLGL

Page 64: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

64

Context-Free

Languages

(Grammars)

Languages

Accepted by

NPDAs

Proof - Step 2:

Convert any NPDA to a context-free

grammar with: GM

)()( GLML

Page 65: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

65

Converting Context-Free Grammars

to NPDAs

Page 66: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

66

An example grammar:

T

TaT

bS

aSTbS

What is the equivalent NPDA?

Page 67: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

67

Grammar NPDA

/,

/,

/,

/,

T

TaT

bS

aSTbS

T

TaT

bS

aSTbS

/,

/,

bb

aa

q0 q1 2qS/, $/$,

Page 68: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

68

The NPDA simulates

leftmost derivations of the grammar

L(Grammar) = L(NPDA)

Page 69: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

69

Grammar:

T

TaT

bS

aSTbS

A leftmost derivation:

abababTabbTabTbSaS

Page 70: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

70

NPDA execution:

0q q1 2qS/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

Input

Stack

$

a ab

Time 0

b

Start

Page 71: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

71

q0 q1 2q

Input

Stack

a ab

Time 1

b

$S

S/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

Page 72: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

72

q0 2q

Input

Stack

a ab

Time 2

b

$

a

b

ST

q1S/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

Page 73: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

73

q0 2q

Input

Stack

a ab

Time 3

b

$

a

b

ST

q1S/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

Page 74: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

74

q0 2q

Input

Stack

a ab

Time 4

b

$bTb

q1S/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

Page 75: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

75

q0 2q

Input

Stack

a ab

Time 5

b

$bTb

q1S/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

Page 76: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

76

q0 2qS, , $ $

bb

aa

,

,

Input

Stack

a ab

Time 6

b

T

TaT

bS

aSTbS

,

,

,

, $b

Ta

q1

Page 77: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

77

q0 2q

Input

Stack

a ab

Time 7

b

$b

Ta

q1S/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

Page 78: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

78

q0 2q

Input

Stack

a ab

Time 8

b

$ba

q1S/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

Page 79: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

79

q0 2q

Input

Stack

a ab

Time 9

b

$b

q1S/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

Page 80: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

80

q0 q1 2q

Input

Stack

$

a ab

Time 10

b

acceptS/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

Page 81: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

81

In general

Given any grammar G

We can construct a NPDA M

With )()( MLGL

Page 82: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

82

q0 q1 2qS/, $/$,

wA /, /, aa

For any productionwA

For any terminala

Constructing NPDA from grammarM GTop-down parser

Page 83: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

83

Grammar generates string G w

if and only if

MNPDA accepts w

)()( MLGL

Page 84: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

84

Therefore:

For any context-free language

there is an NPDA

that accepts the same language

Page 85: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

85

Context-Free

Languages

(Grammars)

Languages

Accepted by

NPDAs

Which means

Page 86: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

86

Converting NPDAsto

Context-Free Grammars

Page 87: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

87

For any NPDA M

we will construct

a context-free grammar withG

)()( GLML

Page 88: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

88

in NPDA M

abcABCabcS

Input processed Stack contents

terminals variables

GA derivation in Grammar

The grammar simulates the machine

Page 89: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

89

Some Simplifications

First we modify the NPDA so that

• It has a single final state

• It empties the stack

when it accepts the input

Original NPDA Empty Stack

fq

fq

Page 90: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

90

Second we modify the NPDA transitions.

All transitions will have form:

iq jq/, Ba

or

iq jqCDBa /,

which means that each move

increases/decreases stack by a single symbol.

Page 91: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

91

Those simplifications do not affect generality of our argument.

It can be shown that for any NPDA there exists an equivalent one having above two properties

i.e.the equivalent NPDA with a single final state

which empties its stack when it accepts the input, and which for each move increases/decreases stack by a single symbol.

Page 92: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

92

/1,

00/0,

$0/$,

a

a

a

/$,0q fq

}:{)( ba nnwML

Example of a NPDA in an appropriate form

symbol)stack initial :($/0,

11/1,

$1/$,

b

b

b},{ ba

Page 93: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

93

The Grammar Construction

)( jiBqq

In grammar G

Terminals:

Input symbols of NPDA

states

Stack symbol

Variables:

Page 94: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

94

iq jq/, BaFor each transition:

we add production: aBqq ji )(

Page 95: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

95

For each transition:

we add production: ))(()( klliki DqqCqqaBqq

iq jqCDBa /,

for all states lk qq ,

Page 96: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

96

Start Variable )$( fo qq

Stack bottom symbol

Start state (Single) Final state

Page 97: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

97

Example

aqq )1( 00Grammar production:

0q fq

/1,

00/0,

$0/$,

a

a

a

/$,

/0,

11/1,

$1/$,

b

b

biq jq/, Ba

aBqq ji )(

}:{)( ba nnwML },{ ba

Page 98: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

98

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

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

00000

00000000

fffff

ff

qqqqbqqqqbqq

qqqqbqqqqbqq

Grammar productions:

0q fq

/1,

00/0,

$0/$,

a

a

a

/$,

/0,

11/1,

$1/$,

b

b

b iq jqCDBa /,

))(()( klljki DqqCqqaBqq

lk qqstatesallfor ,

Page 99: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

99

Grammar production: )$( 0 fqq

0q fq

/1,

00/0,

$0/$,

a

a

a

/$,

/0,

11/1,

$1/$,

b

b

biq jq/, Ba

aBqq ji )(

Page 100: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

100

0q fq

/1,

00/0,

$0/$,

a

a

a

/$,

/0,

11/1,

$1/$,

b

b

b

Resulting Grammar

ablestart vari:)$( 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

}:{)( ba nnwML },{ ba

))(()( klljki DqqCqqaBqq

lk qqstatesallfor ,

Page 101: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

101

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

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

00000

00000000

fffff

ff

qqqqaqqqqaqq

qqqqaqqqqaqq

Resulting Grammar, cont.

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

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

00000

00000000

fffff

ff

qqqqaqqqqaqq

qqqqaqqqqaqq

0q fq

/1,

00/0,

$0/$,

a

a

a

/$,

/0,

11/1,

$1/$,

b

b

b }:{)( ba nnwML

Page 102: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

102

bqq

aqq

)0(

)1(

00

00

)$( 0 fqq0q fq

/1,

00/0,

$0/$,

a

a

a

/$,

/0,

11/1,

$1/$,

b

b

b

Resulting Grammar, cont.

iq jq/, Ba

aBqq ji )(

}:{)( ba nnwML

Page 103: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

103

Derivation of string abba

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

)$( 0 fqqab

)$)(1( 000 fqqqqabb

)$( 0 fqqabba

abba

0q fq

/1,

00/0,

$0/$,

a

a

a

/$,

/0,

11/1,

$1/$,

b

b

b

iq jq/, Ba

aBqq ji )(

Page 104: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

104

In general, in grammar:

wqq f

)$( 0

if and only if

w is accepted by the NPDA

Page 105: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

105

Explanation

By construction of Grammar:

wAqq ji

)(

if and only if

in the NPDA going from to

the stack doesn’t change below

and is removed from stack

iq jq

A

iq jq/, Ba

aBqq ji )(

Page 106: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

106

Context-Free

Languages

(Grammars)

Languages

Accepted by

NPDAs

We have shown the procedure to convert

any NPDA to a context-free

grammar with:

which means

GM

)()( GLML

Page 107: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

107

Context-Free

Languages

(Grammars)

Languages

Accepted by

NPDAs

Therefore

END OF PROOF

Page 108: CD5560 FABER Formal Languages, Automata  and Models of Computation Lecture 8 Mälardalen University

108

q0 q1

Example (Sudkamp 8.1.2)

/, Ab

/,

/, AbAa /,

}0:{}0:{)( ibaiaML iii

Language consisting solely of a’s or an equal number of a´s and b´s.

q2 /, A