Pda Exercises
Pda Exercises
We start with standard problems on building pda for a given language, ending with more
challenging problems.
. . . to be continued . . .
1. Construct pushdown automata for the following languages. Acceptance either by
empty stack or by final state.
(a)
(b)
(c)
(d)
(e)
(f)
(g)
(h)
(i)
(j)
{ an bm an | m, n N }
{ an bm cm | m, n N }
{ai bj ck | i, j, k N, i > j}
{ai bj ck | i, j, k N, i + j = k}
{ai bj ck | i, j, k N, i + k = j}
{ an bm | n m 2n }
PAL = {w {a, b} | mir(w) = w}
{w1 cw2 c cwk cx | x, w1 , . . . , wk {a, b} , k N, x = mir(wj ) for some j }
{w {a, b} | #a (w) = #b (w)}, #a (w) represents the number of as in w
{w {a, b} | #a (w) = 2 #b (w)}
2. More languages.
(a)
(b)
(c)
(d)
{w
{w
{w
{w
Here is bin(i) {0, 1} the binary representation (without leading zeros) of the
number i. Eg. bin(11) = 1011 and mir(bin(12)) = 0011.
4. Give a one-state pda for the coin-change language from the chapter:
Lex = { x =y | x {1, 2} , y {5} , |x|1 + 2 |x|2 = 5 |y|5 },
Pda and cfg.
5. pushdown automaton A is specified by
A = ({q0 , q1 }, {a, b}, {Z, X}, , qin , Z, ), where contains the following transitions:
(q0 , a, Z) 7 (q0 , ),
(q0 , b, X) 7 (q1 , ),
6. Given two cf languages K and L, there is a pda A such that Lf (A) = K and
Le (A) = L (where the subscripts f and e refer to the final state and empty stack
acceptance respectively).
Deterministic automata.
7. Consider the languages of Exercise 1. Which of these are accepted by deterministic
automata? Give an automaton where possible. What is the acceptance type?
8. Show formally that the language { an bm | n m 2n } is not deterministically
context-free.
Unfortunately, when applying the operation pre from the chapter, we obtain
{ an bm | n < m 2n } which still is context-free. We need a different closure
property of the deterministic context-free languages to tackle this problem.
Closure Properties.
13. For each of the following operations, construct a finite state transducer that performs
that operation (for a fixed regular R).
(a) intersection with regular language R
(b) right quotient with regular language R:
for K, R , K/R = { x | xy K for some y R }
(c) concatenation with regular language R
14. Write each of the operations of the previous exercise as a combination of full trio
operations, i.e., as a composition of morphism, inverse morphism, and intersection
with regular languages.
15. The family CF is closed under inverse morpisms. This can be shown using pda, either
directly as in the chapter (Lemma 7) or via finite state transductions, as in the notes
(Lemma 11). Verify that this closure property also holds in the deterministic case.
Extending the model.
16. A two-way pushdown automaton may move on its input tape in two directions.
As usual for two-way automata we assume that the begin and end of the input
tape is marked by special symbols. In this way the automaton can recognize those
positions.
Describe a two-way pda for each of the following languages.
(a) { an bn cn | n N }
(b) { ww | w {a, b} }
(easy)
(nice puzzle)
17. In the case of finite state automata, the two-way model is equivalent to the usual
one-way automaton. Find a proof of this result.
18. Stack automata are pda that may inspect their stack. The chapter states: stack
automata that do not read input during inspection of the stack are equivalent to
pdas. Verify this fact.
Research.
19. Investigate the notion of recursive finita state automata. How do you formalize this
notion. How do you define determinism here? What is the relation to (deterministic)
pushdown automata?
http://www.liacs.nl/home/hoogeboo/praatjes/tarragona/
Convention. When we use a diagram to depict a pda, we use our own private conventions. A single instruction (p, a, A, q, ) is given as an arc from state p to state q labelled
by a; A/. Label a; +A represents a set of instructions pushing A on top of the stack
(p, a, X, q, AX) for all X , while label a represents the set (p, a, X, q, X) of instructions
that ignore the stack.
a; A/
a; +A
a
p
q
p
q
p
q
Solutions
1a The pda is depicted by the following diagram. Formally, it consists of the following components: state set Q = {1, 2, 3, 4}, alphabet = {a, b}, stack alphabet = {Z, A}, initial state 1, final state 4, initial stack symbol Z, and instructions (1, a, X, 1, AX), (1, , X, 2, X), (2, b, X, 2, X), (2, , X, 3, X) (3, a, A, 3, ),
(3, , Z, 3, ), for all X . Acceptance either by final state or by empty stack.
a; +A
1
a; A/
; Z/
b; Z/Z
b0
a; A/
a; +A
a; A/AA
o
b; A/A
b; A/A
b
a; A/
b; A/A
; Z/
Study such a diagram with care. Is it deterministic? Are the special cases (like the
empty string) handled properly? How are the good strings accepted, but also how
are the bad ones rejected?
1e We give three different pda accepting this language. All variants use the fact that
the language can be writen as { ai bi bj cj | i, j N }.
b; A/
a; +A
1
; Zin /Zin
b;C/
a;+C
c; A/
b; +A
; Zin /
c;D/
; Zin /AC
2
b;+D
a; A/AB
; A/
b; B/
;Zin /
b; C/CD
; C/
c; D/
1f For each a we put nondeterministically one or two symbols onto the stack. Each b
removes one symbol.
b; A/
a; +A
1
; Zin /
a; +AA
1i Push for as and pop for bs, or more precisely put the number #a (v)#b (v) onto the
stack, where v is the prefix of the input read. Note that this number can become
negative. We can either use two different pushdown symbols, or we can use the
states to store the sign.
Determinism. Before each letter we test whether the contents of the stack represents
zero. In that case we pass through a final state to signal possible acceptance (in
case the end of the input was reached).
Initial state 0, final state f , initial pushdown symbol Z (for zero). Instructions:
(0, , Z, f, Z), (0, , P, 1, P ), (0, , N, 1, N ), (f, , Z, 1, Z), (testing zero)
(1, a, P, 0, P P ), (1, a, N, 0, ), (1, b, P, 0, ), (1, b, N, 0, N N ) (counting).
2b When w is not of the form xmir(x) then either its length is odd, or there is a k such
that the k-th symbol from the word differs from the k-th symbol from the rear. De
language of strings of odd length is regular, and hence accepted by a pda. Since
pda languages are closed under union it suffices to construct a pda for the language
{ x1 y2 z | x, y, z {a, b} , |x| = |z|, 1 , 2 {a, b}, 1 6= 2 }.
5
This automaton goes through the following steps: (1) read the input, stacking for
each letter read the symbol X under the initial symbol Z. (2) read a letter, storing
its value on top of the stack, (3) ignore a random number of symbols from the input,
(4) read a letter that is different from the one stored on the stack, otherwise block,
(5) read input symbols, taking an X from the stack for each letter. This can be
achieved using a single state. Acceptance by empty stack (of course).
(p, , Z) 7 (p, ZX)
(p, a, Z) 7 (p, A),
(p, , A) 7 (p, A),
(p, a, B) 7 (p, ),
(p, , X) 7 (p, )
( {a, b}),
(p, b, Z) 7 (p, B),
(p, , B) 7 (p, B)
(p, b, A) 7 (p, ),
( {a, b}).
( {a, b}),
a, b
a, b, c
a; +A
b; +A
a, b
a; Zin /Zin
b; Zin /Zin
a; A/
b; A/
c
; A/A
L4 = { x1 ay1 cx2 by2 | x1 , x2 , y1 , y2 {a, b} , |x1 | = |x2 | },
a; +A
b; +A
a; A/
b; A/
a, b
a, b
b; Zin /Zin
and finally L5 equals L4 but with strings of the form x1 by1 cx2 ay2 .
The pda that we have constructed can be joined with instructions (qin , , Zin ) 7
(q, Zin ) starting from a new initial state qin .
2d When w does not consist of two copies of the same word, then either w has odd
length, or the first half of w does not equal the second half, so there is a k such
that the letter on position k differs from the letter at position k after the middle.
Unlike the previous exercise we cannot recognize the middle, and we have to guess
that point.
Thus we find the language
Ke = { x1 1 y1 x2 2 y2 | x1 , y1 , x2 , y2 {a, b} ,
|x1 | = |x2 |, |y1 | = |y2 |, 1 , 2 {a, b}, 1 6= 2 }.
In this way we cannot recognize the language. We can if we realize that that it is
not necessary to determibe middle, but that it suffices to check that between the s
are as many letters as before and after: |y1 x2 | = |x1 | + |y2 | replaces the requirement
|x1 | = |x2 |, |y1 | = |y2 |.
3 B = B1 B2 B3 met
B1 = { 1x01n $0n 1mir(x)1 | x {0, 1} , n 0 },
B2 = { 1n $0n 1 | n 1 }, en
B3 = { 0$1 }.
From this a pda for B can be constructed in a straightforward way.
5 Using the standard construction for obtaining a cfg for a given pda, we find 15
productions: (in all cases: p, r Q, where Q = {q0 , q1 }).
S [q0 Zp],
[q0 Zq0 ] a, [q0 Zr] a[q0 Xp][pZr],
[q0 Xr] a[q0 Xp][pXr], [q0 Xq1 ] b,
[q1 Xq1 ] b, [q1 Zr] a[q0 Zr].
In a bottom-up way we determine the productive nonterminals, those nonterminals
that can derive a (terminal) string:
(1) [q0 Zq0 ], [q0 Xq1 ], [q1 Xq1 ], (because of productions [q0 Zq0 ] a, etc.)
(2) S, [q1 Zq0 ] (because of [q1 Zq0 ] a[q0 Zq0 ]).
Thus, e.g., [q0 , Z, q1 ] is not productive. This means that there is no computation of
the form (w, q0 , Z) `A (, q1 , ), from q0 to q1 the pda removes Z from the stack,
reading some string w {a, b} .
7