Intro to Theory of Computation

profileTrist1111
PDAs.pptx

1

Pushdown Automata PDAs

2

Pushdown Automaton -- PDA

Input String

Stack

States

3

Initial Stack Symbol

Stack

Stack

bottom

special symbol

stack

head

top

4

The States

Input

symbol

Pop

symbol

Push

symbol

Or equivalently

5

top

input

stack

Replace

6

Push

top

input

stack

7

Pop

top

input

stack

8

No Change

top

input

stack

9

Pop

top

input

stack

A Possible Transition

empty

10

input

A Bad Transition

The automaton Halts in state

and Rejects the input string

Empty stack

HALT

11

input

A Bad Transition

The automaton Halts in state

and Rejects the input string

Empty stack

HALT

12

No transition is allowed to be followed

When the stack is empty

Empty stack

13

Pop

top

input

stack

A Good Transition

14

Non-Determinism

These are allowed transitions in a

Non-deterministic PDA (NPDA)

15

NPDA: Non-Deterministic PDA

Example:

16

Execution Example:

Input

current

state

Time 0

Stack

17

Input

Time 1

Stack

18

Input

Stack

Time 2

19

Input

Stack

Time 3

20

Input

Stack

Time 4

21

Input

Stack

Time 5

22

Input

Stack

Time 6

23

Input

Stack

Time 7

24

Input

Time 8

accept

Stack

25

A string is accepted if there is

a computation such that:

All the input is consumed

AND

The last state is a final state

At the end of the computation,

we do not care about the stack contents

26

The input string

is accepted by the NPDA:

27

is the language accepted by the NPDA:

In general,

28

Another NPDA example

NPDA

29

Execution Example:

Input

Time 0

Stack

30

Input

Time 1

Stack

31

Input

Time 2

Stack

32

Input

Time 3

Stack

Guess the middle

of string

33

Input

Time 4

Stack

34

Input

Time 5

Stack

35

Input

Time 6

Stack

accept

36

Rejection Example:

Input

Time 0

Stack

37

Input

Time 1

Stack

38

Input

Time 2

Stack

39

Input

Time 3

Stack

Guess the middle

of string

40

Input

Time 4

Stack

41

Input

Time 5

Stack

There is no possible transition.

Input is not consumed

42

Another computation on same string:

Input

Time 0

Stack

43

Input

Time 1

Stack

44

Input

Time 2

Stack

45

Input

Time 3

Stack

46

Input

Time 4

Stack

47

Input

Time 5

Stack

No final state

is reached

48

There is no computation

that accepts string

49

A string is rejected if there is

no computation such that:

All the input is consumed

AND

The last state is a final state

At the end of the computation,

we do not care about the stack contents

50

In other words, a string is rejected

if in every computation with this string:

The input cannot be consumed

OR

The input is consumed and the last state is not a final state

OR

The stack head moves below the bottom of the stack

51

Another NPDA example

NPDA

52

Execution Example:

Input

Time 0

Stack

53

Input

Time 1

Stack

54

Input

Time 2

Stack

55

Input

Time 3

Stack

accept

56

Rejection example:

Input

Time 0

Stack

57

Input

Time 1

Stack

58

Input

Time 2

Stack

59

Input

Time 3

Stack

60

Input

Time 4

Stack

Halt and Reject

61

Pushing Strings

Input

symbol

Pop

symbol

Push

string

62

top

input

stack

Push

pushed

string

Example:

63

Another NPDA example

NPDA

64

Time 0

Input

current

state

Stack

Execution Example:

65

Time 1

Input

Stack

66

Time 3

Input

Stack

67

Time 4

Input

Stack

68

Time 5

Input

Stack

69

Time 6

Input

Stack

70

Time 7

Input

Stack

71

Time 8

Input

Stack

accept

72

Formalities for NPDAs

73

Formal Definition

Non-Deterministic Pushdown Automaton

NPDA

States

Input

alphabet

Stack

alphabet

Transition

function

Final

states

Stack

start

symbol

Initial

state

74

75

Transition function:

76

Transition function:

77

Instantaneous Description

Current

state

Remaining

input

Current

stack

contents

78

Input

Stack

Time 4:

Example:

Instantaneous Description

79

Input

Stack

Time 5:

Example:

Instantaneous Description

80

We write:

Time 4

Time 5

81

A computation:

82

For convenience we write:

83

Formal Definition

Language of NPDA :

Initial state

Final state

84

Example:

NPDA :

85

NPDA :

86

NPDA :

Therefore:

87

NPDAs Accept Context-Free Languages

88

Context-Free

Languages

(Grammars)

Languages

Accepted by

NPDAs

Theorem:

89

Context-Free

Languages

(Grammars)

Languages

Accepted by

NPDAs

Proof - Step 1:

Convert any context-free grammar

to a NPDA with:

90

Context-Free

Languages

(Grammars)

Languages

Accepted by

NPDAs

Proof - Step 2:

Convert any NPDA to a context-free

grammar with:

91

