280 likes | 298 Views
Learn about pushdown automata versus NFA, their state controls, and computational power. Discover how PDAs accept languages through stack manipulation. Converting CFGs to PDAs for better understanding.
E N D
Fall 2011 The Chinese University of Hong Kong CSCI 3130: Formal languages and automata theory Pushdown automata Andrej Bogdanov http://www.cse.cuhk.edu.hk/~andrejb/csc3130
Motivation regularexpression NFA DFA syntactic computational is more powerful than CFG pushdown automaton syntactic computational
Pushdown automata versus NFA state control input 0 1 0 0 NFA
Pushdown automata state control input 0 1 0 0 … stack pushdown automaton (PDA) A PDA is like an NFA with but with an infinite stack
Pushdown automata state control input 0 1 0 0 … $ 0 1 1 stack pushdown automaton (PDA) As the PDA is reading the input, it can push / pop symbols from the top of the stack
Building a PDA state control L = {0n1n: n ≥ 1} push $ We remember each 0 by pushingx onto the stack read 0 push x read 1 When we see a 1, we pop an x from the stack pop x read 1 pop x We want to accept when we hit the stack bottom pop $ We’ll use t$mark bottom
A PDA in action state control L = {0n1n: n ≥ 1} push $ input read 0 0 0 0 1 1 1 push x read 1 pop x … $ x x x read 1 stack pop x pop $
Notation for PDAs q0 e, e / $ push $ q1 0, e / x read 0 push x 1, x / e read 1 pop x q2 1, x / e read 1 pop x e, $ / e pop $ q3 read, pop / push
Definition of a PDA A pushdown automaton is (Q, ,, , q0, F) where: • Q is a finite set of states; • is the input alphabet; • is the stack alphabet • q0in Q is the initial state; • F Q is a set of final states; • is the transition function d: Q ( {}) ( {}) → subsets of Q (G {}) pop symbol state input symbol state push symbol
Example q0 S = {0, 1} e, e / $ G = {$, x} q1 0, e / x d(q0, e, e) = {(q1, $)} 1, x / e d(q0, e, $) = ∅ d(q0, e, x) = ∅ q2 1, x / e d(q0, 0, e) = ∅ ... e, $ / e q3 d: Q ( {}) ( {}) → subsets of Q (G {}) pop symbol state input symbol state push symbol
The language of a PDA • A PDA is nondeterministicMultiple transitions on same pop/input allowed • Transitions may but do not have to push or pop The language of a PDA is the set of all strings in S* that can lead the PDA to an accepting state
Example 1 • L = {w#wR: w{0, 1}*} • S = {0, 1, #} • G = {0, 1} #, 0#0, 01#10 L e, 01#1, 0##0 ∉L e, e / $ #, e / e e, $/ e q1 q3 q2 q0 0, e / 0 0, 0 / e 1, e / 1 1, 1 / e write w on stack read wR from stack
Example 2 • L = {wwR: w S*} • S = {0, 1} e, 00, 0110 L 1, 011, 010 ∉L e, e / $ e, e / e e, $/ e q1 q3 q0 q2 0, e / 0 0, 0 / e 1, e / 1 1, 1 / e guess middle of string
Example 3 • L = {w: w = wR, w S*} • S = {0, 1} e, 1, 00, 010, 0110 L 0110110110 or 011010110 011 ∉L x xR x xR e, e / e 0, e / e e, e / $ e, $/ e 1, e / e q1 q3 q0 q2 0, e / 0 0, 0 / e 1, e / 1 1, 1 / e middle symbol can be e, 0, or 1
Example 4 L = {0n1m0m1n | n 0, m 0} • S = {0, 1} 0, e / 0 1, e / 1 e, e / e e, e / $ q1 q2 q0 input: 0n 1m 0m 1n stack: 0n 1m e, e / e q4 q3 q5 e, e / e e, $/ e 0, 1 / e 1, 0/ e
Example 5 L = {w: w has same number 0s and 1s} • S = {0, 1} Strategy: Stack keeps track of excess of 0s or 1s If at the end, stack is empty, number is equal e, e / $ e, $/ e q1 q3 q0 1, e / 1 0, e / 0 0, 1 / e 1, 0 / e
Example 5 L = {w: w has same number 0s and 1s} • S = {0, 1} Invariant: In every execution of the PDA: #1 - #0 on stack = #1 - #0 in input so far e, e / $ e, $/ e q1 q3 q0 1, e / 1 0, e / 0 0, 1 / e 1, 0 / e If w is not in L, it must be rejected
Example 5 L = {w: w has same number 0s and 1s} • S = {0, 1} Property: In some execution of the PDA: stack consists only of 0s or only of 1s (or e) e, e / $ e, $/ e q1 q3 q0 1, e / 1 0, e / 0 0, 1 / e 1, 0 / e If w is in L, some execution will accept
Example 5 L = {w: w has same number 0s and 1s} • S = {0, 1} e, e / $ e, $/ e q1 q3 q0 read stack w = 001110 1, e / 1 0, e / 0 0 $0 0 $00 0, 1 / e 1, 0 / e 1 $0 1 $ 1 $1 0 $
Example 6 L = {w: whas two 0-blocks with same number of 0s 01011, 001011001, 10010101001 01001000, 01111 allowed not allowed Strategy: Detect start of first 0-block Push 0s on stack Detect start of second 0-block Pop 0s from stack
Example 6 0, 1 0, e / 0 1 e q1 q0 0, e / 0 e 1 Detect start of first 0-block 2 Push 0s on stack 1 1 0, 1 1 0, 0/ e 3 Detect start of second 0-block 4 Pop 0s from stack
Example 6 L = {w: whas two 0-blocks with same number of 0s 0, e / e 1 2 1, e / e 0, e / 0 0, e / 0 e, e / $ 1, e / e q1 q0 q2 3 q3 1, e / e e, e / $ 0, e / e 0, e / e 1, e / e q4 1, e / e 1, e / e 1, e / e e, $/ e 1, e/ e q7 q6 q5 e, $/ e 4 0, 0/ e
CFGs and PDAs L has a context-free grammar if and only if it is accepted by some pushdown automaton. context-free grammar pushdown automaton
A convention • When we have a sequence of transitions like: • We will abbreviate it like this: q0 q1 x, a/ b e, e/ c e, e/ d • pop a, then push b, c, and d q0 q1 x, a/ bcd • replace a by bcd on stack
Converting a CFG to a PDA • Idea: Use PDA to simulate derivations A → 0A1A → BB →# A 0A1 00A11 00B11 00#11 input: PDA control: stack: 00#11 write start variable e, e / A $A 00#11 e, A / 1A0 $1A0 replace production in reverse 0#11 pop terminal and match 0, 0 / e $1A 0#11 e, A / 1A0 $11A0 replace production in reverse #11 pop terminal and match 0, 0 / e $11A #11 e, A / B $11B replace production in reverse
Converting a CFG to a PDA A → 0A1A → BB →# e, $ / e e, e / $A q1 q0 q2 e, A / 1A0 e, A / B e, B / # 0, 0 / e 1, 1 / e #, # / e A 0A1 00A11 00B11 00#11 CFG input 00#11 00#11 00#11 00#11 00#11 stack $A $1A0 $1A $11A0 $11A 00#11 00#11 00#11 00#11 00#11 $11B $11# $11 $1 $
General PDA to CFG conversion a, a / e e, A / ak...a1 for every terminal a for every production A → a1...ak e, e / $S e, $ / e q2 q1 q0