39
1 Pushdown Automata Definition: relation to -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

1 Pushdown Automata Definition: relation to -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

Embed Size (px)

Citation preview

Page 1: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

1

Pushdown Automata

Definition: relation to -NFAMoves of the PDA

Languages of the PDADeterministic PDA’s

Quiz Wednesday 11-19-14 on CFGs lecture #3

Page 2: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

2

Pushdown Automata

The PDA is an automaton equivalent to the CFG in language-defining power.

Only the nondeterministic PDA defines all the CFL’s.

The deterministic version models parsers. Most programming languages have

deterministic PDA’s.

Page 3: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

3

PDA’s related to -NFA’s

Think of an ε-NFA with the additional power that it can manipulate a stack.

Moves are determined by:1. The current state of the -NFA),2. The current input symbol (or ε), and 3. The current symbol on top of its

stack.

Page 4: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

4

PDA’s related to -NFA’s - 2

Being nondeterministic, the PDA can simultaneously explore several moves.

In each move, the PDA can:1. Change state and2. Replace the top symbol on the stack by

zero or more symbols. Zero symbols = “pop.” One symbol = “push”

Page 5: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

5

PDA Formalism

A PDA is described by:1. A finite set of states (Q, typically).2. An input alphabet (Σ, typically).3. A stack alphabet (Γ, typically).4. A transition function (δ, typically).5. A start state (q0, in Q, typically).

6. A start symbol (Z0, in Γ, typically).

7. A set of final states (F ⊆ Q, typically).

Page 6: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

6

Conventions on symbols

a, b, … are input symbols. ε (empty string) may be used at any

point in as a possibility (i.e. a spontaneous transition)

…, X, Y, Z are stack symbols. …, w, x, y, z are strings of input

symbols , ,… are strings of stack symbols.

Page 7: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

7

The Transition Function Takes three arguments:

1. q is a state in Q2. a (next input symbol) or ε.3. Z is top symbol of stack

δ(q, a, Z) is a set of zero or more actions of the form (p, ) where

p is a state and is a string of stack symbols

Page 8: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

8

Actions of the PDA If δ(q, a, Z) contains (p, ) among its

actions, then the PDA could:1. Change its state to p.2. Remove a from the front of the input3. Replace Z on the top of the stack by

Page 9: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

9

Sting acceptance by PDA Depends on status of input and stack

1. Typically all input consumed.2. Stack could be empty or in start state

Page 10: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

10

Example of PDA

Design a PDA to accept {0n1n | n > 1}.

The states: q = start state and where we are if we

have seen only 0’s in the input so far. p = state after we’ve seen at least one 1

and may proceed only if the inputs are 1’s.

f = accepting state.

Page 11: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

11

PDA to accept {0n1n | n > 1}

The stack symbols: Z0 = start symbol.

Whenever Z0 is top of stack, we have counted the same number of 1’s as 0’s (initially count is zero)

X = marker pushed onto stack every time 0 is the input (i.e. counts the number of 0’s)

Page 12: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

12

PDA to accept {0n1n | n > 1}

The transitions: (1) δ(q, 0, Z0) = {(q, XZ0)}. (2) δ(q, 0, X) = {(q, XX)}. These two

rules cause one X to be pushed onto the stack for each 0 read from the input.

(3) δ(q, 1, X) = {(p, ε)}. When we see a 1, go to state p and pop one X.

(4) δ(p, 1, X) = {(p, ε)}. Pop one X per 1. (5) δ(p, ε, Z0) = {(f, Z0)}. Accept

Page 13: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

13

Actions of the Example PDA

q

0 0 0 1 1 1

Z0

Page 14: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

14

Actions of the Example PDA

q

0 0 1 1 1

XZ0

Page 15: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

15

Actions of the Example PDA

q

0 1 1 1

XXZ0

Page 16: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

16

Actions of the Example PDA

q

1 1 1

XXXZ0

Page 17: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

17

Actions of the Example PDA

p

1 1

XXZ0

Page 18: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

18

Actions of the Example PDA

p

1

XZ0

Page 19: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

19

Actions of the Example PDA

p

Z0

Page 20: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

20

Actions of the Example PDA

f

Z0

Page 21: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

21

Instantaneous Descriptions

We can formalize the pictures just seen with an instantaneous description (ID).

A ID is a triple (q, w, ), where:1. q is the current state.2. w is the remaining input.3. is the stack contents, top at the

left.

Page 22: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

22

The “Goes-To” Relation

To say that ID i can become ID j in one move of the PDA, we write i⊦j.

Formally, (q, aw, X)⊦(p, w, ) for any w and , if δ(q, a, X) contains (p, ).

Extend ⊦ to ⊦*, meaning “zero or more moves,” by: Basis: i⊦*i. Induction: If i⊦*j and j⊦k, then i⊦*k.

Page 23: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

23

Example: Goes-To Using transitions defined on slide 12,

we decide if string 000111 is accept or not by a sequence of IDs.

Superscript denotes transition used (q, 000111, Z0)1⊦(q, 00111, XZ0)2⊦

(q, 0111, XXZ0)2⊦(q, 111, XXXZ0)3⊦ (p, 11, XXZ0)4⊦(p, 1, XZ0)4⊦(p, ε, Z0)5⊦ (f, ε, Z0)

Thus, (q, 000111, Z0)⊦*(f, ε, Z0). What would happen on input

0001111?

Page 24: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

24

Example of PDA again The transitions:

