Pushdown Automata Definition Moves of the PDA Languages

  • Slides: 68
Download presentation
Pushdown Automata Definition Moves of the PDA Languages of the PDA Deterministic PDA’s w

Pushdown Automata Definition Moves of the PDA Languages of the PDA Deterministic PDA’s w 1

Pushdown Automata n n n The PDA is an automaton equivalent to the CFG

Pushdown Automata n n n 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. w 2

Intuition: PDA n n Think of an ε-NFA with the additional power that it

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

Picture of a PDA Next input symbol 0111 q X Y Z Input State

Picture of a PDA Next input symbol 0111 q X Y Z Input State Top of Stack w 4

Intuition: PDA – (2) n n Being nondeterministic, the PDA can have a choice

Intuition: PDA – (2) n n Being nondeterministic, the PDA can have a choice of next moves. In each choice, the PDA can: 1. 2. Change state, and also Replace the top symbol on the stack by a sequence of zero or more symbols. u u Zero symbols = “pop. ” Many symbols = sequence of “pushes. ” w 5

PDA Formalism n A PDA is described by: 1. 2. 3. 4. 5. 6.

PDA Formalism n A PDA is described by: 1. 2. 3. 4. 5. 6. 7. A finite set of states (Q, typically). An input alphabet (Σ, typically). A stack alphabet (Γ, typically). A transition function (δ, typically). A start state (q 0, in Q, typically). A start symbol (Z 0, in Γ, typically). A set of final states (F ⊆ Q, typically). w 6

Conventions n a, b, … are input symbols. ¡ n n n But sometimes

Conventions n a, b, … are input symbols. ¡ n n n 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. w 7

The Transition Function n Takes three arguments: 1. 2. 3. n A state, in

The Transition Function n Takes three arguments: 1. 2. 3. n A state, in Q. An input, which is either a symbol in Σ or ε. 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. w 8

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

Actions of the PDA n 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: 2. Change the state to p. Remove a from the front of the input (but a may be ε). 3. Replace Z on the top of the stack by . 1. w 9

