CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University

101
1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University 2010

description

CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University 2010. - PowerPoint PPT Presentation

Transcript of CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University

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

1

CDT314

FABER

Formal Languages, Automata and Models of Computation

Lecture 6

Mälardalen University

2010

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

2

Content

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

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

Midterm

• 5 out of 13 passed…• Many failed on basic definitions

• Is something wrong with the course format?• Scheduled exercises• Don’t hesitate to ask for help!• Do labs, exercises etc.• Review lecture material before lectures• Strongly recommended to get a book if you

don’t have one• It doesn’t get easier…

3

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

4

JFLAP demo

Concerning examination in the course:Exercises are voluntaryLabs are voluntaryMidterms are voluntaryLectures are voluntary…All of them are recommended!

http://www.cs.duke.edu/csed/jflap/movies

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

5

Regular Languages

}{ nnba }{ Rww

Context-Free Languages

Non-regular languages

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

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

6

Context-Free Languages

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

7

Context-Free Languages

Pushdown

Automata

Context-Free

Grammars

stack

automaton

(CF grammars are

defined as generalized Regular Grammars)

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

8

Definition: Context-Free Grammars

Grammar

Productions of the form:xA

x is string of variables and terminals

),,,( PSTVG

Variables Terminal

symbols

Start

variables

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

9

Pushdown AutomataPDAs

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

10

Pushdown Automaton - PDA

Input String

Stack

States

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

11

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 (certain kind of) information.

zxy

POP reading symbol PUSH writing symbol

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

12

The States

Input

symbol

Pop old- reading

stack symbol

Push new- writing

stack symbol

q1 q2cba /,

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

13

a

b top

input

stack

a

Replace

eh

$eh

$

c

q1 q2cba /,

(An alternative is to start and finish with empty stack or a stack bottom symbol such as $)

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

14

a a

input

Pushb

eh

$eh

$

bc

top

stack

q1 q2ca /,

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

15

a a input

Popb

eh

$eh

$

top

stack

q1 q2/,ba

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

16

a a input

No Changeb

eh

$eh

$

btop

stack

q1 q2 /,a

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

17

NPDAsNon-deterministic Push-Down Automata

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

18

Non-Determinism

q1

q2cba /,

q3

q1 q2

transitioncba /,

cb /,

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

19

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 20: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 6 Mälardalen University

20

}0:{ nbaL nn

is the language accepted by the NPDA:

Example NPDA

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

/, abaa /,

},{ ba

symbol)stack initial :($

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

21

/,

/,

bb

aa

q1 q2 /,q0

NPDA M

}{)( RwwML

bb

aa

/,

/,

$/$,

(Even-length palindromes)

},{ ba

Example NPDA

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

22

Pushing Strings

q1 q2wba /,

Input

symbol

Pop

symbolPush

string

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

23

q1 q2cdfba /,

a

b top

input

stack

a

Push

eh h

e

cdf

pushed

string

Example

$ $

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

24

Another NPDA example

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

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

$/$,q1 q2

NPDA M

}:{)( ba nnwML

},{ ba

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

25

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 26: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 6 Mälardalen University

26

Input

a ab b ba

Stack

$0

q1 q2

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

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

$/$,

Time 1

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

27

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 28: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 6 Mälardalen University

28

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 29: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 6 Mälardalen University

29

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 30: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 6 Mälardalen University

30

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 31: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 6 Mälardalen University

31

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 32: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 6 Mälardalen University

32

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 33: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 6 Mälardalen University

33

Formal Definitions for NPDAs

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

34

q1 q2wba /,

)},{(),,( 21 wqbaq

Transition function

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

35

Transition function

q1

q2wba /,

q3

wba /,

current state

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

current input symbol

current 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 36: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 6 Mälardalen University

36

Formal Definition

Non-Deterministic Pushdown Automaton NPDA

),,,,,( FzQM States

Input

alphabetStack

alphabet

Transition

function

Final

states

Stack

start

symbol

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

37

Instantaneous Description

),,( suq

Current

state Remaining

input

Current

stack

contents

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

38q0 q1 q2 q3

Input

Stack

a a a b b b$

aaTime 4:

Example Instantaneous Description

$),,( 1 aaabbbq

a

/, /, ab $/$,

/, abaa /,

),,( stackinputremainingstateurrentc

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

39q0 q1 q2 q3

Input

Stack

a a a b b b$

aaTime 5:

Example Instantaneous Description

$),,( 2 aabbq

a

/, /, ab $/$,

/, abaa /,

),,( stackinputremainingstateurrentc

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

40

We write

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

),,( contentstackcurrentinputremainingstatecurrent

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

41

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

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

$),,($),,(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

A computation example

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

/, abaa /,

),,( contentstackcurrentinputremainingstatecurrent

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

42

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

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

$),,($),,(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

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

/, abaa /,

),,( contentstackcurrentinputremainingstatecurrentA computation example

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

43

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

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

$),,($),,(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

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

/, abaa /,

),,( contentstackcurrentinputremainingstatecurrentA computation example

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

44

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

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

$),,($),,(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

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

/, abaa /,

),,( contentstackcurrentinputremainingstatecurrentA computation example

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

45

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

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

$),,($),,(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

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

/, abaa /,

),,( contentstackcurrentinputremainingstatecurrentA computation example

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

46

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

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

$),,($),,(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

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

/, abaa /,

),,( contentstackcurrentinputremainingstatecurrentA computation example

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

47

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

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

$),,($),,(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

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

/, abaa /,

),,( contentstackcurrentinputremainingstatecurrentA computation example

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

48

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

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

$),,($),,(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

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

/, abaa /,

),,( contentstackcurrentinputremainingstatecurrentA computation example

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

49

For convenience we write

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

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

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

$),,($),,(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

),,( contentstackcurrentinputremainingstatecurrent

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

50

Formal Definition

Language of NPDA M

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

Initial state Final state

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

51

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

q0 q1 q2 q3

NPDA M

)(MLaaabbb

/, /, ab $/$,

/, abaa /,

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

52

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

)(MLba nn

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

/, abaa /,NPDA M

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

53

}0:{)( nbaML nnTherefore:

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

/, abaa /,

NPDA M

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

54

NPDAs Accept Context-Free Languages

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

55

Context-Free

Languages

(Grammars)

Languages

Accepted by

NPDAs

Theorem

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

56

Context-Free

Languages

(Grammars)

Languages

Accepted by

NPDAs

Proof - Step 1:

Convert any context-free grammar G to a NPDA M

with L(G) = L(M)

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

57

Context-Free

Languages

(Grammars)

Languages

Accepted by

NPDAs

Proof - Step 2:

Convert any NPDA M to a context-free grammar G with L(M) = L(G)

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

58

Converting Context-Free Grammars

to NPDAs

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

59

An example grammar:

T

TaT

bS

aSTbS

What is the equivalent NPDA?

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

60

Grammar NPDA

/,

/,

/,

/,

T

TaT

bS

aSTbS

T

TaT

bS

aSTbS

/,

/,

bb

aa

q0 q1 2qS/, $/$,

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

61

The NPDA simulates

leftmost derivations of the grammar

L(Grammar) = L(NPDA)

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

62

Grammar:

T

TaT

bS

aSTbS

A leftmost derivation:

abababTabbTabTbSaS

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

63

NPDA execution:

0q q1 2qS/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

Input

Stack

$

a ab

Time 0

b

Start

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

64

q0 q1 2q

Input

Stack

a ab

Time 1

b

$S

S/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

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

65

q0 2q

Input

Stack

a ab

Time 2

b

$

a

b

ST

q1S/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

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

66

q0 2q

Input

Stack

a ab

Time 3

b

$

a

b

ST

q1S/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

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

67

q0 2q

Input

Stack

a ab

Time 4

b

$bTb

q1S/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

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

68

q0 2q

Input

Stack

a ab

Time 5

b

$bTb

q1S/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

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

69

q0 2qS, , $ $

bb

aa

,

,

Input

Stack

a ab

Time 6

b

T

TaT

bS

aSTbS

,

,

,

, $b

Ta

q1

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

70

q0 2q

Input

Stack

a ab

Time 7

b

$b

Ta

q1S/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

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

71

q0 2q

Input

Stack

a ab

Time 8

b

$ba

q1S/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

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

72

q0 2q

Input

Stack

a ab

Time 9

b

$b

q1S/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

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

73

q0 q1 2q

Input

Stack

$

a ab

Time 10

b

acceptS/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

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

74

In general

Given any grammar Gwe can construct a NPDA M with

)()( MLGL

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

75

q0 q1 2qS/, $/$,

wA /, /, aa

For any productionwA

For any terminala

Constructing NPDA M from grammar GTop-down parser

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

76

Grammar G generates string w

if and only if

NPDA M accepts w

)()( MLGL

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

77

For any context-free language

there is an NPDA

that accepts the same language

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

78

Which means

Languages

Accepted by

NPDAs

Context-

FreeLanguages(Grammars)

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

79

Converting NPDAsto

Context-Free Grammars

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

80

For any NPDA M

we will construct

a context-free grammar G with

)()( GLML

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

81

in NPDA M

abcABCabcS

Input processed Stack contents

terminals variables

GA derivation in Grammar

The grammar simulates the machine

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

82

Some Simplifications

First we modify the NPDA so that

• It has a single final state qf and

• It empties the stack

when it accepts the input

Original NPDA Empty Stack

fq

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

83

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 84: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 6 Mälardalen University

84

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 85: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 6 Mälardalen University

85

/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 86: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 6 Mälardalen University

86

The Grammar Construction

)( jiBqq

In grammar G

Terminals:

Input symbols of NPDA

states

Stack symbol

Variables:

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

87

iq jq/, BaFor each transition:

we add production: aBqq ji )(

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

88

For each transition:

we add production: ))(()( klljki DqqCqqaBqq

iq jqCDBa /,

for all states qk , ql

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

89

Start Variable )$( fo qq

Stack bottom symbol

Start state (Single) Final state

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

90

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 91: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 6 Mälardalen University

91

)$)(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 92: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 6 Mälardalen University

92

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 93: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 6 Mälardalen University

93

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 94: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 6 Mälardalen University

94

)$)(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 95: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 6 Mälardalen University

95

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 96: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 6 Mälardalen University

96

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 97: CDT314 FABER Formal Languages, Automata  and Models of Computation Lecture 6 Mälardalen University

97

In general, in grammar:

wqq f

)$( 0

if and only if

w is accepted by the NPDA

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

98

Explanation

By construction of Grammar:

wAqq ji

)(

if and only if

in the NPDA going from qi to qj

the stack doesn’t change below

and A is removed from stack

iq jq/, Ba

aBqq ji )(

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

99

Context-Free

Languages

(Grammars)

Languages

Accepted by

NPDAs

We have shown the procedure to convert

any NPDA M to a context-free

grammar G with L(M) = L(G)which means

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

100

Context-Free

Languages

(Grammars)

Languages

Accepted by

NPDAs

Therefore

END OF PROOF

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

101

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