Lecture 14 Push Down Automata (PDA) Topics: Definition Moves of the PDA Languages of the PDA ...
-
Upload
ferdinand-adams -
Category
Documents
-
view
219 -
download
0
description
Transcript of Lecture 14 Push Down Automata (PDA) Topics: Definition Moves of the PDA Languages of the PDA ...
Lecture 14Push Down Automata (PDA)
Topics:Topics: DefinitionDefinition Moves of the PDAMoves of the PDA Languages of the PDALanguages of the PDA Deterministic PDA’sDeterministic PDA’s
June 18, 2015
CSCE 355 Foundations of Computation
– 2 – CSCE 355 Summer 2015ialc slides Ullman Stanford
2
Pushdown Automata
The PDA is an automaton equivalent to the CFG in The PDA is an automaton equivalent to the CFG in language-defining power.language-defining power.
Only the nondeterministic PDA defines all the Only the nondeterministic PDA defines all the CFL’s.CFL’s.
But the deterministic version models parsers.But the deterministic version models parsers. Most programming languages have deterministic PDA’s.
– 3 – CSCE 355 Summer 2015ialc slides Ullman Stanford
3
Intuition: PDA Think of an Think of an εε-NFA with the additional power that it -NFA with the additional power that it
can manipulate a stack.can manipulate a stack. Its moves are determined by: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.
– 4 – CSCE 355 Summer 2015ialc slides Ullman Stanford
4
Intuition: PDA – (2) Being nondeterministic, the PDA can have a choice Being nondeterministic, the PDA can have a choice
of next moves.of next moves. In each choice, the PDA can:In each choice, the PDA can:
1. Change state, and also2. Replace the top symbol on the stack by a sequence of
zero or more symbols. Zero symbols = “pop.” Many symbols = sequence of “pushes.”
– 5 – CSCE 355 Summer 2015ialc slides Ullman Stanford
5
PDA Formalism
A PDA is described by: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).
– 6 – CSCE 355 Summer 2015ialc slides Ullman Stanford
6
Conventions a, b, … are input symbols.a, b, … are input symbols.
But sometimes we allow ε as a possible value.
……, X, Y, Z are stack symbols., X, Y, Z are stack symbols. ……, w, x, y, z are strings of input symbols., w, x, y, z are strings of input symbols. , , ,… are strings of stack symbols.,… are strings of stack symbols.
– 7 – CSCE 355 Summer 2015ialc slides Ullman Stanford
7
The Transition Function
Takes three arguments: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 (q, a, Z) is a set of zero or more actions of the form (p, form (p, ).).
p is a state; is a string of stack symbols.
– 8 – CSCE 355 Summer 2015ialc slides Ullman Stanford
8
Actions of the PDA If If δδ(q, a, Z) contains (p, (q, a, Z) contains (p, ) among its actions, then ) among its actions, then
one thing the PDA can do in state q, with one thing the PDA can do in state q, with aa at the at the front of the input, and Z on top of the stack is: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 .
– 9 – CSCE 355 Summer 2015ialc slides Ullman Stanford
9
Example: PDA Design a PDA to accept {0Design a PDA to accept {0nn11nn | n | n >> 1}. 1}. The states:The states:
q = start state. We are in state q if we have seen only 0’s so far.
p = we’ve seen at least one 1 and may now proceed only if the inputs are 1’s.
f = final state; accept.
– 10 – CSCE 355 Summer 2015ialc slides Ullman Stanford
10
Example: PDA – (2) The stack symbols:The stack symbols:
Z0 = start symbol. Also marks the bottom of the stack, so we know when we have counted the same number of 1’s as 0’s.
X = marker, used to count the number of 0’s seen on the input.
– 11 – CSCE 355 Summer 2015ialc slides Ullman Stanford
11
Example: PDA – (3)
The transitions: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.
– 12 – CSCE 355 Summer 2015ialc slides Ullman Stanfordialc slides Ullman Stanford
12
Actions of the Example PDA
q
0 0 0 1 1 1
Z0
– 13 – CSCE 355 Summer 2015ialc slides Ullman Stanfordialc slides Ullman Stanford
13
Actions of the Example PDA
q
0 0 1 1 1
XZ0
– 14 – CSCE 355 Summer 2015ialc slides Ullman Stanfordialc slides Ullman Stanford
14
Actions of the Example PDA
q
0 1 1 1
XXZ0
– 15 – CSCE 355 Summer 2015ialc slides Ullman Stanfordialc slides Ullman Stanford
15
Actions of the Example PDA
q
1 1 1
XXXZ0
– 16 – CSCE 355 Summer 2015ialc slides Ullman Stanfordialc slides Ullman Stanford
16
Actions of the Example PDA
p
1 1
XXZ0
– 17 – CSCE 355 Summer 2015ialc slides Ullman Stanfordialc slides Ullman Stanford
17
Actions of the Example PDA
p
1
XZ0
– 18 – CSCE 355 Summer 2015ialc slides Ullman Stanfordialc slides Ullman Stanford
18
Actions of the Example PDA
p
Z0
– 19 – CSCE 355 Summer 2015ialc slides Ullman Stanfordialc slides Ullman Stanford
19
Actions of the Example PDA
f
Z0
– 20 – CSCE 355 Summer 2015ialc slides Ullman Stanford
20
Instantaneous Descriptions We can formalize the pictures just seen with an We can formalize the pictures just seen with an
instantaneous descriptioninstantaneous description (ID). (ID). A ID is a triple (q, w, A ID is a triple (q, w, ), where:), where:
1. q is the current state.2. w is the remaining input.3. is the stack contents, top at the left.
– 21 – CSCE 355 Summer 2015ialc slides Ullman Stanford
21
The “Goes-To” Relation
To say that ID I can become ID J in one move of To say that ID I can become ID J in one move of the PDA, we write Ithe PDA, we write I⊦⊦J.J.
Formally, (q, aw, XFormally, (q, aw, X))⊦⊦(p, w, (p, w, ) for any w and ) for any w and , if , if δδ(q, a, X) contains (p, (q, a, X) contains (p, ).).
Extend Extend ⊦ ⊦ to to ⊦⊦*, meaning “zero or more moves,” *, meaning “zero or more moves,” by:by: Basis: I⊦*I. Induction: If I⊦*J and J⊦K, then I⊦*K.
– 22 – CSCE 355 Summer 2015ialc slides Ullman Stanford
22
Example: Goes-To
Using the previous example PDA, we can Using the previous example PDA, we can describe the sequence of moves by: (q, 000111, describe the sequence of moves by: (q, 000111, ZZ00))⊦⊦(q, 00111, XZ(q, 00111, XZ00))⊦ ⊦ (q, 0111, XXZ(q, 0111, XXZ00))⊦⊦(q, (q, 111, XXXZ111, XXXZ00))⊦ ⊦ (p, 11, XXZ(p, 11, XXZ00))⊦⊦(p, 1, XZ(p, 1, XZ00))⊦⊦(p, (p, εε, , ZZ00))⊦ ⊦ (f, (f, εε, Z, Z00))
Thus, (q, 000111, ZThus, (q, 000111, Z00))⊦⊦*(f, *(f, εε, Z, Z00).). What would happen on input 0001111?What would happen on input 0001111?
– 23 – CSCE 355 Summer 2015ialc slides Ullman Stanford
23
Answer (q, 0001111, Z(q, 0001111, Z00))⊦⊦ (q, 001111, XZ(q, 001111, XZ00))⊦ ⊦ (q, 01111, XXZ(q, 01111, XXZ00))⊦⊦ (q, 1111, XXXZ(q, 1111, XXXZ00))⊦ ⊦ (p, 111, XXZ(p, 111, XXZ00))⊦⊦ (p, 11, XZ(p, 11, XZ00))⊦⊦ (p, 1, Z(p, 1, Z00))⊦ ⊦ (f, 1, Z(f, 1, Z00)) Note the last ID has no move.Note the last ID has no move. 0001111 is 0001111 is notnot accepted, because the input is not completely accepted, because the input is not completely
consumed.consumed.
Legal because a PDA can useε input even if input remains.
– 24 – CSCE 355 Summer 2015ialc slides Ullman Stanford
24
Aside: FA and PDA Notations We represented moves of a FA by an extended We represented moves of a FA by an extended δδ, ,
which did not mention the input yet to be read.which did not mention the input yet to be read. We could have chosen a similar notation for PDA’s, We could have chosen a similar notation for PDA’s,
where the FA state is replaced by a state-stack where the FA state is replaced by a state-stack combination, like the pictures just shown.combination, like the pictures just shown.
– 25 – CSCE 355 Summer 2015ialc slides Ullman Stanford
25
FA and PDA Notations – (2)
Similarly, we could have chosen a FA notation Similarly, we could have chosen a FA notation with ID’s.with ID’s. Just drop the stack component.
Why the difference? Why the difference? My theoryMy theory:: FA tend to model things like protocols, with FA tend to model things like protocols, with
indefinitely long inputs.indefinitely long inputs. PDA model parsers, which are given a fixed PDA model parsers, which are given a fixed
program to process.program to process.
– 26 – CSCE 355 Summer 2015ialc slides Ullman Stanford
26
Language of a PDA The common way to define the language of a PDA is The common way to define the language of a PDA is
by by final statefinal state.. If P is a PDA, then If P is a PDA, then L(P)L(P) is the set of strings w such is the set of strings w such
that (qthat (q00, w, Z, w, Z00) ) ⊦⊦* (f, * (f, εε, , ) for final state f and any ) for final state f and any ..
– 27 – CSCE 355 Summer 2015ialc slides Ullman Stanford
27
Language of a PDA – (2) Another language defined by the same PDA is by Another language defined by the same PDA is by
empty stackempty stack.. If P is a PDA, then If P is a PDA, then N(P)N(P) is the set of strings w such is the set of strings w such
that (qthat (q00, w, Z, w, Z00) ) ⊦⊦* (q, * (q, εε, , εε) for any state q.) for any state q.
– 28 – CSCE 355 Summer 2015ialc slides Ullman Stanford
28
Equivalence of Language Definitions
1.1. If L = L(P), then there is another PDA P’ such If L = L(P), then there is another PDA P’ such that L = N(P’).that L = N(P’).
2.2. If L = N(P), then there is another PDA P’’ such If L = N(P), then there is another PDA P’’ such that L = L(P’’).that L = L(P’’).
– 29 – CSCE 355 Summer 2015ialc slides Ullman Stanford
29
Proof: L(P) -> N(P’) Intuition
P’ will simulate P.P’ will simulate P. If P accepts, P’ will empty its stack.If P accepts, P’ will empty its stack. P’ has to avoid accidentally emptying its stack, P’ has to avoid accidentally emptying its stack,
so it uses a special bottom-marker to catch the so it uses a special bottom-marker to catch the case where P empties its stack without case where P empties its stack without accepting.accepting.
– 30 – CSCE 355 Summer 2015ialc slides Ullman Stanford
30
Proof: L(P) -> N(P’)
P’ has all the states, symbols, and moves of P, P’ has all the states, symbols, and moves of P, plus: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.
– 31 – CSCE 355 Summer 2015ialc slides Ullman Stanford
31
Proof: N(P) -> L(P’’) Intuition
P” simulates P.P” simulates P. P” has a special bottom-marker to catch the P” has a special bottom-marker to catch the
situation where P empties its stack.situation where P empties its stack. If so, P” accepts.If so, P” accepts.
– 32 – CSCE 355 Summer 2015ialc slides Ullman Stanford
32
Proof: N(P) -> L(P’’)
P’’ has all the states, symbols, and moves of P, P’’ has all the states, symbols, and moves of P, plus: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.
– 33 – CSCE 355 Summer 2015ialc slides Ullman Stanford
33
Deterministic PDA’s To be deterministic, there must be at most one To be deterministic, there must be at most one
choice of move for any state q, input symbol choice of move for any state q, input symbol aa, and , and stack symbol X.stack symbol X.
In addition, there must not be a choice between In addition, there must not be a choice between using input using input εε or real input. or real input.
Formally, Formally, δδ(q, a, X) and (q, a, X) and δδ(q, (q, εε, X) cannot both be , X) cannot both be nonempty.nonempty.