Saturday 26 January 2013

TOC Chapter 7

Chapter 7. Pushdown Automata

In the earlier chapters, we have considered the simplest type of automaton, namely, the FSA. We have seen that a FSA has finite amount memory and hence cannot accept type 2 languages like {anbn|n ≥ 1}. In this chapter, we consider a class of automata, the pushdown automata, which accept exactly the class of context-free (type 2) languages. The pushdown automaton is a finite automaton with an additional tape, which behaves like a stack. We consider two ways of acceptance and show the equivalence between them. The equivalence between context-free grammars (CFG) and pushdown automata is also proved.

7.1. The Pushdown Automaton

Let us consider the following language over the alphabet Σ = {a, b, c}: L = {anbmcn|n, m ≥ 1}. To accept this, we have an automaton which has a finite control and the input tape which contains the input. Apart from these, there is an additional pushdown tape which is like a stack of plates placed on a spring. Only the top most plate is visible. Plates can be removed from top and added at the top only. In the following example, we have a red plate and a number of blue plates. The machine is initially is q0 state and initially a red plate is on the stack. When it reads a it adds a blue plate to the stack and remains in state q0. When it sees the b, it changes to q1. In q1, it reads b’s without manipulating the stack. When it reads a c it goes to state q2 and removes a blue plate. In q2 state, it proceeds to read c’s and whenever it reads a c it removes a blue plate. Finally, in q2 state without reading any input it removes the red plate. The working of the automaton can be summarized by the following table.
StateTop plateInput
abc
q0redadd blue plate remain in state q0
blueadd blue plate remain in state q0go to q1
q1red
blueremain in state q1go to q2 remove the plate
q2redwithout waiting for input remove red plate
blueremain in q2 remove the plate

7.2. Equivalence between Acceptance by Empty Store and Acceptance by Final State

In this section, we show that acceptance by empty store and acceptance by final state are equivalent.

Theorem 7.1

L is accepted by a PDA M1 by empty store, if and only if L is accepted by a PDA M2 by final state.
Proof (i) Let L be accepted by a PDA M2 = (K, Σ, Γ, δ2, q0, Z0, F) by final state. Then construct M1 as follows: . We add two more states q0′ and qe and one more pushdown symbol X0. q0′ is the new initial state and X0 is the new initial pushdown symbol. qe is the erasing state.
δ mappings are defined as follows:
  1. δ1(q0′, ε, X0) contains (q0, Z0X0)
  2. δ1(q, a, Z) includes δ2(q, a, Z) for all qK, a ∊ Σ ∪ {ε}, Z ∊ Γ
  3. δ1(qf, ε, Z) contains (qe, ε) for qfF and Z ∊ Γ ∪ {X0}
  4. δ1(qe, ε, Z) contains (qe, ε) for Z ∊ Γ ∪ {X0}
The first move makes M1 go to the initial ID of M2 (except for the X0 in the pushdown store). Using the second set of mappings M1 simulates M2. When M2 reaches a final state using mapping 3, M1 goes to the erasing state qe and using the set of mappings 4, entire pushdown store is erased.
If w is the input accepted by M2, we have .
This can happen in M1 also. .
M1 accepts w as follows:
Equation 7.1
Hence, if w is accepted by M2, it will be accepted by M1. On the other hand, if M1 is presented with an input, the first move it can make is using mapping 1 and once it goes to state qe, it can only erase the pushdown store and has to remain in qe only. Hence, mapping 1 should be used in the beginning and mapping 3 and 4 in the end. Therefore, mapping 2 will be used in between and the sequence of moves will be as in Equation (7.1).
Hence, which means and w will be accepted by M2.
(ii) Next, we prove that if L is accepted by M1 by empty store, it will be accepted by M2 by final state. Let M1 = (K, Σ, Γ, δ1, q0, Z0, φ). Then, M2 is constructed as follows:
M2 = (K ∪ {q0′, qf}, Σ, Γ ∪ {X0}, δ2, q0′, X0, {qf})

