Pushdown Automata Context Free Languages IV Input tape 1 2 Pushdown Automata 3 5 4 State machine Stack Plan for today Pushdown Automata • Introduction to Pushdown Automata • The stack – The stack has its own alphabet – Included in this alphabet is a special symbol used to indicate an empty stack. (Z 0 ) • This special symbol should not be removed from the stack. • Note that the basic PDA is non- deterministic! Pushdown Automata Pushdown Automata • A pushdown automata (PDA) is essentially: • Let’s formalize this: – An NDFA- Λ with a stack – A pushdown automata (PDA) is a 7-tuple: – A “move” of a PDA will depend upon • M = (Q, Σ , Γ , q 0 , Z 0 , A, δ ) where • Current state of the machine – Q = finite set of states • Current symbol being read in – Σ = tape alphabet • Current symbol popped off the top of the stack – Γ = stack alphabet (may have symbols in common w/ Σ ) – q 0 ∈ Q = start state – With each “move”, the machine can – Z 0 ∈ Γ = initial stack symbol • Move into a new state – A ⊆ Q = set of accepting states • Push symbols on to the stack – δ = transition function 1
Pushdown Automata Pushdown Automata • About this transition function δ : • Configuration of a PDA – During a move of a PDA: – Gives the current “configuration” of the • At most one character is read from the input tape machine – Λ transitions are okay • The topmost character is popped from the stack – Unless it is Z 0 – (p, x, α ) where • The machine will move to a new state based on: • p is the current state – The character read from the tape • x is a string indicating what remains to be read on – The character popped off the stack the tape – The current state of the machine • 0 or more symbols from the stack alphabet are pushed onto the • α is the current contents of the stack. stack. Pushdown Automata Pushdown Automata • Formally: • Move of a PDA: – δ : Q x ( Σ ∪ { Λ }) x Γ → (finite subsets of Q x Γ * ) – We can describe a single move of a PDA: • (q, x, α ) a (p, y, β ) – Domain: • If: • Q = state – x = ay, α = γ X, β = YX • ( Σ ∪ { Λ }) = symbol read off tape • Γ = symbol popped off stack » And – δ (q, x, γ ) includes (p, Y) or – Range – δ (q, Λ , γ ) includes (p, Y) and x = y. • Q = new state • Γ * = symbols pushed onto the stack Pushdown Automata Pushdown Automata • Example: • Moves of a PDA – δ (q, a, a ) = ( p, aa) – We can write: • (q, x, α ) a * (p, y, β ) • Meaning: – When in state q, • If – Reading in an a from the tape – You can get from one configuration to the other by – With an a popped off the stack applying 0 or more moves. • The machine will – Go into state p – Push the string “aa” onto the stack 2
Pushdown Automata Pushdown Automata • Strings accepted by a PDA • Let’s look at an example: – L = { xcx r | x ∈ { a,b } * } – Let M = (Q, Σ , Γ , q 0 , Z 0 , A, δ ) be a PDA – x is accepted by M if – Basic idea for building a PDA • (q 0 , x, Z 0 ) a * (q, Λ , β ) • Read chars off the tape until you reach the ‘c’. • As you read chars push them on the stack • Where • After reading the c, match the chars read with the chars popped off the stack until all chars are read – q ∈ A • If at any point the char read does not match the char popped, – β ∈ Γ * the machine “crashes” Pushdown Automata Pushdown Automata • Let’s look at an example: • Strings accepted by a PDA – L = { xcx r | x ∈ { a,b } * } – Start at (q 0 , x, Z 0 ) • Start state q 0 • X on the input tape – The PDA will have 4 states • Empty stack • State 0 (initial) : reading before the ‘c’ – End with (q, Λ , β ) • State 1: read the ‘c’ • End in an accepting state • State 2 :read after ‘c’, comparing chars • All characters of x have been read • Some string on the stack (doesn’t matter what). • State 3: (accepting): move only after all chars read and stack empty – Acceptance by “final state” Pushdown Automata Pushdown Automata • The language accepted by a PDA • Let’s look at an example: – L = { xcx r | x ∈ { a,b } * } – Let M = (Q, Σ , Γ , q 0 , Z 0 , A, δ ) be a PDA – The language accepted by M, b, b / Λ • Denoted L(M) is b, Z 0 / b Z 0 c, Z 0 / Z 0 Λ , Z 0 / Z 0 a, a / Λ • The set of all strings x that are accepted by M. a, Z 0 / a Z 0 Λ , a / a c, a / a c, b / b Λ , b / b q 0 q 1 q 2 q 3 Λ , Z / Z b, b / bb a, b / ab a, a / aa b, a / ba 3
PDA Example Pushdown Automata • Transition for abcba • Let’s look at another example: – L = { xx r | x ∈ { a,b } * } – (q 0 , abcba, Z) a (q 0 , bcba, a) // push a – a (q 0 , cba, ba) // push b – a (q 1 , ba, ba) // goto 1 – Basic idea for building a PDA a (q 2 , ba, ba) // Λ trans – • Much like last example, except – a (q 2 , a, a) // pop b – This time we don’t know when to start popping and a (q 2 , Λ , Z) // pop a – comparing – Since PDAs are non-deterministic, this is not a problem a (q 3 , Λ , Z) // Accept! – PDA Example Pushdown Automata • Let’s look at another example: • Transition for abcb – L = { xx r | x ∈ { a,b } * } – (q 0 , abcb, Z) a (q 0 , bcb, a) // push a – a (q 0 , cb, ba) // push b – The PDA will have 3 states – a (q 1 , b, ba) // goto 1 • State 0 (initial) : reading before the center of string • State 1: read after center of string, comparing chars a (q 2 , b, ba) // Λ trans – • State 2 (accepting): after all chars read, stack should be empty a (q 2 , Λ , a) // pop b – – The machine can choose to go from state 0 to state 1 at any time: – Nowhere to go // Reject! • Will result in many “wrong” set of moves • All you need is one “right” set of moves for a string to be accepted. Pushdown Automata Pushdown Automata • I bet you’re wondering if JFLAP can handle • Let’s look at an example: PDAs! – L = { xx r | x ∈ { a,b } * } – Yes, it can… b, b / Λ – Let’s take a look. b, Z 0 / b Z 0 Λ , Z 0 / Z 0 a, a / Λ a, Z 0 / a Z 0 Λ , a / a Λ , b / b q 0 q 1 q 2 Λ , Z / Z b, b / bb a, b / ab a, a / aa b, a / ba 4
PDA Example Pushdown Automata • Let’s see a bad transition set for abba • Questions? – (q 0 , abba, Z) a (q 0 , bba, a) // push a – a (q 0 , ba, ba) // push b – a (q 0 , a, bba) // push b a (q 1 , a, bba) // Λ trans – – Nowhere to go // Reject! PDA Example Deterministic PDAs • Let’s see a good transition set for abba • As mentioned before – (q 0 , abba, Z) a (q 0 , bba, a) // push a – Our basic PDA in non-deterministic – a (q 0 , ba, ba) // push b – We can define a Deterministic PDA (DPDA) as follows: a (q 1 , ba, ba) // Λ trans – • Let M = (Q, Σ , Γ , q 0 , Z 0 , A, δ ) be a PDA – a (q 1 , a, a) // pop b • M is deterministic if: a (q 1 , Λ , Z) // pop a – – δ (q, a, X) has at most one element a (q 2 , Λ , Z) – // Accept! – If δ (q, Λ , X) ≠ ∅ then δ (q, a, X) = ∅ for all a ∈ Σ Pushdown Automata Deterministic PDAs • “Let’s go to the video tape” • In other words: – Actually JFLAP… – There is no configuration where the machine has a “choice” of moves • Each transition has at most 1 element. • If you can make a Λ -transition from a state with a given symbol on the stack, – You cannot make that same transition on any tape input symbol. 5
Deterministic PDAs PDA Example • A language L is a deterministic context-free • Example: – L = { x ∈ { a, b } * | n a (x) > n b (x) } language (DCFL) if there is a DPA that accepts L b, Z 0 / b Z 0 a, Z 0 / a Z 0 Λ , a / a q 0 q 1 b, b / bb a, b / Λ a, a / aa b, a / Λ Non-determinism PDA Example PDA Example • Example: • Let’s try on JFLAP – L = { x ∈ { a, b } * | n a (x) > n b (x) } – First using a PDA: • Let the stack store the “excess” of one symbol over another – If more a’s have been read than b’s, a’s will be on the stack, and via versa – If a is on the stack and you read a b, simple match the a with the b. – If a is on the stack and you read an a, we have one more extra a – Push it on the stack. – An empty stack means the number of a’s and b’s are equal. PDA Example PDA Example • Example: Example: – L = { x ∈ { a, b } * | n a (x) > n b (x) } – L = { x ∈ { a, b } * | n a (x) > n b (x) } – The PDA will have 2 states: – Removing the non-determinism : • State 0 (start) : where all the work gets done • Let the stack store 1 minus the “excess” of one • State 1 (accepting) : one you’re in here, the machine symbol over another stops. • The state will determine whether you have excess – The machine can “choose” to go into state 1 on a’s or excess b’s a Λ transition whenever an a is on the stack. 6
Recommend
More recommend