cdt314 faber formal languages, automata and models of computation lecture 6 mälardalen university

Post on 19-Jan-2016

18 Views

Category:

Documents

3 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

1

CDT314

FABER

Formal Languages, Automata and Models of Computation

Lecture 6

Mälardalen University

2010

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

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

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

5

Regular Languages

}{ nnba }{ Rww

Context-Free Languages

Non-regular languages

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

6

Context-Free Languages

7

Context-Free Languages

Pushdown

Automata

Context-Free

Grammars

stack

automaton

(CF grammars are

defined as generalized Regular Grammars)

8

Definition: Context-Free Grammars

Grammar

Productions of the form:xA

x is string of variables and terminals

),,,( PSTVG

Variables Terminal

symbols

Start

variables

9

Pushdown AutomataPDAs

10

Pushdown Automaton - PDA

Input String

Stack

States

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

12

The States

Input

symbol

Pop old- reading

stack symbol

Push new- writing

stack symbol

q1 q2cba /,

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

14

a a

input

Pushb

eh

$eh

$

bc

top

stack

q1 q2ca /,

15

a a input

Popb

eh

$eh

$

top

stack

q1 q2/,ba

16

a a input

No Changeb

eh

$eh

$

btop

stack

q1 q2 /,a

17

NPDAsNon-deterministic Push-Down Automata

18

Non-Determinism

q1

q2cba /,

q3

q1 q2

transitioncba /,

cb /,

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)

20

}0:{ nbaL nn

is the language accepted by the NPDA:

Example NPDA

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

/, abaa /,

},{ ba

symbol)stack initial :($

21

/,

/,

bb

aa

q1 q2 /,q0

NPDA M

}{)( RwwML

bb

aa

/,

/,

$/$,

(Even-length palindromes)

},{ ba

Example NPDA

22

Pushing Strings

q1 q2wba /,

Input

symbol

Pop

symbolPush

string

23

q1 q2cdfba /,

a

b top

input

stack

a

Push

eh h

e

cdf

pushed

string

Example

$ $

24

Another NPDA example

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

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

$/$,q1 q2

NPDA M

}:{)( ba nnwML

},{ ba

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

$/$,

26

Input

a ab b ba

Stack

$0

q1 q2

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

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

$/$,

Time 1

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

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

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

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

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

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

33

Formal Definitions for NPDAs

34

q1 q2wba /,

)},{(),,( 21 wqbaq

Transition function

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.

36

Formal Definition

Non-Deterministic Pushdown Automaton NPDA

),,,,,( FzQM States

Input

alphabetStack

alphabet

Transition

function

Final

states

Stack

start

symbol

37

Instantaneous Description

),,( suq

Current

state Remaining

input

Current

stack

contents

38q0 q1 q2 q3

Input

Stack

a a a b b b$

aaTime 4:

Example Instantaneous Description

$),,( 1 aaabbbq

a

/, /, ab $/$,

/, abaa /,

),,( stackinputremainingstateurrentc

39q0 q1 q2 q3

Input

Stack

a a a b b b$

aaTime 5:

Example Instantaneous Description

$),,( 2 aabbq

a

/, /, ab $/$,

/, abaa /,

),,( stackinputremainingstateurrentc

40

We write

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

),,( contentstackcurrentinputremainingstatecurrent

41

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

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

$),,($),,(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

A computation example

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

/, abaa /,

),,( contentstackcurrentinputremainingstatecurrent

42

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

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

$),,($),,(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

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

/, abaa /,

),,( contentstackcurrentinputremainingstatecurrentA computation example

43

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

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

$),,($),,(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

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

/, abaa /,

),,( contentstackcurrentinputremainingstatecurrentA computation example

44

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

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

$),,($),,(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

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

/, abaa /,

),,( contentstackcurrentinputremainingstatecurrentA computation example

45

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

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

$),,($),,(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

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

/, abaa /,

),,( contentstackcurrentinputremainingstatecurrentA computation example

46

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

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

$),,($),,(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

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

/, abaa /,

),,( contentstackcurrentinputremainingstatecurrentA computation example

47

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

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

$),,($),,(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

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

/, abaa /,

),,( contentstackcurrentinputremainingstatecurrentA computation example

48

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

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

$),,($),,(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

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

/, abaa /,

),,( contentstackcurrentinputremainingstatecurrentA computation example

49

For convenience we write

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

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

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

$),,($),,(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

),,( contentstackcurrentinputremainingstatecurrent

50

Formal Definition

Language of NPDA M

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

Initial state Final state

51

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

q0 q1 q2 q3

NPDA M

)(MLaaabbb

/, /, ab $/$,

/, abaa /,

52

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

)(MLba nn

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

/, abaa /,NPDA M

53

}0:{)( nbaML nnTherefore:

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

/, abaa /,

NPDA M

54

NPDAs Accept Context-Free Languages

55

Context-Free

Languages

(Grammars)

Languages

Accepted by

NPDAs

Theorem

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)

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)