Two more states q0′ and qf are added to the set of states K. q0′ becomes the new initial state and qf becomes the only final state. One more pushdown symbol X0 is added which becomes the new initial pushdown symbol. The δ mappings are defined as follows:
  1. δ2(q0′, ε, X0) contains (q0, Z0X0)
  2. δ2(q, a, Z) includes all elements of δ1(q, a, Z) for qK, a ∊ Σ ∪ {ε},Z ∊ Γ
  3. δ2(q, ε, X0) contains (qf, X0) for each qK
Mapping 1 makes M2 go to the initial ID of M1 (except for the X0 in the pushdown store). Then using mapping 2, M2 simulates M1. When M1 accepts by emptying the pushdown store, M2 has X0 left on the pushdown store. Using mapping 3, M2 goes to the final state qf.
The moves of M2 in accepting an input w can be described as follows:
(q0′, w, X0) ⊢ (q0, w, Z0 X0) ⊢* (q, ε, X0) ⊢ (qf, ε, X0)

It is not difficult to see that w is accepted by M2, if and only if w is accepted by M1.
It should be noted that X0 is added in the first part for the following reason. M2 may reject an input w by emptying the store and reaching a nonfinal state. If X0 were not there, M1 while simulating M2 will empty the store and accept the input w. In the second part, X0 is added because for M2 to make the last move and reach a final state, a symbol in the pushdown store is required. Thus, we have proved the equivalence of acceptance by empty store and acceptance by final state in the case of non-deterministic PDA.

7.3. Equivalence of CFG and PDA

In this section, we show the equivalence of CFG and non -deterministic PDA.

Theorem 7.2

If L is generated by a CFG, then L is accepted by a non-deterministic PDA by empty store.
Proof Let L be generated by G = (N, T, P, S). Then M = ({q}, T, NT, δ, q, S, φ) can be constructed to accept L(G). δ is defined as follows.
If Aα is a rule in P, δ(q, ε, A) contains (q, α). For each α in T, δ(q, a, a) contains (q, ε).
To see that L(G) = N(M), we note that M simulates a leftmost derivation in G. Suppose
Equation 7.2

is a leftmost derivation in G. M uses an ε-move if the top pushdown symbol is a non-terminal and reads a true input symbol if the top pushdown symbol is the same as that input symbol and pops it off.
(i) We show if wL(G), then wN(M).
Consider the derivation in Equation (7.2). Suppose αi can be written as xiγi where xi is the terminal prefix string and γi begins with a non-terminal.
We show by induction on i, the number of steps of derivations in G, that


Problems and Solutions

1.Let L = {aibjck|i, j, k ≥ 1 and i + j = k}
  1. Find a PDA (which accepts via final state) that recognizes L.
  2. Find a PDA (which accepts via empty stack) that recognizes L.
Solution.
  1. Hint: Push a’s and b’s into the stack and match them with each c and clear.
    The PDA M = ({q0, q1, q2, q3}, {a, b, c}, {a, b, $}, δ, q0, $, {q3})
    Transitions are:
    δ(q0, a, $) contains (q0, a $)
    δ(q0, a, a) contains (q0, aa)
    δ(q0, b, a) contains (q1, ba)
    δ(q1, b, b) contains (q1, bb)
    δ(q1, c, b) contains (q2, ε)
    δ(q2, c, a) contains (q2, ε)
    δ(q2, c, b) contains (q2, ε)
    δ(q2, ε, $) contains (q3, ε)
  2. The machine M above is the required PDA, q3 is the final state and all the transitions remain the same as for (i). This machine accepts L via both by empty stack and final state. Note that this is a DPDA.
