Top Banner
Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 1
68

Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

Jan 03, 2016

Download

Documents

Laurence Sutton
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

1

Definition

Moves of the PDA

Languages of the PDA

Deterministic PDA’s

Pushdown Automata

Page 2: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

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.

But the deterministic version models parsers. Most programming languages have deterministic PDA’s.

Page 3: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

3

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.

Page 4: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

4

Picture of a PDA

q

0 1 1 1

XYZ

State

Stack

Top of Stack

Input

Next inputsymbol

Page 5: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

5

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

Page 6: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

6

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 7: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

7

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.

Page 8: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

8

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.

Page 9: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

9

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 .

Page 10: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

10

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

Page 11: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

11

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 1’s as 0’s.

X = marker, used to count the number of 0’s seen on the input.

Page 12: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

12

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.

Page 13: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

13

Actions of the Example PDA

q

0 0 0 1 1 1

Z0

Page 14: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

14

Actions of the Example PDA

q

0 0 1 1 1

XZ0

Page 15: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

15

Actions of the Example PDA

q

0 1 1 1

XXZ0

Page 16: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

16

Actions of the Example PDA

q

1 1 1

XXXZ0

Page 17: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

17

Actions of the Example PDA

p

1 1

XXZ0

Page 18: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

18

Actions of the Example PDA

p

1

XZ0

Page 19: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

19

Actions of the Example PDA

p

Z0

Page 20: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

20

Actions of the Example PDA

f

Z0

Page 21: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

Graphical Presentation

q p f

1, X/ ε0, X/ XX0, Z0/XZ0

1, X/ ε

1, X/ ε

ε, Z0/Z0

f

Page 22: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

22

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 23: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

23

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 24: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

24

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?

Page 25: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

25

Answer

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

Page 26: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

Theorem 1: Given a PDA P, if (q, x, )⊦* (p, y, ) , for all the string w in Σ* and all the string γ in Γ*, we have (q, xw, γ)⊦* (p, yw, γ)

Vice Versa?

Theorem 2: Given a PDA P, if (q, xw, )⊦* (p, yw, ) , we have (q, x, )⊦* (p, y, )

Page 27: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

27

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 .

Page 28: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

28

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.

Page 29: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

29

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’’).

Page 30: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

30

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 bottom-marker to catch the case where P empties its stack without accepting.

Page 31: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

31

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

P’ has all the states, symbols, and moves of P, plus:1. Stack symbol X0 (the start symbol of P’), used to guard

the stack bottom.

2. New start state s and “erase” state e.

3. δ(s, ε, X0) = {(q0, Z0X0)}. Get P started.

4. Add {(e, ε)} to δ(f, ε, X) for any final state f of P and any stack symbol X, including X0.

5. δ(e, ε, X) = {(e, ε)} for any X.

Why add X0?

Page 32: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

Graphical Presentation

s q0

f

ε, X0/Z0X0

ε, any/ε

f

e

ε, any/ε

ε, any/ε

Page 33: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

33

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.

Page 34: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

34

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

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

1. Stack symbol X0 (the start symbol), 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.

Again, why add X0?

Page 35: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

p

Graphical Presentation

s q0

ε, X0/Z0X0

ε, X0/ε

f

ε, X0/ε

ε, X0/ε

Page 36: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

Example

Design a PDA, which can handle the if else statement, it stops when the number of else exceeds the number of prefix if

p

e, Z/ εi, Z/ ZZ

N(P) version

Page 37: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

Construct the L(P) version?

p s p

ε, X0/Z0X0

f

ε, X0/ε

e, Z/ εi, Z/ ZZ

Page 38: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

38

Deterministic PDA’s

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.

Page 39: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

NPDA VS PDA

NPDA is more powerful than PDA

Think about wwR.

Suppose there is such a DPDA, when you met 0m 11 0m, as you need to make sure this string has same number of 0 before and after 11, the stack of the DPDA has to be “emptied”.

Suppose you met 0m 11 0m after that, the DPDA has to accept

Suppose you met 0n110n after that, the DPDA has to reject.

But, as the stack is already empty, how can the DPDA tell m!=n?

Page 40: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

wcwR can be presented by DPDA

When you have not met c, push. Otherwise, pop.

Page 41: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

Theorem: If L is a regular language, there exists a DPDA P, such that L=L(P)

Proof:?

Think about a DFA with a stack that never change…

Page 42: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

In the previous slides, the DPDA is defined by final states, how about empty stack?

These two are not equivalent on the DPDA case!

Think about {0}*.

Page 43: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

43

Deterministic PDA’s

RE

DPDA (L(P))

NPDA

RE

DPDA (N(P))

DPDA (L(P))

Page 44: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

DPDA and Ambiguity

Given a DPDA P defined by final states, L=L(P), L has a non-ambiguous grammar.

However, non-ambiguous grammars don’t have to be able to be presented by DPDA.

Think about wwr S->0S0|1S1| ε

Page 45: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

45

Conversion of CFG to PDA

Conversion of PDA to CFG

Equivalence of PDA, CFG

Page 46: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

46

Overview

When we talked about closure properties of regular languages, it was useful to be able to jump between RE and DFA representations.

Similarly, CFG’s and PDA’s are both useful to deal with properties of the CFL’s.

Page 47: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

47

Overview – (2)

Also, PDA’s, being “algorithmic,” are often easier to use when arguing that a language is a CFL.

