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

Preview:

DESCRIPTION

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

Citation preview

1

CD5560

FABER

Formal Languages, Automata and Models of Computation

Lecture 8

Mälardalen University

2010

2

Content

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

3

Regular Languages

}{ nnba }{ Rww

Context-Free Languages

Non-regular languages

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

4

Context-Free Languages

5

Context-Free Languages

Pushdown

Automata

Context-Free

Grammars

stack

automaton

(CF grammars are

defined as generalized Regular Grammars)

6

Definition: Context-Free Grammars

Grammar

Productions of the form:xA

x is string of variables and terminals

),,,( PSTVG

Variables Terminal

symbols

Start

variables

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

8

Pushdown AutomataPDAs

9

Pushdown Automaton - PDA

Input String

Stack

States

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

zxy

POP reading symbol PUSH writing symbol

11

The States

Input

symbol

Pop old- reading

stack symbol

Push new- writing

stack symbol

q1 q2cba /,

12

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

13

a a

input

Pushb

eh

$eh

$

bc

top

stack

q1 q2ca /,

14

a a input

Popb

eh

$eh

$

top

stack

q1 q2/,ba

15

a a input

No Changeb

eh

$eh

$

btop

stack

q1 q2 /,a

16

Input Stack

( )

Example 3.7 Salling: A PDA for simple nested parenthesis strings

sstart q(/),

( ( ) )

end

(/,( /(,)

Time 0

17

Input

Stack

( )

Example 3.7

sstart q(/),

(/,(

( ( ) )

/(,)

(

end

Time 1

18

Input

Stack

( )

sstart q(/), end

( ( ) ) (

(

Example 3.7

(/,( /(,)

Time 2

19

Input

( )

sstart q(/),

( ( ) )

Stack

(

((

end

Example 3.7

(/,( /(,)

Time 3

20

Input

( )

sstart q(/),

( ( ) )

Stack

(

((

end

Example 3.7

(/,( /(,)

Time 4

21

Input

( )

sstart (/),

( ( ) )

Stack

(

(

q

Example 3.7

end

(/,( /(,)

Time 5

22

Input

Stack

( )

sstart q(/),

( ( ) )(

end

Example 3.7

(/,( /(,)

Time 6

23

Input

( )

sstart q(/),

( ( ) )

Example 3.7

end(/),

(/,( /(,)

Time 7

Stack

24

NPDAsNon-deterministic Push-Down Automata

25

Non-Determinism

q1

q2cba /,

q3

q1 q2

transitioncba /,

cb /,

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)

27

}0:{ nbaL nn

is the language accepted by the NPDA:

Example NPDA

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

/, abaa /,

},{ ba

28

/,

/,

bb

aa

q1 q2 /,q0

NPDA M

}{)( RwwML

bb

aa

/,

/,

$/$,

(Even-length palindromes)

},{ ba

Example NPDA

29

Pushing Strings

q1 q2wba /,

Input

symbol

Pop

symbolPush

string

30

q1 q2cdfba /,

a

b top

input

stack

a

Push

eh h

e

cdf

pushed

string

Example

$ $

31

Another NPDA example

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

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

$/$,q1 q2

NPDA M

}:{)( ba nnwML

},{ ba

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

$/$,

33

Input

a ab b ba

Stack

$0

q1 q2

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

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

$/$,

Time 1

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

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

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

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

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

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

40

Formal Definitions for NPDAs

41

q1 q2wba /,

)},{(),,( 21 wqbaq

Transition function

42

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.

43

Formal Definition

Non-Deterministic Pushdown Automaton NPDA

),,,,,( FzQM States

Input

alphabetStack

alphabet

Transition

function

Final

states

Stack

start

symbol

44

Instantaneous Description

),,( suq

Current

state Remaining

input

Current

stack

contents

45q0 q1 q2 q3

Input

Stack

a a a b b b$

aaTime 4:

Example Instantaneous Description

$),,( 1 aaabbbq

a

/, /, ab $/$,

/, abaa /,

),,( stackinputremainingstateurrentc

46q0 q1 q2 q3

Input

Stack

a a a b b b$

aaTime 5:

Example Instantaneous Description

$),,( 2 aabbq

a

/, /, ab $/$,

/, abaa /,

),,( stackinputremainingstateurrentc

47

We write

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

),,( contentstackcurrentinputremainingstatecurrent

48

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

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

$),,($),,(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

A computation example

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

/, abaa /,

),,( contentstackcurrentinputremainingstatecurrent

49

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

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

$),,($),,(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

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

/, abaa /,

),,( contentstackcurrentinputremainingstatecurrentA computation example

50

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

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

$),,($),,(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

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

/, abaa /,

),,( contentstackcurrentinputremainingstatecurrentA computation example

51

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

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

$),,($),,(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

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

/, abaa /,

),,( contentstackcurrentinputremainingstatecurrentA computation example

52

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

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

$),,($),,(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

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

/, abaa /,

),,( contentstackcurrentinputremainingstatecurrentA computation example

53

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

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

$),,($),,(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

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

/, abaa /,

),,( contentstackcurrentinputremainingstatecurrentA computation example

54

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

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

$),,($),,(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

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

/, abaa /,

),,( contentstackcurrentinputremainingstatecurrentA computation example

55

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

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

$),,($),,(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

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

/, abaa /,

),,( contentstackcurrentinputremainingstatecurrentA computation example

56

For convenience we write

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

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

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

$),,($),,(

3222

111

10

qqabqaabbq

aaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

),,( contentstackcurrentinputremainingstatecurrent

57

Formal Definition

Language of NPDA M

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

Initial state Final state

58

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

q0 q1 q2 q3

NPDA M

)(MLaaabbb

/, /, ab $/$,

/, abaa /,

59

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

)(MLba nn

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

/, abaa /,NPDA M

60

}0:{)( nbaML nnTherefore:

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

/, abaa /,

NPDA M

61

NPDAs Accept Context-Free Languages

62

Context-Free

Languages

(Grammars)

Languages

Accepted by

NPDAs

Theorem

63

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)

64

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)

65

Converting Context-Free Grammars

to NPDAs

66

An example grammar:

T

TaT

bS

aSTbS

What is the equivalent NPDA?

67

Grammar NPDA

/,

/,

/,

/,

T

TaT

bS

aSTbS

T

TaT

bS

aSTbS

/,

/,

bb

aa

q0 q1 2qS/, $/$,

68

The NPDA simulates

leftmost derivations of the grammar

L(Grammar) = L(NPDA)

69

Grammar:

T

TaT

bS

aSTbS

A leftmost derivation:

abababTabbTabTbSaS

70

NPDA execution:

0q q1 2qS/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

Input

Stack

$

a ab

Time 0

b

Start

71

q0 q1 2q

Input

Stack

a ab

Time 1

b

$S

S/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

72

q0 2q

Input

Stack

a ab

Time 2

b

$

a

b

ST

q1S/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

73

q0 2q

Input

Stack

a ab

Time 3

b

$

a

b

ST

q1S/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

74

q0 2q

Input

Stack

a ab

Time 4

b

$bTb

q1S/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

75

q0 2q

Input

Stack

a ab

Time 5

b

$bTb

q1S/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

76

q0 2qS, , $ $

bb

aa

,

,

Input

Stack

a ab

Time 6

b

T

TaT

bS

aSTbS

,

,

,

, $b

Ta

q1

77

q0 2q

Input

Stack

a ab

Time 7

b

$b

Ta

q1S/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

78

q0 2q

Input

Stack

a ab

Time 8

b

$ba

q1S/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

79

q0 2q

Input

Stack

a ab

Time 9

b

$b

q1S/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

80

q0 q1 2q

Input

Stack

$

a ab

Time 10

b

acceptS/, $/$,

/,

/,

/,

/,

T

TaT

bS

aSTbS

/,

/,

bb

aa

81

In general

Given any grammar Gwe can construct a NPDA M with

)()( MLGL

82

q0 q1 2qS/, $/$,

wA /, /, aa

For any productionwA

For any terminala

Constructing NPDA M from grammar GTop-down parser

83

Grammar G generates string w

if and only if

NPDA M accepts w

)()( MLGL

84

For any context-free language

there is an NPDA

that accepts the same language

85

Context-Free

Languages

(Grammars)

Languages

Accepted by

NPDAs

Which means

86

Converting NPDAsto

Context-Free Grammars

87

For any NPDA M

we will construct

a context-free grammar G with

)()( GLML

88

in NPDA M

abcABCabcS

Input processed Stack contents

terminals variables

GA derivation in Grammar

The grammar simulates the machine

89

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

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.

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.

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

93

The Grammar Construction

)( jiBqq

In grammar G

Terminals:

Input symbols of NPDA

states

Stack symbol

Variables:

94

iq jq/, BaFor each transition:

we add production: aBqq ji )(

95

For each transition:

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

iq jqCDBa /,

for all states qk , ql

96

Start Variable )$( fo qq

Stack bottom symbol

Start state (Single) Final state

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

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 ,

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

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 ,

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

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

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

104

In general, in grammar:

wqq f

)$( 0

if and only if

w is accepted by the NPDA

105

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

106

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

107

Context-Free

Languages

(Grammars)

Languages

Accepted by

NPDAs

Therefore

END OF PROOF

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

Recommended