Converting Context-Free Grammars to NPDAs

Proof - step 1

92

to an NPDA automaton

We will convert any context-free grammar

Such that:

Simulates leftmost derivations of

93

Input

processed

Stack

contents

Input

Stack

leftmost variable

Leftmost derivation

Simulation of derivation

. . .

...

...

...

...

...

...

...

94

Input

Stack

Leftmost derivation

Simulation of derivation

string of terminals

end of input is reached

. . .

...

. . .

95

An example grammar:

What is the equivalent NPDA?

96

Grammar:

NPDA:

97

Grammar:

A leftmost derivation:

98

Input

Stack

Time 0

Derivation:

99

Input

Stack

Time 0

Derivation:

100

Input

Stack

Time 1

Derivation:

101

Input

Stack

Time 2

Derivation:

102

Input

Stack

Time 3

Derivation:

103

Input

Stack

Time 4

Derivation:

104

Input

Stack

Time 5

Derivation:

105

Input

Stack

Time 6

Derivation:

106

Input

Stack

Time 7

Derivation:

107

Input

Stack

Time 8

Derivation:

108

Input

Stack

accept

Time 9

Derivation:

109

In general:

Given any grammar

We can construct a NPDA

With

110

Constructing NPDA from grammar :

For any production

For any terminal

111

Grammar generates string

if and only if

NPDA accepts

112

Therefore:

For any context-free language

there is a NPDA

that accepts the same language

Context-Free

Languages

(Grammars)

Languages

Accepted by

NPDAs

113

Converting NPDAs to Context-Free Grammars

Proof - step 2

114

Deterministic PDA: DPDA

Allowed transitions:

(deterministic choices)

115

Allowed transitions:

(deterministic choices)

116

Not allowed:

(non deterministic choices)

117

DPDA example

118

The language

is deterministic context-free

119

Definition:

A language is deterministic context-free

if there exists some DPDA that accepts it

120

Example of Non-DPDA (NPDA)

121

Not allowed in DPDAs

122

NPDAs Have More Power than DPDAs

123

Deterministic

Context-Free

Languages

(DPDA)

Context-Free

Languages

NPDAs

Since every DPDA is also a NPDA

It holds that:

124

We will actually show:

We will show that there exists

a context-free language which is not

accepted by any DPDA

Deterministic

Context-Free

Languages

(DPDA)

Context-Free

Languages

(NPDA)

125

The language is:

We will show:

is context-free

is not deterministic context-free

126

Language is context-free

Context-free grammar for :

127

is not deterministic context-free

Theorem:

The language

(there is no DPDA that accepts )

128

Proof:

Assume for contradiction that

is deterministic context free

Therefore:

there is a DPDA that accepts

129

DPDA with

accepts

accepts

130

DPDA with

Such a path exists because of the determinism

131

The language

is not context-free

(we will prove this at a later class using

pumping lemma for context-free languages)

Fact 1:

Regular languages

Context-free languages

132

The language

is not context-free

Fact 2:

(we can prove this using pumping lemma

for context-free languages)

133

We will construct a NPDA that accepts:

which is a contradiction!

134

Modify

Replace

with

135

The NPDA that accepts

Connect final states of

with final states of

136

Since is accepted by a NPDA

it is context-free

Contradiction!

(since is not context-free)

137

Therefore:

There is no DPDA that accepts

End of Proof

Not deterministic context free

$

z

q

1

q

2

c

b

a

®

,

c

b

a

,

,

c

b

a

®

,

a

b

e

h

$

c

a

,

l

®

c

a

,

b

®

l

a

,

l

®

l

b

l

®

$

,

a

c

b

a

®

,

c

a

®

l

,

z

y

x

®

,

b

a

®

$

,

b

a

,

b

®

c

q

3

l

,

b

®

c

transition

-

l

l

,

l

®

l

a

,

l

®

a

b

,

a

®

l

q

0

l

,

$

®

$

0

q

aaabbb

}

0

:

{

³

=

n

b

a

L

n

n

b

b

a

a

®

®

l

l

,

,

l

l

®

®

b

b

a

a

,

,

M

}

{

)

(

R

ww

M

L

=

1

q

abbb

)

(

M

L

abbb

Ï

l

l

®

®

a

b

a

a

,

,

}

1

:

{

)

(

-

³

=

m

n

b

a

M

L

m

n

b

l

l

l

®

®

®

$

,

,

,

b

a

b

a

a

$

b

a

,

b

®

w

d

f

a

,

b

®

cdf

L

c

$

$

,

®

l

}

:

{

)

(

b

a

n

n

w

M

L

=

=

a

,

$

®

0

$

a

,

0

®

00

a

,

1

®

l

b

,

$

®

1

$

b

,

1

®

11

b

,

0

®

l

0

0

1

)

,

,

,

ä

,

Ã

,

Ó

,

(

0

F

z

q

Q

M

=

)}

,

{(

)

,

,

(

2

1

w

q

b

a

q

=

d

w

b

a

®

,

w

b

a

®

,

)}

