You are on page 1of 33

Pushdown Automata

Definition Moves of the PDA Languages of the PDA Deterministic PDAs


1

Pushdown Automata
The PDA is an automaton equivalent to the CFG in language-defining power. Only the nondeterministic PDA defines all the CFLs. But the deterministic version models parsers.
Most programming languages have deterministic PDAs.
2

Intuition: PDA
Think of an -NFA with the additional power that it can manipulate a stack. Its moves are determined by:
1. The current state (of its NFA), 2. The current input symbol (or ), and 3. The current symbol on top of its stack.

Intuition: PDA (2)


Being nondeterministic, the PDA can have a choice of next moves. In each choice, the PDA can:
1. Change state, and also 2. Replace the top symbol on the stack by a sequence of zero or more symbols.
Zero symbols = pop. Many symbols = sequence of pushes.
4

PDA Formalism
A PDA is described by:
1. 2. 3. 4. A finite set of states (Q, typically). An input alphabet (, typically). A stack alphabet (, typically). 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).
5

Conventions
a, b, are input symbols.
But sometimes we allow as a possible value.

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

The Transition Function


Takes three arguments:
1. A state, in Q. 2. An input, which is either a symbol in or . 3. A stack symbol in .

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

p is a state; is a string of stack symbols.


7

Actions of the PDA


If (q, a, Z) contains (p, ) among its actions, then one thing the PDA can do in state q, with a at the front of the input, and Z on top of the stack is:
1. Change the state to p. 2. Remove a from the front of the input (but a may be ). 3. Replace Z on the top of the stack by .
8

Example: PDA
Design a PDA to accept {0n1n | n > 1}. The states:
q = start state. We are in state q if we have seen only 0s so far. p = weve seen at least one 1 and may now proceed only if the inputs are 1s. f = final state; accept.
9

Example: PDA (2)


The stack symbols:
Z0 = start symbol. Also marks the bottom of the stack, so we know when we have counted the same number of 1s as 0s. X = marker, used to count the number of 0s seen on the input.

10

Example: PDA (3)


The transitions:
(q, 0, Z0) = {(q, XZ0)}. (q, 0, X) = {(q, XX)}. These two rules cause one X to be pushed onto the stack for each 0 read from the input. (q, 1, X) = {(p, )}. When we see a 1, go to state p and pop one X. (p, 1, X) = {(p, )}. Pop one X per 1. (p, , Z0) = {(f, Z0)}. Accept at bottom.

11

Actions of the Example PDA


000111 q

Z0

12

Actions of the Example PDA


00111 q

X Z0

13

Actions of the Example PDA


0111 q

X X Z0
14

Actions of the Example PDA


111 q

X X X Z0
15

Actions of the Example PDA


11 p

X X Z0
16

Actions of the Example PDA


1 p

X Z0

17

Actions of the Example PDA

Z0

18

Actions of the Example PDA

Z0

19

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.
20

The Goes-To Relation


To say that ID I can become ID J in one move of the PDA, we write IJ. 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 JK, then I*K.
21

Example: Goes-To
Using the previous example PDA, we can describe the sequence of moves by: (q, 000111, Z0)(q, 00111, XZ0) (q, 0111, XXZ0)(q, 111, XXXZ0) (p, 11, XXZ0)(p, 1, XZ0)(p, , Z0) (f, , Z0) Thus, (q, 000111, Z0)*(f, , Z0). What would happen on input 0001111?
22

Answer

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

(q, 0001111, Z0)(q, 001111, XZ0) (q, 01111, XXZ0)(q, 1111, XXXZ0) (p, 111, XXZ0)(p, 11, XZ0)(p, 1, Z0) (f, 1, Z0) Note the last ID has no move. 0001111 is not accepted, because the input is not completely consumed.
23

Aside: FA and PDA Notations


We represented moves of a FA by an extended , which did not mention the input yet to be read. We could have chosen a similar notation for PDAs, where the FA state is replaced by a state-stack combination, like the pictures just shown.
24

FA and PDA Notations (2)


Similarly, we could have chosen a FA notation with IDs.
Just drop the stack component.

Why the difference? My theory: FA tend to model things like protocols, with indefinitely long inputs. PDA model parsers, which are given a fixed program to process.
25

Language of a PDA
The common way to define the language of a PDA is by final state. If P is a PDA, then L(P) is the set of strings w such that (q0, w, Z0) * (f, , ) for final state f and any .

26

Language of a PDA (2)


Another language defined by the same PDA is by empty stack. If P is a PDA, then N(P) is the set of strings w such that (q0, w, Z0) * (q, , ) for any state q.

27

Equivalence of Language Definitions


1. If L = L(P), then there is another PDA P such that L = N(P). 2. If L = N(P), then there is another PDA P such that L = L(P).

28

Proof: L(P) -> N(P) Intuition


P will simulate P. If P accepts, P will empty its stack. P has to avoid accidentally emptying its stack, so it uses a special bottommarker to catch the case where P empties its stack without accepting.

29

Proof: L(P) -> N(P)


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)}. Get P started. 4. (f, , X) = (e, , X) = {(e, )} for any final state f of P and any stack symbol X.
30

Proof: N(P) -> L(P) Intuition


P simulates P. P has a special bottom-marker to catch the situation where P empties its stack. If so, P accepts.

31

Proof: N(P) -> L(P)


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 final state f. 3. (s, , X0) = {(q0, Z0X0)}. Get P started. 4. (q, , X0) = {(f, )} for any state q of P.
32

Deterministic PDAs
To be deterministic, there must be at most one choice of move for any state q, input symbol a, and stack symbol X. In addition, there must not be a choice between using input or real input. Formally, (q, a, X) and (q, , X) cannot both be nonempty.
33

You might also like