2.Find a PDA for L = {x ∊ {a, b, c}*||x|a + |x|b = |x|c}.
Solution.This example is slightly different from the previous one as one can have interleaving occurrences of a’s, b’s and c’s.
The transitions are:
δ(q0, a, $) contains (q0, a$)
δ(q0, b, $) contains (q0, b$)
δ(q0, c, $) contains (q0, c$)
δ(q0, a, a) contains (q0, aa)
δ(q0, a, b) contains (q0, ab)
δ(q0, b, a) contains (q0, ba)
δ(q0, b, b) contains (q0, bb)
δ(q0, c, c) contains (q0, cc)
δ(q0, a, c) contains (q0, ε)
δ(q0, b, c) contains (q0, ε)
δ(q0, c, a) contains (q0, ε)
δ(q0, c, b) contains (q0, ε)
δ(q0, ε, $) contains (qf, ε)
qf - final state.
3.Give an example of a finite language that cannot be recognized by any one-state PDA that accepts via final state.
Solution.L = {abc}
Reason
If abc is accepted in state q0, then ab, a are also accepted in q0.
4.Let L = {anbncmdm|n, m ≥ 1}. Find a PDA that accepts L.
Solution.M = ({q0, q1, q2, q3, q4}, {a, b, c, d}, {a, c, $}, δ, q0, $, {q4})
Transitions are:
δ(q0, a, $) contains (q0, a$)
δ(q0, a, a) contains (q0, aa)
δ(q0, b, a) contains (q1, ε)
δ(q1, b, a) contains (q1, ε)
δ(q1, c, $) contains (q2, c$)
δ(q2, c, c) contains (q2, cc)
δ(q2, d, c) contains (q3, ε)
δ(q3, d, c) contains (q3, ε)
δ(q3, ε, $) contains (q4, ε)
This machine accepts L by empty stack. Taking q4 as the final state L is accepted by final state also.
5.Design a PDA to accept the following language.
  1. The set of all strings of balanced parentheses. i.e., each left parenthesis has a matching right parentheses and pairs of matching parentheses are properly nested.
  2. The set of all non-palindromes over {a, b}.
