unit 6 - powerpoint ·  · 2010-12-14–remembering a's by pushing it to the stack. ......

34
1 Unit 8 Pushdown Automata Reading: Sipser, chapter 2.2

Upload: duongthien

Post on 21-May-2018

217 views

Category:

Documents


1 download

TRANSCRIPT

1

Unit 8

Pushdown Automata

Reading: Sipser, chapter 2.2

2

Finite Automata

• A finite automaton has a huge limitation.

• It can count only by changing states.

• So an FA can distinguish between at most

|Q| states.

• This limitation bounds the class of languages

that an NFA can recognize to a rather small

category - that of regular languages.

3

Turing Machine

• Infinite memory allow us to recognize a larger

class of languages.

• If there are no restrictions on the use of this

memory, i.e. the automaton can read and

write in any location in the memory, then

the result is the strongest possible model - a

Turing Machine.

• A Turing machine (TM) can perform any

computation that the most powerful computer

can (now and in the future).

4

The Stack Model

• We add to an NFA an unlimited memory but

we limit the way it is used.

• Our limitation - the memory will be a stack.

• We can read/write only at the top of the stack.

• We still have a finite number of states that can

be used as before.

• we get a machine which is stronger than NFA

but less powerful than TM.

5

Pushdown Automata

• Informally a pushdown automata (PDA) is an

NFA + Stack

• To remember or to count we can write to the

stack and can read/pop from it afterwards when

we need the information.

state control

a a b c a a

xyz

6

Example:

• The following language can be recognized by

PDA but is not regular

L = {anbn | n>0}

• The basic idea of recognition:

– remembering a's by pushing it to the stack.

– comparing the number of b's to the number

of a's by popping one a for each b.

7

Example (cont.)

• If the stack is emptied before the end of input

– the word includes more b's than a's.

• If the stack is not empty at the end of input –

the word includes more a's than b's.

8

Transition function of a PDA

• In NFA a transition function is determined

according to:

1. A current state

2. An input letter

• In PDA we move according to

1. A current state

2. An input letter

3. A letter on top of the stack

• The transition function output is a state and a

letter to be written into the stack.

9

Formal Definition of PDA• A pushdown automaton is a 6-tuple:

• Q - is a finite set of states.

• - is the input alphabet.

• - is the stack alphabet.

• : (Q ) P(Q ) !!!

• q0Q - the start state.

• F Q - a set of accept states.

(Q, , , , q0, F)

10

The Transition Function

