210 likes | 397 Views
Chapter 5 Pushdown Automata. 5.1 Pushdown Automata. In this section, we shall introduce pushdown automata as machines to accept context-free languages. For a context-free grammar G, there is an equivalent pushdown automaton M to recognize the language generated by the grammar G.
E N D
Chapter 5 Pushdown Automata 5.1 Pushdown Automata In this section, we shall introduce pushdown automata as machines to accept context-free languages. For a context-free grammar G, there is an equivalent pushdown automaton M to recognize the language generated by the grammar G. Here the pushdown automaton is nondeterministic, the deterministic version accepts only a subset of all CFL's.
(input tape) 0 0 0 1 0 0 0 q1 q1 q1 (stack) Descriptionof pushdown automaton (PDA) APDA has an input tape, a finite control, and a stack. step1: read 0, push 0, and move to right step2: read 0, push 0, and move to right 0 0 (finite control)
There are2 types of moves: According to an input symbol, current state, and top symbol of the stack, a decision is made, then the input head is moved ahead one symbol. Next move is decided by the current state and the top symbol of the stack only. The input symbol is not used. Hence the input head is not moved at all after the decision is made. This type of move is called ε-move that allows the PDA to manipulate the stack without reading input symbol.
Languages accepted by PDA's There are two ways to accept inputs: • The PDA accepts an input if after reading the input and the machine empty its stack. • The set of inputs accepted by the PDA is the language accepted by empty stack. • Some states of the PDA are final states. The PDA accepts an input if the machine enters a final state. • The set of inputs accepted by the PDA is the language accepted by final state.
Definition of PDA's A pushdown automaton M is a system (Q, Σ, Γ, δ, q0, Z0 , F), where • Q is a finite set of states; • Σ is an alphabet called the input alphabet; • Γ is an alphabet, called the stack alphabet; • q0 in Q is the initial state; • Z0 in Γ is a particular stack symbol called the start symbol; • F, a subset of Q, is the set of final states; • mapping δ: Q×(Σ∪{ε}) ×Γ → finite subset of Q×Γ*.
0 0 1 0 0 0 1 0 δ B G G q1 q1 R R Moves: δ(q, a, Z) = {(p1, r1), (p2, r2), ..., (pm, rm)}, where q and pi, 1≦i ≦ m, are states, a is in , Z is a stack symbol, and ri is in Γ*, 1≦i ≦ m, means PDA in state q, reading input symbol a with top symbol Z on the stack, can enter any of the state pi and replaces top symbol Z by ri, and advances reading head one symbol. Example: δ(q1, 0, G) = {(q1, BG)}
0 0 1 0 0 0 1 0 δ B G G q1 q1 R R -Moves: δ(q, , Z) = {(p1, r1), (p2, r2), ..., (pm, rm)}, where q and pi, 1≦i ≦ m, are states, Z is a stack symbol, and ri is in Γ*, 1≦i ≦ m, means PDA in state q, without reading any input symbol with top symbol Z on the stack, can enter any of the state pi and replaces top symbol Z by ri, and the reading head remains at the same place. Example: δ(q1, , G) = {(q1, BG)}
If δ(q, a, Z) = contains (p, ), we say (q, a, Z) M(p, , ). 0 0 1 0 0 0 1 0 δ B For the above case, we have that (q1, 010, GR) M(q1, 01, BGR). G G q1 q1 R R Let be the reflexive and transitive closure of M. Instantaneous descriptions: Example: δ(q1, 0, G) = {(q1, BG)}
L(M) = {| (q0, , Z0) (p, , ) for some p in F and in Γ*} N(M) = {| (q0, , Z0) (p, , ) for some p in Q} Languages accepted by final states: Languages accepted by empty stack:
Example: N(M) = {wwR| w in (0+1)*}, where M = ({q1, q2}, {0, 1}, {Z0, 0, 1}, δ, q1, Z0, {}), and δis as follows: δ(q1, 0, Z0) ={(q1, 0Z0)}, δ(q1, 1, Z0) ={(q1, 1Z0)}, δ(q1, 0, 0) ={(q1, 00), (q2, )}, δ(q1, 0, 1) ={(q1, 01)}, δ(q1, 1, 0) ={(q1, 10)}, δ(q1, 1, 1) ={(q1, 11), (q2, )}, δ(q1, , Z0) = {(q2, )}, δ(q2, 0, 0) ={(q2, )}, δ(q2, 1, 1) ={(q2, )}, δ(q2, , Z0) ={(q2, )}.
Example: L(M) = {wwR| w in (0+1)*}, where M = ({q1, q2, q3}, {0, 1}, {Z0, 0, 1}, δ, q1, Z0, {q3}), and δis as follows: δ(q1, 0, Z0) ={(q1, 0Z0)}, δ(q1, 1, Z0) ={(q1, 1Z0)}, δ(q1, 0, 0) ={(q1, 00), (q2, )}, δ(q1, 0, 1) ={(q1, 01)}, δ(q1, 1, 0) ={(q1, 10)}, δ(q1, 1, 1) ={(q1, 11), (q2, )}, δ(q1, , Z0) = {(q3, Z0)}, δ(q2, 0, 0) ={(q2, )}, δ(q2, 1, 1) ={(q2, )}, δ(q2, , Z0) ={(q3, Z0)}.
Equivalence of acceptance by final states and empty stack Theorem 1 L =L(M1) for some PDA M1 L = N(M2) for some PDA M2. Proof Let M1 = (Q, Σ, Γ, δ, q0, Z0, F), and L(M1) = L. Construct M2= (Q{qe, q0'}, Σ, Γ{Z'}, δ', q0, Z' , {}), where δ'(q0', , Z') ={(q0, Z0Z')}, δ'(q, a, X) = δ(q, a, X), for all q in Q, a in Σ{}, X in Γ. For all q in F, and X in Γ {Z'}, δ'(q, , X) contains (qe, ). For all X in Γ {Z'}, δ'(qe, , X) contains (qe, ).
Theorem 2 L = N(M2) for some PDA M2 L = L(M1) for some PDA M1. Proof Let M2 = (Q, Σ, Γ, δ, q0, Z0, {}), and N(M2) = L. Construct M1= (Q{qf, q0'}, Σ, Γ{Z'}, δ', q0', Z', {qf}), where δ'(q0', , Z') ={(q0, Z0Z')}, δ'(q, a, X) = δ(q, a, X), for all q in Q, a in Σ{}, X in Γ. For all q in Q, δ'(q, , Z') contains (qf, ).
5.2 Equivalence of PDA's and CFL's Theorem 3 L is a context-free language L = N(M) for some PDA M. Proof Assume is not in L and L = L(G) for some CFG in Greiback normal form G = (V, T, P, S). Let M = ({q}, T, V, δ, q, S , {}), where δ(q, a, A) contains (q, ), whenever Aa is in P. We have that δ(q, a, A) contains (q, ), whenever Aa is in P.
Example L ={anbn | n≧1} is a context-free language . L = L(G), G = (V, T, P, S) a CFG in Greiback normal form, where S aSB, S aB, B b. Let M = ({q}, T, V, δ, q, S , {}), a PDA, where δ(q, a, S) contains (q, SB), where SaSBis in P, δ(q, a, S) contains (q, B), where SaB is in P, δ(q, b, B) contains (q, ), where Bb is in P.
a a b b a a a a a a a a b b b b b b b b δ(q, a, S) := (q, SB) S q q q q S B B δ(q, b, B) := (q, ) δ(q, a, S) := (q, B) δ(q, b, B) := (q, ) B B q
Theorem 4 L = N(M) for some PDA M L is a context-free language. Proof Let M = (Q, T, Γ,δ, q0, Z0, {}) be the PDA. Let G = (V, T, P, S) be a context-free grammar, where V is the set of elements of the form [q, A, p], q and p in Q, and A in Γ, plus the new symbol S.
P is the set of productions: S [q0, Z0, q] for each q in Q; [q, A, qm+1] a[q1, B1, q2][q2, B2, q3]… [qm, Bm, qm+1]for each q, q1, q2, …,qm+1 in Q, a in {}, and A, B1, B2, …, Bm in Γ, if (q, a, A) contains (q1, B1B2…Bm), for m>0. If m = 0, the production is [q, A, q1] a.
The key concept of the construction is the following: A leftmost derivation in G of a sentence x is a simulation of the PDA M when the input is x. The variables that appear in any step of a leftmost derivation in G correspond to the symbols on the stack of M at a time when M has scanned as much of the input as the grammar has already generated. In another word, [q, A, p] derives x if and only if x causes M to erase the symbol A from its stack by some sequence of moves beginning in state q and ending in state p.
Example L ={anbn | n≧1} is a context-free language . Let PDA M = ({q}, {a, b}, {S}, δ, q, S , {}), where δ(q, a, S) = {(q, Sb), (q, b)} δ(q, b, b) = {(q, )} Construct G = (V, T, P, S) from M as follows: V = {S, [q, S, q]} P contains the following productions: S [q, S, q] [q, S, q] a[q, S, q][q, b, q] [q, b, q] b [q, S, q] a[q, b, q]