58

Converting Context-Free Grammars

to NPDAs

59

An example grammar:

T

TaT

bS

aSTbS

What is the equivalent NPDA?

60

Grammar NPDA

/,

/,

/,

/,

T

TaT

bS

aSTbS

T

TaT

bS

aSTbS

/,

/,

bb

aa

q0 q1 2qS/, $/$,

61

The NPDA simulates

leftmost derivations of the grammar

L(Grammar) = L(NPDA)

62

Grammar:

T

TaT

bS

aSTbS

A leftmost derivation:

abababTabbTabTbSaS

63

NPDA execution:

0q q1 2qS/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

Input

Stack

$

a ab

Time 0

b

Start

64

q0 q1 2q

Input

Stack

a ab

Time 1

b

$S

S/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

65

q0 2q

Input

Stack

a ab

Time 2

b

$

a

b

ST

q1S/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

66

q0 2q

Input

Stack

a ab

Time 3

b

$

a

b

ST

q1S/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

67

q0 2q

Input

Stack

a ab

Time 4

b

$bTb

q1S/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

68

q0 2q

Input

Stack

a ab

Time 5

b

$bTb

q1S/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

69

q0 2qS, , $ $

bb

aa

,

,

Input

Stack

a ab

Time 6

b

T

TaT

bS

aSTbS

,

,

,

, $b

Ta

q1

70

q0 2q

Input

Stack

a ab

Time 7

b

$b

Ta

q1S/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

71

q0 2q

Input

Stack

a ab

Time 8

b

$ba

q1S/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

72

q0 2q

Input

Stack

a ab

Time 9

b

$b

q1S/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

73

q0 q1 2q

Input

Stack

$

a ab

Time 10

b

acceptS/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

74

In general

Given any grammar Gwe can construct a NPDA M with

)()( MLGL

75

q0 q1 2qS/, $/$,

wA /, /, aa

For any productionwA

For any terminala

Constructing NPDA M from grammar GTop-down parser

76

Grammar G generates string w

if and only if

NPDA M accepts w

)()( MLGL

77

For any context-free language

there is an NPDA

that accepts the same language

78

Which means

Languages

Accepted by

NPDAs

Context-

FreeLanguages(Grammars)

79

Converting NPDAsto

Context-Free Grammars

80

For any NPDA M

we will construct

a context-free grammar G with

)()( GLML

81

in NPDA M

abcABCabcS

Input processed Stack contents

terminals variables

GA derivation in Grammar

The grammar simulates the machine

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

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.

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.

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

86

The Grammar Construction

)( jiBqq

In grammar G

Terminals:

Input symbols of NPDA

states

Stack symbol

Variables:

87

iq jq/, BaFor each transition:

we add production: aBqq ji )(

88

For each transition:

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

iq jqCDBa /,

for all states qk , ql

89

Start Variable )$( fo qq

Stack bottom symbol

Start state (Single) Final state

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

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 ,

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

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 ,

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

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

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

97

In general, in grammar:

wqq f

)$( 0

if and only if

w is accepted by the NPDA

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

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

100

Context-Free

Languages

(Grammars)

Languages

Accepted by

NPDAs

Therefore

END OF PROOF

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

top related