,

(

),

,

{(

)

,

,

(

3

2

1

w

q

w

q

b

a

q

=

d

)

,

,

(

s

u

q

$)

,

,

(

1

aaa

bbb

q

$)

,

,

(

2

aa

bb

q

$)

,

,

(

$)

,

,

(

2

1

aa

bb

q

aaa

bbb

q

f

,$)

,

(

,$)

,

(

$)

,

,

(

$)

,

,

(

$)

,

,

(

$)

,

,

(

$)

,

,

(

,$)

,

(

,$)

,

(

3

2

2

2

1

1

1

1

0

l

l

q

q

a

b

q

aa

bb

q

aaa

bbb

q

aa

abbb

q

a

aabbb

q

aaabbb

q

aaabbb

q

f

f

f

f

f

f

f

f

,$)

,

(

,$)

,

(

$)

,

,

(

$)

,

,

(

$)

,

,

(

$)

,

,

(

$)

,

,

(

,$)

,

(

,$)

,

(

3

2

2

2

1

1

1

1

0

l

l

q

q

a

b

q

aa

bb

q

aaa

bbb

q

aa

abbb

q

a

aabbb

q

aaabbb

q

aaabbb

q

f

f

f

f

f

f

f

f

,$)

,

(

,$)

,

(

3

0

l

q

aaabbb

q

*

f

M

)}

'

,

,

(

)

,

,

(

:

{

)

(

0

s

q

s

w

q

w

M

L

f

l

*

=

f

)

(

M

L

,$)

,

(

,$)

,

(

3

0

l

q

aaabbb

q

*

f

M

)

(

M

L

aaabbb

Î

,$)

,

(

,$)

,

(

3

0

l

q

b

a

q

n

n

*

f

)

(

M

L

b

a

n

n

Î

}

0

:

{

)

(

³

=

n

b

a

M

L

n

n

=

Í

G

M

)

(

)

(

M

L

G

L

=

Ê

)

(

)

(

M

L

G

L

=

M

G

L

L

L

L

L

Þ

Þ

Þ

m

k

V

V

V

S

2

1

2

1

s

s

s

2

V

m

V

:

G

:

M

M

L

1

s

2

s

k

s

1

+

k

s

$

1

V

n

S

s

s

s

L

L

L

L

2

1

Þ

Þ

L

L

n

s

l

®

®

®

®

T

Ta

T

b

S

aSTb

S

2

q

S

®

l

l

,

l

l

l

l

l

®

®

®

®

T

Ta

T

b

S

aSTb

S

,

,

,

,

l

®

®

®

®

T

Ta

T

b

S

aSTb

S

l

l

®

®

b

b

a

a

,

,

l

®

®

®

®

T

Ta

T

b

S

aSTb

S

abab

abTab

abTb

aSTb

S

Þ

Þ

Þ

Þ

S

S

T

aSTb

S

Þ

abTb

aSTb

S

Þ

Þ

abTab

abTb

aSTb

S

Þ

Þ

Þ

T

abab

abTab

abTb

aSTb

S

Þ

Þ

Þ

Þ

G

M

)

(

)

(

M

L

G

L

=

a

G

w

A

®

,

l

l

®

a

a

,

M

w

A

®

w

)

(

)

(

M

L

G

L

=

w

b

a

®

,

1

q

2

q

w

b

®

,

l

1

,

w

b

®

l

2

,

w

c

®

l

1

,

w

b

a

®

2

,

w

c

a

®

2

,

w

b

a

®

2

,

w

b

a

®

}

0

:

{

)

(

³

=

n

b

a

M

L

n

n

L

}

{

)

(

R

ww

M

L

=

Ì

Î

L

Ï

L

}

{

}

{

2

n

n

n

n

b

a

b

a

L

È

=

0

³

n

L

2

1

|

S

S

S

®

l

|

1

1

b

aS

S

®

l

|

2

2

bb

aS

S

®

}

{

n

n

b

a

}

{

2

n

n

b

a

}

{

}

{

2

n

n

n

n

b

a

b

a

È

}

{

}

{

2

n

n

n

n

b

a

b

a

L

È

=

L

M

n

n

b

a

n

b

n

n

b

a

2

n

n

b

a

}

{

}

{

)

(

2

n

n

n

n

b

a

b

a

M

L

È

=

}

{

n

n

n

c

b

a

*

*

b

a

n

n

b

a

}

{

n

n

n

c

b

a

L

È

})

{

}

{

(

2

n

n

n

n

b

a

b

a

L

È

=

}

{

n

n

n

c

b

a

L

È

M

¢

n

c

n

n

c

a

}

{

}

{

)

(

2

n

n

n

n

b

a

b

a

M

L

È

=

}

{

}

{

)

(

2

n

n

n

n

c

a

c

a

M

L

È

=

¢

b

c

n

n

b

a

n

b

n

c

M

n

n

c

a

l

M

¢

}

{

}

{

2

n

n

n

n

b

a

b

a

L

È

=