(1) δ(q, 0, Z0) = {(q, XZ0)}. (2) δ(q, 0, X) = {(q, XX)}. These two

rules cause one X to be pushed onto the stack for each 0 read from the input.

(3) δ(q, 1, X) = {(p, ε)}. When we see a 1, go to state p and pop one X.

(4) δ(p, 1, X) = {(p, ε)}. Pop one X per 1. (5) δ(p, ε, Z0) = {(f, Z0)}. Accept.

Page 25: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

25

Answer

(q, 0001111, Z0)1⊦(q, 001111, XZ0)2⊦ (q, 01111, XXZ0)2⊦(q, 1111, XXXZ0)3⊦ (p, 111, XXZ0)4⊦(p, 11, XZ0)4⊦(p, 1, Z0)5⊦ (f, 1, Z0)

Note the last ID has no move. 0001111 is not accepted, because the

input is not completely consumed.

Legal because a PDA can useε input even if input remains.

Dead endDead end; no alternative

Page 26: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

26

Review: PDA Formalism

A PDA is described by:1. A finite set of states (Q, typically).2. An input alphabet (Σ, typically).3. A stack alphabet (Γ, typically).4. A transition function (δ, typically).5. A start state (q0, in Q, typically).

6. A start symbol (Z0, in Γ, typically).

7. A set of final states (F ⊆ Q, typically).

Page 27: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

27

Ex 6.1.1 pp233-234P=({p,q},{0,1},{Z0,X},,q,Z0,{p, noIP, noSk})

The transitions: (1) δ(q, 0, Z0) = {(q, XZ0)} (2) δ(q, 0, X) = {(q, XX)} (3) δ(q, 1, X) = {(q, X)} (4) δ(q, , X) = {(p, ε)} (5) δ(p, , X) = {(p, ε)} (6) δ(p, 1, X) = {(p, XX)} (7) δ(p, 1, Z0) = {(p, ε)}

Apply to string 01

Why do both {} and () in definitions of the transition function?

Page 28: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

28

(q, 01, Z0)1⊦(q, 1, XZ0)3⊦(q, , XZ0)4⊦

(p, , Z0) dead end no way to empty stack

(q, 01, Z0)1⊦(q, 1, XZ0)⊦(p, , Z0)7⊦(p, , ) accepting

Ex 6.1.1a p234 input 01

Page 29: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

29

CptS 317 Fall 2014 Assignment 12, Due 12-5-14Exercise 6.2.5a text p 242

Number the moves in the transition function 1-11.Identify which move applies at each step of the trace by a superscript on the ID.

Quiz Friday, 12-5-14, PDA lectures 1 and 2

Page 30: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

30

Languages of a PDAs

A common way to define the language of a PDA is by final state.

If P is a PDA, then L(P) denotes the set of strings w such that (q0, w, Z0) ⊦* (f, ε, ) for final state f and any .

Acceptance determined by final state of P when all input consumed irrespective of stack content.

Page 31: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

31

Languages of PDAs A PDA with the same properties

could define a language by empty stack.

If P is a PDA, then N(P) denotes the set of strings w such that (q0, w, Z0) ⊦* (q, ε, ε) for any state q.

All input consumed, stack empty, and any state can be accepting

Page 32: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

32

Languages of PDAsThe class of all context-free languages contains both L(P) and N(p)A language L has a PDA that accepts its strings by final state iff it has another PDA that accepts its strings by empty stack.

Page 33: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

33

Restate the theorem1. If language L has a PDA=P that

accepts its strings by final state then it has a PDA=P’ that accepts its strings by empty stack

2. If language L has a PDA=P’ that accepts its strings by empty stack then it has a PDA=P” that accepts its strings by final state.

Page 34: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

34

If L(P) then N(P’): Basic idea

Construct P’ that “simulates” P’s acceptance of strings.

If P accepts, then P’ empties its stack. Since stack-state of P is irrelevant to its

acceptance of strings, could have the case were P empties its stack without accepting.

P’ most avoid simulating this behavior because strings rejected by P could be accepted by P’

Page 35: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

35

If L(P) then N(P’): Basic idea 2

To avoid accidentally emptying its stack, P’ uses a special bottom-marker to catch the case where P empties its stack without accepting.

Special bottom marker tells P’ to push P’s start symbol onto its stack and go to the start state of P

Page 36: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

36

If L(P) then N(P’): Proof

P’ has all the states, symbols, and moves of P, plus:

1. Stack symbol X0, used to guard the stack bottom against accidental emptying.

2. New start state s and “erase” state e.3. δ(s, ε, X0) = {(q0, Z0X0)}. Gets P’ started.

4. δ(f, ε, Y) = δ(e, ε, Y) = {(e, ε)} for any final state f of P and any stack symbol Y including X0 (erases stack).

Page 37: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

37

If N(P’) then L(P’’) Basic idea

P” simulates P’ acceptance of strings.

P” has a special bottom-marker to catch the situation where P’ has consumed input and emptied its stack.

If so, P” accepts by going to its final state.

Page 38: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

38

If N(P’) then L(P’’) proof

P’’ has all the states, symbols, and moves of P’, plus:

1. Stack symbol X0, used to guard the stack bottom.

2. New start state s and new final state f.3. δ(s, ε, X0) = {(q0, Z0X0)}. Gets P”

started.4. δ(q, ε, X0) = {(f, ε)} for any state q of

P’.

Page 39: 1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday 11-19-14 on CFGs lecture #3

39