The transition function (q,,x) = (q',y) reads:

When

– q is the current state

– is the input symbol

– x is the topmost stack symbol

you may

– move to state q’

– push y to the stack

• The diagram for the PDA is:

z

x

z

y

q q’,xy

11

Possible transition functions

• (q,,x) = (q',y)

– move without reading a symbol from the input

• (q,,) = (q',y)

– move without reading a symbol from the stack

• (q, ,x) = (q',)

– move without pushing a symbol to the stack

• (q, , ) = (q,y)

– push y

• (q, , x) = (q,)

– pop x

q q’,xy

12

Example:

• Change the stack:

(q,,x) = (q',y)

q q’,xy

z

x

z

y

13

Example:

• Popping the stack:

(q,,x) = (q', )

q q’,x

z

x

z

14

Example:

• Pushing the stack:

(q,,) = (q', y)

q q’,y

z

x

z

x

y

15

Example:

• No change:

(q,,x) = (q', x)

(q,,) = (q', )

q q’,xx

z

x

z

x

,

16

Example of a PDA

• A state diagram for a PDA that recognizes

{ambn | m=n0}

• The PDA is (Q,,,,q1,F) where

q1 q2

,$

q4 q3,$

b,A

a,A

b,A

• Q={q1,q2,q3,q4}

• ={a,b}

• ={A,$}

• in the diagram

• q1 the start state

• F={q1,q4}

17

• The symbol $ signifies the stack emptiness.

• An input word is accepted if the input is

finished on an accepting state.

• The input is rejected - If the stack becomes

empty while b’s remain (m<n) or if the b’s are

finished while the stack is not empty (m>n).

Example of a PDA

q1 q2

,$

q4 q3,$

b,A

a,A

b,A

18

Example of a PDA• The transition function can be described using a

transition table:

q1 q2

,$

q4 q3,$

b,A

a,A

b,A

input:stack:

19

Another Example

• A state diagram for a PDA that recognizes

{ambn | n>m 0}

• The PDA is (Q,,,,q1,F) where

q1 q2

,$

q4 q3b,$

b,A

a,A

b,A

• Q={q1,q2,q3,q4}

• ={a,b}

• ={A,$}

• in the diagram

• q1 the start state

• F={q4}

b,$

b,

20

Properties of PDA

• With this definition of a PDA there is no explicit

mechanism to test whether the stack is empty.

• We solve the problem by initially placing a

special symbol ($) on the stack.

• The PDA cannot explicitly test for the end of an

input.

• However, the accept state takes effect only

when the machine is at the end of the input.

21

Computation of a PDA

• A PDA accepts input w if w can be written as

w=w1w2…wm, wi and there exist a sequence

of states r0,r1,…rm, riQ, and a sequence of

strings s0,s1,…,sm , si such that:

1. r0=q0 and s0= .

2. for i=0,...,m-1 (ri+1,b)(ri,wi+1,a) where

si=a t and si+1=b t , where a,b and t

3. rmF

t

a

t

b

si si+1

ri ri+1

wi+1,ab

Another Example of PDA

• This PDA recognizes {aibjck | i=j or i=k}

22

q1

q2

,$

q4q3

,

,$

a,A

b,A c,

q5 q6 q7,

b, c,A

, ,$

Non Determinism in PDA

• The formal definition of a PDA allows non-

determinism.

• A PDA is called non-deterministic if there

exist at least two possible transitions from a

particular situation (current state q, input

symbol , and stack symbol x).

– Note, that these transitions are not necessarily

defined by (q,,x)

23

Non Determinism in PDA

• A PDA is called deterministic iff it holds the

following three conditions:

– for each qQ, a and x, |(q,a,x)| 1

– for each qQ and x, if (q,,x) then for each

, (q, , x)=

– for each qQ and , if (q,,) then for

each x, (q, ,x)=

Unlike FA deterministic and nondeterministic

PDA are not equivalent in power.24

Example of Deterministic PDA

• This PDA recognizes {wcwR | w{0,1}* }

25

q0

,$q1

c,

0,0

q3

1,1

q2

0,01,1

,$

Example of Nondeterministic PDA

• This PDA recognizes {wwR | w{0,1}* }

• A nondeterminism is a must here.

26

q0

,$q1

,

0,0

q3

1,1

q2

0,01,1

,$

27

Example of a PDA

• Construct a PDA that recognizes:

L = {aibjci+j | i,j0}

q0

,$q1

,

a,Z

q4 q2 b,Zq3

c,Z

,$ ,

28

Example of a PDA

• Construct a PDA that recognizes:

L = {aibj | i,j0}

Conclusion: every FA is automatically a PDA whose stack is ignored.

q0

a,

q2

b,

b,

29

Example of a PDA

• Construct a PDA that recognizes:

L = {aibj | i>0 and j=i+3}

q0

,$q1

q7 q5,

q2

,Aq3 q4

,A ,A

a,A

a,A

q6

b,A

,$

Shorthand Notation for PDA• Sometimes it is convenient to allow the PDA

to write on the stack an entire string u* in one machine step.

• We use the notation:

(r,u)(q,a,x) where u*

to denote that when

– the current state is qQ

– the next input symbol is a

– the next stack symbol is x

the PDA may move to state r and push the string u* into the stack.

qa,xu

r

Shorthand Notation for PDA

• If u=u1u2…uk, ui the notation (r,u)(q,a,x) means

qa,xu1u2…uk

r

q rz

x

z

uk

u2

u1

Shorthand Notation for PDA

• We can implement the action (r,u) (q,a,x) in the standard PDA by introducing new states q1,…,qk-1 and setting the transition functions:

– (q,a,x) = {(q1,uk)}{ what we have so far}

– (q1,,)={(q2,uk-1)}

– (q2,,)={(q3,uk-2)}

– (qk-1,,)={(r,u1)}

qa,xxyz

r

qq1

a,xz

q2

,y,x r

33

Example of a Shorthand PDA

• Construct a PDA that recognizes:

L = {aibj | i>0 and j=i+3}

q0

,AAA$q1

q7 q5,

a,A

a,A

q6

b,A

,$

The Power of PDA

• What is the power of a PDA? Can it recognize all context-free languages? Is it stronger?

• Proof:

– We show that if L is CFL then a PDA recognizes it.

– We show that is a PDA recognizes L then L is CFL.

Theorem: A language is context-free iff

some pushdown automaton recognizes it.