CS:4330 Theory of Computation Spring 2018 Context-Free Languages Pushdown Automata Haniel Barbosa
Readings for this lecture Chapter 2 of [Sipser 1996], 3rd edition. Section 2.2.
Finite automaton 1 / 13
Pushdown automaton 2 / 13
A new type of computational model Pushdown automata, PDA, are a new type of computation model PDAs are like NFAs but have an extra component called a stack The stack provides additional memory beyond the finite amount available in the control The stack allows PDA to recognize some nonregular languages 3 / 13
PDA and CFG PDAs are equivalent in specification power with CFGs This is useful because it gives us two options for proving that a language is context-free: 1. construct a CFG that generates the language, or 2. construct a PDA that recognizes the language Note Some CFLs are more easily described in terms of their generators, whereas others are more easily described in terms of their recognizers. 4 / 13
Terminology Writing a symbol on the stack is referred to as pushing the symbol Removing a symbol from the stack is referred to as popping the symbol. All accesses to the stack may be done only at the top. In other words, a stack is a last-in first-out storage device. 5 / 13
Benefits of the stack A stack can hold an unlimited amount of information A PDA can recognize a language like {0 n 1 n n 0} because it can use the stack to remember the number of 0s it has seen Informal algorithm 1. Read symbols from the input. As each 0 is read, push it into the stack. 2. As soon as an 1 is read, pop a 0 off the stack for each 1 read. 3. If input finishes when stack becomes empty, accept; if stack becomes empty while there is still input or input finishes while stack is not empty, reject. 6 / 13
PDA formalization Formal definition of PDA is similar to an NFA, except for the stack PDA may use different alphabets for input and stack, which we will denote by Σ and Γ, respectively Nondeterminism allows PDA to make transitions on empty input. Hence we will use Σ ɛ = Σ {ɛ} and Γ ɛ = Γ {ɛ} PDA transition functions has domain Q Σ ɛ Γ ɛ, where Q is the set of states Since a PDA can write on the stack while performing nondeterministic transitions the range of the PDA transition function is P(Q Γ ɛ ) The transition function of a PDA is δ : Q Σ ɛ Γ ɛ P(Q Γ ɛ ) 7 / 13
PDA formalization Formal definition of PDA is similar to an NFA, except for the stack PDA may use different alphabets for input and stack, which we will denote by Σ and Γ, respectively Nondeterminism allows PDA to make transitions on empty input. Hence we will use Σ ɛ = Σ {ɛ} and Γ ɛ = Γ {ɛ} PDA transition functions has domain Q Σ ɛ Γ ɛ, where Q is the set of states Since a PDA can write on the stack while performing nondeterministic transitions the range of the PDA transition function is P(Q Γ ɛ ) The transition function of a PDA is δ : Q Σ ɛ Γ ɛ P(Q Γ ɛ ) Definition (PDA) A PDA is a 6-tuple (Q, Σ, Γ, δ, q 0, F), in which Q is its set of states, Σ its input alphabet, Γ its stack alphabet, δ its transition function, q 0 the start state, and F Q the set of accept states. 7 / 13
Example The PDA P 1 that recognizes the language {0 n 1 n n 0} is such that Q = {q 1,q 2,q 3,q 4 } Σ = {0,1} Γ = {0,$} F = {q 1,q 4 } with δ : Input 0 1 ɛ Stack 0 $ ɛ 0 $ ɛ 0 $ ɛ q 1 {(q 2,$)} q 2 {(q 2,0)} {(q 3,ɛ)} q 3 {(q 3,ɛ)} {(q 4,ɛ)} q 4 8 / 13
Example The PDA P 1 that recognizes the language {0 n 1 n n 0} is such that Q = {q 1,q 2,q 3,q 4 } Σ = {0,1} Γ = {0,$} F = {q 1,q 4 } with δ s.t. ɛ,ɛ $ start q 1 q 2 0,ɛ 0 1,0 ɛ q 4 ɛ,$ ɛ q 3 1,0 ɛ 8 / 13
PDA computation A PDA P = (Q, Σ, Γ, δ, q 0, F) accepts a word w Σ where w = w 1,..., w m, with w i Σ ɛ if there exists a sequence where r i Q, with r 0 = q 0, (r 0,s 0 ) (r 1,s 1 ) (r 2,s 2 ) (r m,s m ) s i Γ (representing the stack), with s 0 = ɛ, q m F, (r i+1,b) δ(r i,w i,a) where s i = at and s i+1 = bt for some a,b Γ ɛ and t Γ 9 / 13
Transition diagrams of PDA Transition diagrams for PDAs are similar to the ones used for finite automata To show how it manipulates the stack we write a,b c to signify that the machine: 1. is reading a from the input 2. may replace the symbol b on the top of the stack with the symbol c where a,b,c may be ɛ If a = ɛ, the machine may take this transition without reading any symbol from the input If b = ɛ, the machine may make this transition without popping any symbol from stack If c = ɛ, the machine may make this transition without pushing any pushing any symbol on the stack 10 / 13
Note The formal definition of a PDA contains no explicit mechanism for testing the empty stack The PDA P 1 in the previous example tests whether stack is empty by initially placing a special symbol $ on the stack If it ever sees $ again on the stack, it knows that the stack is effectively empty. We use this convention to test whether the stack is empty. 11 / 13
Another example: palyndromes The PDA P 2 that recognizes the language {ww R w {a 1,..., a n } } is such that 12 / 13
Another example: palyndromes The PDA P 2 that recognizes the language {ww R w {a 1,..., a n } } is such that ɛ,ɛ $ a 1,ɛ a... 1 a n,ɛ a n start q 1 q 2 ɛ,ɛ ɛ q 4 ɛ,$ ɛ q 3 a 1,a 1 ɛ... a n,a n ɛ 12 / 13
Another example The PDA P 3 that recognizes the language {a i b j c k i,j,k 0 i = j i = k} is such that The PDA reads and push a s on the stack When this is done, it can mach a s b s or c s Since the machine does not know in advance whether a s are matched with b s or c s, nondeterminism helps Using nondeterminism the machine can guess: it branches into two possibilities, one for each guess If either of these branches matches the input, that branch accepts the word, as does the machine. 13 / 13
Another example The PDA P 3 that recognizes the language {a i b j c k i,j,k 0 i = j i = k} is such that b,a ɛ c,ɛ ɛ start q 1 ɛ,$ ɛ q 3 q 4 ɛ,ɛ $ ɛ,ɛ ɛ q 2 ɛ,ɛ ɛ ɛ,ɛ ɛ ɛ,$ ɛ q 5 q 6 q 7 a,ɛ a b,ɛ ɛ c,a ɛ 13 / 13