Solution.
  1. M = ({q0}, {(,)}, {x, $}, δ, q0, $, φ)
    δ is given by:
    δ(q0, (, $) contains (q0, X$)
    δ(q0, (, X) contains (q0, X X)
    δ(q0,), X) contains (q0, ε)
    δ(q0, ε, $) contains (q1, ε)
    The acceptance is by empty stack.
  2. M = {(q0, q1, q2), {a, b}, {0, 1, $}, δ, q0, $, φ}
    • For one symbol strings, accept by emptying the stack and going to q2.
    δ(q0, a, $) contains (q2, ε)
    δ(q0, b, $) contains (q2, ε)
    • First half: Push 0 when a is seen and 1 when b is seen.
    δ(q0, a,$) contains (q0, 0$)
    δ(q0, a, 0) contains (q0, 00)
    δ(q0, a, 1) contains (q0, 01)
    δ(q0, b, $) contains (q0, 1$)
    δ(q0, b, 0) contains (q0, 10)
    δ(q0, b, 1) contains (q0, 11)
    • Guessing the middle of the string:
    Change state: For strings with odd length, a symbol is read without changing the stack.
    δ(q0, a, 0) contains (q1, 0)
    δ(q0, a, 1) contains (q1, 1)
    δ(q0, b, 0) contains (q1, 0)
    δ(q0, b, 1) contains (q1, 1)
    For strings with even length
    Pop 0 when a is seen, pop 1 when b is seen.
    δ(q0, a, 0) contains (q1, ε)
    δ(q0, b, 1) contains (q1, ε)
    • Keep popping for (a, 0) or (b, 1) combination
    δ(q1, a, 0) contains (q1, ε)
    δ(q1, b, 1) contains (q1, ε)
    • If 0 is not found when a is seen or 1 is not found when b is seen, then pop the symbol and change state to q2. Then continue to clear out all the symbols including $.
    δ(q1, a, 1) contains (q2, ε)
    δ(q1, b, 0) contains (q2, ε)
    δ(q2, a, 0) contains (q2, ε)
    δ(q2, b, 0) contains (q2, ε)
    δ(q2, a, 1) contains (q2, ε)
    δ(q2, b, 1) contains (q2, ε)
    • (q2, ε, $) contains (q2, ε)
6.Find a PDA for
L = {x0y1| x, y ∊ {0, 1}*, |x| = |y|}
Solution.
M =({q0, q1, q2}, {0, 1}, {X, $}, δ, q0, $, φ}

where δ is given by:
δ(q0, 0, $) contains (q0, X$)
δ(q0, 0, X) contains (q0, X X)
δ(q0, 1, $) contains (q0, X$)
δ(q0, 1, X) contains (q0, X X)
δ(q0, 0, $) contains (q1, $)
δ(q0, 0, X) contains (q1, X)
δ(q1, 0, X) contains (q1, ε)
δ(q1, 1, X) contains (q1, ε)
δ(q1, 1, $) contains (q2, ε)
Acceptance is by empty stack. We can also look at it as acceptance by final state by taking q2 as the final state.

Exercises

1.Design a PDA which accepts strings of the form 1*0n1n and one which accepts strings which contain twice as many zeros as ones.
2.Find a PDAs which accepts set of strings composed of zeros and ones which are:
  1. not of the form ww
  2. of the form 0n1n or 0n12n
3.Show that the language {0m 1n|mn ≤ 2m} is context-free by giving a PDA that accepts it.
4.Let G = (N, Σ, P, S) be a CFG with P = {SaSA|aAA|b, AbBBB, B → b}. Construct a PDA which accepts L(G) by final state.
5.Let M be a PDA such that M = (K, Σ, Γ, δ, q0, Z, F) where K = {q0, q1, q2, q3}, Σ = {a, b}, Γ = {A, Z}, F = φ
δ: δ (q0, a, Z) = (q0, AZ)
δ(q0, b, A) = (q1, ε)
δ(q0, a, A) = (q3, ε)
δ(q1, ε, Z) = (q2, ε)
δ(q3, ε, Z) = (q0, AZ)
Construct a CFG accepting L(M).
6.Construct a DPDA for the following languages.
  1. {anbn|n ≥ 1}
  2. {anbmcn + m|n, m > 0}
7.Show that if L is a CFL and εL, then there is a PDA M accepting L by final state such that M has at most two states and makes no ε-moves.
8.A pushdown transducer (PDT) M is an 8-tuple (K, Σ, Γ, Δ, δ, q0, Z0, F), where all the symbols have the same meaning as for a PDA except that Δ is a finite output alphabet and δ is a mapping from K × (Σ ∪{ε}) × Γ} to finite subsets of K × Γ* × Δ*.
Any configuration of M will be a 4-tuple (q, x, a, y) where q, x, a are as in any PDA and y is the output string at this point of time. The output happens on a separate tape. It never goes back on this tape for any change. The machine may write a string as part of each instruction.
Design an automaton that changes infix arithmetic expression to postfix expression.
9.The PDA’s defined in this chapter make moves on the input tape oneway only. By allowing it to move two-ways on the input tape by having end markers on the input tape, one can define a two-way PDA or 2PDA. Show that the following languages are recognized by a 2PDA.
  1. {anbncn|n ≥ 1}
  2. {ww|w ∊ {0, 1}*}
10.For a PDA M, let there exist a constant k such that M can never have more than k symbols on its pushdown stack at any time. Show that L(M) is a regular language.
11.Find a grammar generating L(M), M = ({q0, q1, q2}, {0, 1}, {Z0, A}, δ, q0, Z0, {q2}) where δ is given by:
δ(q0, a, Z0) = {(q1, AZ0)}
δ(q0, a, A) = {(q1, AA)}
δ(q1, a, A) = {(q0, AA)}
δ(q1, ε, A) = {(q2, AA)}
δ(q2, b, A) = {(q2, ε)}
12.A PDA M = (Q1, Σ, Γ, δ, q0, z0, F) is said to be a single-turn PDA if whenever and |γ2| < |γ1|, then |γ3| ≤ |γ2|. That is, once the stack starts to de crease in height, it never increases in height.
Show that a language is generated by a linear CFG if and only it is accepted by a single-turn PDA.

No comments:

Post a Comment