Example: It is easy to see how a PDA can recognize balanced parentheses; not so easy as a grammar.

Page 48: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

48

Converting a CFG to a PDA

Let L = L(G). Construct PDA P such that N(P) = L. P has:

One state q. Input symbols = terminals of G. Stack symbols = all symbols of G. Start symbol = start symbol of G.

Page 49: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

49

Intuition About P

At each step, P represents some left-sentential form (step of a leftmost derivation).

If the stack of P is , and P has so far consumed x from its input, then P represents left-sentential form x.

At empty stack, the input consumed is a string in L(G).

Page 50: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

50

Transition Function of P

1. δ(q, a, a) = (q, ε). (Type 1 rules) This step does not change the LSF represented, but “moves”

responsibility for a from the stack to the consumed input.

2. If A -> is a production of G, then δ(q, ε, A) contains (q, ). (Type 2 rules)

Guess a production for A, and represent the next LSF in the derivation.

Page 51: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

51

Proof That L(P) = L(G)

We need to show that (q, wx, S) ⊦* (q, x, ) for any x if and only if S =>*lm w.

Part 1: “only if” is an induction on the number of steps made by P.

Basis: 0 steps. Then = S, w = ε, and S =>*lm S is surely true.

Page 52: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

52

Induction for Part 1

Consider n moves of P: (q, wx, S) ⊦* (q, x, ) and assume the IH for sequences of n-1 moves.

There are two cases, depending on whether the last move uses a Type 1 or Type 2 rule.

Page 53: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

53

Use of a Type 1 Rule

The move sequence must be of the form (q, yax, S) ⊦* (q, ax, a) ⊦ (q, x, ), where ya = w.

By the IH applied to the first n-1 steps, S =>*lm ya.

But ya = w, so S =>*lm w.

Page 54: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

54

Use of a Type 2 Rule

The move sequence must be of the form (q, wx, S) ⊦* (q, x, A) ⊦ (q, x, ), where A -> is a production and = .

By the IH applied to the first n-1 steps, S =>*lm wA.

Thus, S =>*lm w = w.

Page 55: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

55

Proof of Part 2 (“if”)

We also must prove that if S =>*lm w, then (q, wx, S) ⊦* (q, x, ) for any x.

Induction on number of steps in the leftmost derivation.

Ideas are similar; omitted.

Page 56: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

56

Proof – Completion

We now have (q, wx, S) ⊦* (q, x, ) for any x if and only if S =>*lm w.

In particular, let x = = ε. Then (q, w, S) ⊦* (q, ε, ε) if and only if S =>*lm w. That is, w is in N(P) if and only if w is in L(G).

Page 57: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

57

From a PDA to a CFG

Now, assume L = N(P). We’ll construct a CFG G such that L = L(G). Intuition: G will have variables [pXq] generating

exactly the inputs that cause P to have the net effect of popping stack symbol X while going from state p to state q. P never gets below this X while doing so.

Page 58: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

58

Picture: Popping X

X

w

Stackheight

Page 59: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

59

Variables of G

G’s variables are of the form [pXq]. This variable generates all and only the strings w

such that

(p, w, X) ⊦*(q, ε, ε). Also a start symbol S we’ll talk about later.

Page 60: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

60

Productions of G

Each production for [pXq] comes from a move of P in state p with stack symbol X.

Simplest case: δ(p, a, X) contains (q, ε). Note a can be an input symbol or ε.

Then the production is [pXq] -> a. Here, [pXq] generates a, because reading a is one

way to pop X and go from p to q.

Page 61: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

61

Productions of G – (2)

Next simplest case: δ(p, a, X) contains (r, Y) for some state r and symbol Y.

G has production [pXq] -> a[rYq]. We can erase X and go from p to q by reading a

(entering state r and replacing the X by Y) and then reading some w that gets P from r to q while erasing the Y.

Page 62: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

62

Picture of the Action

X Y

a w

p r q

Page 63: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

63

Productions of G – (3)

Third simplest case: δ(p, a, X) contains (r, YZ) for some state r and symbols Y and Z.

Now, P has replaced X by YZ. To have the net effect of erasing X, P must erase

Y, going from state r to some state s, and then erase Z, going from s to q.

Page 64: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

64

Picture of the Action

X Z Z

Y

p r s q

a u v

Page 65: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

65

Third-Simplest Case – Concluded

Since we do not know state s, we must generate a family of productions:

[pXq] -> a[rYs][sZq]

for all states s. [pXq] =>* auv whenever [rYs] =>* u and

[sZq] =>* v.

Page 66: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

66

Productions of G: General Case

Suppose δ(p, a, X) contains (r, Y1,…Yk) for some state r and k > 3.

Generate family of productions

[pXq] ->

a[rY1s1][s1Y2s2]…[sk-2Yk-1sk-1][sk-1Ykq]

Page 67: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

67

Completion of the Construction

We can prove that (q0, w, Z0)⊦*(p, ε, ε) if and only if [q0Z0p] =>* w. Proof is two easy inductions.

But state p can be anything. Thus, add to G another variable S, the start

symbol, and add productions S -> [q0Z0p] for each state p.

Page 68: Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.

Example Recall

Design a PDA, which can handle the if else statement, it stops when the number of else exceeds the number of prefix if

p

e, Z/ εi, Z/ ZZ

S->[pZp]

[pZp]->e

[pZp]->i[pZp][pZp]

S->A

A->e

A->iAA

S->e|iSS