Строительный блокнот  Automata methods and madness 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 [ 81 ] 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175

to a PDA that accepts the same language by empty stack.

* Exercise 6.3.3: Convert the PDA P = {{p,g},{0,1},{A ,Zo},(S,g, Zo) to a CFG, if 5 is given by:

1. 5iqA,Za) = {{q,XZo)}.

2. 5iq,l,X) = {{q,XX)}.

3. S{q,0,X) = {{p,X)}.

4. S{q,e,X) = {iq,e)}.

5. 5Cp,l,X) = {(p,e)}.

6. 6(p,0,Zo) = {{g,Zo)}.

Exercise 6.3.4: Convert the PDA of Exercise 6.1.1 to a context-free grammar.

Exercise 6.3.5: Below are some context-free languages. For each, devise a PDA that accepts the language by empty stack. You may, if you wish, first construct a grammar for the language, and then convert to a PDA.

a) {a b c2f + ) I n > 0, m > 0}.

b) {afrJc* I г = 2j or J = 2k}. ! c) {О ! I n < m < 2n}.

*! Exercise 6.3-6: Show that if P is a PDA, then there is a one-state PDA Pi such that N{Pi) = N{P).

! Exercise 6.3.7: Suppose we have a PDA with s states, t stack symbols, and no rule in which a replacement stack string has length greater than u. Give a tight upper bound on the number of variables in the CFG that we construct for this PDA by the method of Section 6.3.2.

6.4 Deterministic Pushdown Automata

While PDAs are by definition allowed to be nondeterministic, the deterministic subcase is quite important. In particular, parsers generally behave like deterministic PDAs, so the class of languages that can be accepted by these automata is interesting for the insights it gives us into what constructs are suitable for use in programming languages. In this section, we shall define deterministic PDAs and investigate some of the things they can and cannot do.



6.4.2 Regular Languages and Deterministic PDAs

The DPDAs accept a class of languages that is between the regular languages and the CFLs. We shall first prove that the DPDA languages include all the regular language.s.

Theorem 6.17: If Lis a regular language, then L = L{P) for some DPDA P.

6.4.1 Definition of a Deterministic PDA

Intuitively, a PDA is deterministic if there is never a choice of move in any situation. These choices are of two kinds. I£ 6(q, a,X] contains more than one pmr, then surely the PDA is nondeterministic because we can choose among these pairs when deciding on the next move. However, even if S{q, a, X) is always a singleton, we could still have a choice between using a real input symbol, or making a move on e. Thus, we define a PDA P - ((3,S!,r,J, qiOjZcF) to be deterministic (a deterministic PDA or DPDA), if and only if the following conditions are met:

1. 6{q,a,X) has at most one member for any g in Q, о in E or a = e, and X in Г,

2. If 5{q,a,X) is nonempty, for some a in E, then 5{q,e>X) must be empty.

Example 6.16 : It turns out that the language Lbjr oi Example 6.2 is a CFL that has no DPDA. However, by putting a center-marker с in the middle, we can make the language recognizable by a DPDA. That is, we can recognize the language icwr = {vjcw ш is in (0 -f-1)*} by a deterministic PDA.

The strategy of the DPDA is to store Os and Is on its stack, until it sees the center marker c. it then goes to another state, in which it matches input symbols against stack symbols and pops the stack if they match. If it ever finds a nonmatch, it dies; its input cannot be of the form wcw. If it succeeds in popping its stack down to the initial symbol, which marks the bottom of the stack, then it accepts its input.

The idea is very much like the PDA that we saw in Fig. 6.2. However, that PDA is nondeterministic, because in state q it always has the choice of pushing the next input symbol onto the stack or making a transition on e to state qi; i.e., it has to guess when it has reached the middle. The DPDA for shown as a transition diagram in Fig. 6.11.

This PDA is clearly deterministic. It never has a choice of move in the same state, using the same input and stack symbol. As for choices between using a real input symbol or e, the only e-transition it makes is from qi to q2 with Zq at the top of the stack. However, in state qj, there are no other moves when Zq is at the stack top. □





с , Zq /Zq £ , Zq /Z q

c, 0/ 0

с , 1 / 1

Figure 6.11: A delerniuiistic PDA accepting L-mcwr

PROOF: Essentially, a DPDA can simulate a deterministic finite automaton. The PDA keeps some stack symbol Zo on its stack, because a PDA has to have a stack, but really the FDA ignores its stack and just uses its state. Formally, let A = (0,Е,5л,Со,Р) be a DFA. Construct DPDA

P = {0,S,{Zo},5p,№,Zo,F)

by defining ep{q,a,Zo) = {(p, Zo)} for all states p and q in Q, such that 6л{д,а)р.

We claim that {qo,w, Zo) H (p, e, Zq) if and only if л(о,гу) - p. That is,

P simulates A using its state. The proofs in both directions are easy inductions on and we leave them for the reader to complete. Since both A and P accept by entering one of the states of F, we conclude that their languages are the same. □

Ji we want the DPDA to accept by empty stack, then we find that our language-recognizing capability is rather limited. Say that a language L has the prefix property if there are no two different strings x and у m L such that X is a prefix of y.

Example 6.18: The language icr of Example 6.16 has the prefix property. That is, it is not possible for there to be two struigs wcw and xcx, one of which is a prefix of the other, unless they are the same string. To see why, suppose vjcw is a prefix of xcx, and w Ф x. Then m must be shorter than X. Therefore, the с in wcw comes in a position where xcx has a 0 or 1; it is a position in the first x. That point contradicts the assumption that wcw is a prefix of xcx.

On the other hand, there are some very simple languages that do not have the prefix property. Consider {O}*, i.e., the set of all strings of Os. Clearly,

0, Zq/OZq

1, Zq/IZo 0, 0/0 0

0 , 1/0 1

1,0/10 0 , 0/ e

1,1/11 1 , 1 / £



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 [ 81 ] 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175