Example: PDA n n Design a PDA to accept {0 n 1 n |

Example: PDA n n Design a PDA to accept {0 n 1 n | 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. w 10

Example: PDA – (2) n The stack symbols: ¡ ¡ Z 0 = start

Example: PDA – (2) n The stack symbols: ¡ ¡ Z 0 = 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. w 11

Example: PDA – (3) n The transitions: ¡ ¡ ¡ δ(q, 0, Z 0)

Example: PDA – (3) n The transitions: ¡ ¡ ¡ δ(q, 0, Z 0) = {(q, XZ 0)}. δ(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, ε, Z 0) = {(f, Z 0)}. Accept at bottom. w 12

Actions of the Example PDA 000111 q Z 0 w 13

Actions of the Example PDA 000111 q Z 0 w 13

Actions of the Example PDA 00111 q X Z 0 w 14

Actions of the Example PDA 00111 q X Z 0 w 14

Actions of the Example PDA 0111 q X X Z 0 w 15

Actions of the Example PDA 0111 q X X Z 0 w 15

Actions of the Example PDA 111 q X X X Z 0 w 16

Actions of the Example PDA 111 q X X X Z 0 w 16

Actions of the Example PDA 11 p X X Z 0 w 17

Actions of the Example PDA 11 p X X Z 0 w 17

Actions of the Example PDA 1 p X Z 0 w 18

Actions of the Example PDA 1 p X Z 0 w 18

Actions of the Example PDA p Z 0 w 19

Actions of the Example PDA p Z 0 w 19

Actions of the Example PDA f Z 0 w 20

Actions of the Example PDA f Z 0 w 20

Graphical Presentation 1,X/ε 0,X/XX 0,Z 0/XZ 0 q 1,X/ε p 1,X/ε ff ε,Z 0/Z

Graphical Presentation 1,X/ε 0,X/XX 0,Z 0/XZ 0 q 1,X/ε p 1,X/ε ff ε,Z 0/Z 0

Instantaneous Descriptions n n We can formalize the pictures just seen with an instantaneous

Instantaneous Descriptions n n We can formalize the pictures just seen with an instantaneous description (ID). A ID is a triple (q, w, ), where: 1. 2. 3. q is the current state. w is the remaining input. is the stack contents, top at the left. w 22

The “Goes-To” Relation n To say that ID I can become ID J in

The “Goes-To” Relation n 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. w 23

Example: Goes-To n Using the previous example PDA, we can describe the sequence of

Example: Goes-To n Using the previous example PDA, we can describe the sequence of moves by: (q, 000111, Z 0)⊦(q, 00111, XZ 0)⊦ (q, 0111, XXZ 0)⊦(q, 111, XXXZ 0)⊦ (p, 11, XXZ 0)⊦(p, 1, XZ 0)⊦(p, ε, Z 0)⊦ (f, ε, Z 0) Thus, (q, 000111, Z 0)⊦*(f, ε, Z 0). n What would happen on input 0001111? n w 24

Answer n (q, 0001111, Z 0)⊦(q, 001111, XZ 0)⊦ (q, 01111, XXZ 0)⊦(q, 1111,

Answer n (q, 0001111, Z 0)⊦(q, 001111, XZ 0)⊦ (q, 01111, XXZ 0)⊦(q, 1111, XXXZ 0)⊦ (p, 111, XXZ 0)⊦(p, 11, XZ 0)⊦(p, 1, Z 0)⊦ (f, 1, Z 0) n Note the last ID has no move. 0001111 is not accepted, because the input is not completely consumed. n w 25

n n n Theorem 1: Given a PDA P, if (q, x, )⊦* (p,

n n n 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, )

Language of a PDA n The common way to define the language of a

Language of a PDA n The common way to define the language of a PDA is by final state. n If P is a PDA, then L(P) is the set of strings w such that (q 0, w, Z 0) ⊦* (f, ε, ) for final state f and any . w 27

Language of a PDA – (2) n n Another language defined by the same

Language of a PDA – (2) n n 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 (q 0, w, Z 0) ⊦*(q, ε, ε) for any state q. w 28

Equivalence of Language Definitions 1. 2. If L = L(P), then there is another

Equivalence of Language Definitions 1. 2. If L = L(P), then there is another PDA P’ such that L = N(P’). If L = N(P), then there is another PDA P’’ such that L = L(P’’). w 29

Proof: L(P) -> N(P’) Intuition n P’ will simulate P. If P accepts, P’

Proof: L(P) -> N(P’) Intuition n 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. w 30

Proof: L(P) -> N(P’) n P’ has all the states, symbols, and moves of

Proof: L(P) -> N(P’) n P’ has all the states, symbols, and moves of P, plus: 1. 2. 3. 4. 5. n Stack symbol X 0 (the start symbol of P’), used to guard the stack bottom. New start state s and “erase” state e. δ(s, ε, X 0) = {(q 0, Z 0 X 0)}. Get P started. Add {(e, ε)} to δ(f, ε, X) for any final state f of P and any stack symbol X, including X 0. δ(e, ε, X) = {(e, ε)} for any X. Why add X 0? w 31

Graphical Presentation ε,any/ε f s q 0 ε,any/ε e ε,X 0/Z 0 X 0

Graphical Presentation ε,any/ε f s q 0 ε,any/ε e ε,X 0/Z 0 X 0 f ε,any/ε

Proof: N(P) -> L(P’’) Intuition n P” simulates P. P” has a special bottom-marker

Proof: N(P) -> L(P’’) Intuition n P” simulates P. P” has a special bottom-marker to catch the situation where P empties its stack. If so, P” accepts. w 33

Proof: N(P) -> L(P’’) n P’’ has all the states, symbols, and moves of

Proof: N(P) -> L(P’’) n P’’ has all the states, symbols, and moves of P, plus: 1. 2. 3. 4. n Stack symbol X 0 (the start symbol), used to guard the stack bottom. New start state s and final state f. δ(s, ε, X 0) = {(q 0, Z 0 X 0)}. Get P started. δ(q, ε, X 0) = {(f, ε)} for any state q of P. Again,why add X 0? w 34

Graphical Presentation ε,X 0/ε s q 0 pf ε,X 0/Z 0 X 0 ε,X

Graphical Presentation ε,X 0/ε s q 0 pf ε,X 0/Z 0 X 0 ε,X 0/ε

Example n Design a PDA, which can handle the if else statement, it stops

Example n Design a PDA, which can handle the if else statement, it stops when the number of else exceeds the number of prefix if e,Z/ε i,Z/ZZ N(P) version p

n Construct the L(P) version? e,Z/ε i,Z/ZZ s p ε,X 0/Z 0 X 0

n Construct the L(P) version? e,Z/ε i,Z/ZZ s p ε,X 0/Z 0 X 0 pf ε,X 0/ε

Deterministic PDA’s n n To be deterministic, there must be at most one choice

Deterministic PDA’s n n 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. w 38

NPDA VS PDA n NPDA is more powerful than PDA n Think about ww.

NPDA VS PDA n NPDA is more powerful than PDA n Think about ww. R. n Suppose there is such a DPDA, when you met 0 m 11 0 m, 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”. n Suppose you met 0 m 11 0 m after that, the DPDA has to accept n Suppose you met 0 n 110 n after that, the DPDA has to reject. n But, as the stack is already empty, how can the DPDA tell m!=n?

n wcw. R can be presented by DPDA n When you have not met

n wcw. R can be presented by DPDA n When you have not met c, push. Otherwise, pop.

n Theorem: If L is a regular language, there exists a DPDA P, such

n Theorem: If L is a regular language, there exists a DPDA P, such that L=L(P) n Proof: ? n Think about a DFA with a stack that never change…

n In the previous slides, the DPDA is defined by final states, how about

n In the previous slides, the DPDA is defined by final states, how about empty stack? n These two are not equivalent on the DPDA case! n Think about {0}*.

Deterministic PDA’s n RE n DPDA (L(P)) n DPDA (N(P)) n NPDA n DPDA

Deterministic PDA’s n RE n DPDA (L(P)) n DPDA (N(P)) n NPDA n DPDA (L(P)) Try to prove theorem 6. 19 in the textbook w 43

DPDA and Ambiguity n Given a DPDA P defined by final states, L=L(P), L

DPDA and Ambiguity n Given a DPDA P defined by final states, L=L(P), L has a non-ambiguous grammar. n However, non-ambiguous grammars don’t have to be able to be presented by DPDA. n Think about wwr S->0 S 0|1 S 1| ε

Equivalence of PDA, CFG Conversion of CFG to PDA Conversion of PDA to CFG

Equivalence of PDA, CFG Conversion of CFG to PDA Conversion of PDA to CFG w 45

Overview n n When we talked about closure properties of regular languages, it was

Overview n n 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. w 46

Overview – (2) n n Also, PDA’s, being “algorithmic, ” are often easier to

Overview – (2) n n 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. w 47

Converting a CFG to a PDA n n n Let L = L(G). Construct

Converting a CFG to a PDA n n n 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. w 48

Intuition About P n n n At each step, P represents some left-sentential form

Intuition About P n n n 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). w 49

Transition Function of P δ(q, a, a) = (q, ε). (Type 1 rules) 1.

Transition Function of P δ(q, a, a) = (q, ε). (Type 1 rules) 1. u This step does not change the LSF represented, but “moves” responsibility for a from the stack to the consumed input. If A -> is a production of G, then δ(q, ε, A) contains (q, ). (Type 2 rules) 2. u Guess a production for A, and represent the next LSF in the derivation. w 50

Proof That L(P) = L(G) n n n We need to show that (q,

Proof That L(P) = L(G) n n n 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. w 51

Induction for Part 1 n n Consider n moves of P: (q, wx, S)

Induction for Part 1 n n 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. w 52

Use of a Type 1 Rule n The move sequence must be of the

Use of a Type 1 Rule n The move sequence must be of the form (q, yax, S) ⊦* (q, ax, a ) ⊦ (q, x, ), where ya = w. n By the IH applied to the first n-1 steps, S =>*lm ya. But ya = w, so S =>*lm w. n w 53

Use of a Type 2 Rule n n n The move sequence must be

Use of a Type 2 Rule n n n 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 w. A. Thus, S =>*lm w = w. w 54

Proof of Part 2 (“if”) n We also must prove that if S =>*lm

Proof of Part 2 (“if”) n We also must prove that if S =>*lm w , then (q, wx, S) ⊦* (q, x, ) for any x. n Induction on number of steps in the leftmost derivation. Ideas are similar; omitted. n w 55

Proof – Completion n We now have (q, wx, S) ⊦* (q, x, )

Proof – Completion n 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. n That is, w is in N(P) if and only if w is in L(G). n n w 56

From a PDA to a CFG n n n Now, assume L = N(P).

From a PDA to a CFG n n n Now, assume L = N(P). We’ll construct a CFG G such that L = L(G). Intuition: G will have variables [p. Xq] 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. w 57

Picture: Popping X Stack height X w w 58

Picture: Popping X Stack height X w w 58

Variables of G n n n G’s variables are of the form [p. Xq].

Variables of G n n n G’s variables are of the form [p. Xq]. 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. w 59

Productions of G n n Each production for [p. Xq] comes from a move

Productions of G n n Each production for [p. Xq] comes from a move of P in state p with stack symbol X. Simplest case: δ(p, a, X) contains (q, ε). ¡ n n Note a can be an input symbol or ε. Then the production is [p. Xq] -> a. Here, [p. Xq] generates a, because reading a is one way to pop X and go from p to q. w 60

Productions of G – (2) n n Next simplest case: δ(p, a, X) contains

Productions of G – (2) n n Next simplest case: δ(p, a, X) contains (r, Y) for some state r and symbol Y. G has production [p. Xq] -> a[r. Yq]. ¡ 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. w 61

Picture of the Action X Y p r a q w w 62

Picture of the Action X Y p r a q w w 62

Productions of G – (3) n n n Third simplest case: δ(p, a, X)

Productions of G – (3) n n n 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. w 63

Picture of the Action Y X p a Z Z r s u q

Picture of the Action Y X p a Z Z r s u q v w 64

Third-Simplest Case – Concluded n n Since we do not know state s, we

Third-Simplest Case – Concluded n n Since we do not know state s, we must generate a family of productions: [p. Xq] -> a[r. Ys][s. Zq] for all states s. [p. Xq] =>* auv whenever [r. Ys] =>* u and [s. Zq] =>* v. w 65

Productions of G: General Case n n Suppose δ(p, a, X) contains (r, Y

Productions of G: General Case n n Suppose δ(p, a, X) contains (r, Y 1, …Yk) for some state r and k > 3. Generate family of productions [p. Xq] -> a[r. Y 1 s 1][s 1 Y 2 s 2]…[sk-2 Yk-1 sk-1][sk-1 Ykq] w 66

Completion of the Construction n We can prove that (q 0, w, Z 0)⊦*(p,

Completion of the Construction n We can prove that (q 0, w, Z 0)⊦*(p, ε, ε) if and only if [q 0 Z 0 p] =>* w. ¡ n n 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 -> [q 0 Z 0 p] for each state p. w 67

Example Recall n Design a PDA, which can handle the if else statement, it

Example Recall n Design a PDA, which can handle the if else statement, it stops when the number of else exceeds the number of prefix if e,Z/ε i,Z/ZZ p S->[p. Zp] S->A [p. Zp]->e A->e [p. Zp]->i[p. Zp] A->i. AA S->e|i. SS