Sie sind auf Seite 1von 13

Solutions for Homework Five, CSE 355 1.

(7.1, 10 points) Let M be the PDA dened by (q0 , a, ) = {[q0 , A]} Q = {q0 , q1 , q2 } = {a, b} = {A} F = {q1 , q2 } (q0 , , ) = {[q1 , ]} (q0 , b, A) = {[q2 , ]} (q1 , , A) = {[q1 , ]} (q2 , b, A) = {[q2 , ]} (q2 , , A) = {[q2 , ]} a) Describe the language accepted by M . b) Give the state diagram of M . c) Trace all computations of the strings aab, abb, aba in M . d) Show that aabb, aaab L(M ). Solution: a) The PDA M accepts the language {ai bj | 0 j i}. Processing an a pushes A onto the stack. Strings of the form ai are accepted in state q1 . The transitions in q1 empty the stack after the input has been read. A computation with input ai bj enters state q2 upon processing the rst b. To read the entire input string, the stack must contain at least j As. The transition (q2 , , A) = [q2 , ] will pop any As remaining on the stack. b) The state diagram of M is
a/A M: q0 / A/ q1

bA/ q2 bA/, A/

c) The computations of aab in M are as follows: State q0 q1 String aab aab Stack State q0 q0 q1 q1 String aab ab ab b Stack A A

State q0 q0 q0 q1 q1 q1

String aab ab b b b b

Stack A AA AA A State q0 q0 q1 q1 State q0 q0 q1 q1 String abb bb bb bb String aba ba ba ba Stack A A Stack A A

State q0 q0 q0 q2 q2

String aab ab b

Stack A AA A

The computations of abb in M are as follows: State q0 q1 String abb abb Stack State q0 q0 q2 String abb bb b Stack A

The computations of aba in M are as follows: State q0 q1 String aba aba Stack State q0 q0 q2 String aba ba a Stack A

d) To show that the string aabb and aaab are in L(M ), we trace a computation of M that accepts these strings. State q0 q0 q0 q2 q2 String aabb abb bb b Stack A AA A State q0 q0 q0 q0 q2 q2 q2 String aaab aab ab b Stack A AA AAA AA A

2.

(7.2, 10 points) Let M be the PDA in Example 7.1.3.


b/B, a/A M: q0 / bB/, aA/ q1

a) Give the transition table of M . b) Trace all computations of the strings ab, abb, abbb in M . c) Show that aaaa, baab L(M ). d) Show that aaa, ab L(M ). / Solution: 2

Q = {q0 , q1 } a) = {a, b} = {A, B} F = {q1 } b) The computations of ab in M are as follows: State q0 q1 String ab ab Stack State q0 q0 q1 String ab b b Stack A A

(q0 , b, ) = {[q0 , B]} (q0 , a, ) = {[q0 , A]} (q0 , , ) = {[q1 , ]} (q1 , b, B) = {[q1 , ]} (q1 , a, A) = {[q1 , ]}

State q0 q0 q0 q1 State q0 q0 q1 State q0 q0 q0 q0 q1 String abb bb bb String abb bb b State q0 q0 q0 q1 q1 String abbb bbb bb b

String ab b Stack A A Stack A BA BBA BBA String abbb bbb bb bb b Stack A BA BBA BBBA BBBA

Stack A BA BA

The computations of abb in M are as follows: State q0 q1 State q0 q0 q0 q1 q1 String abb abb String abb bb b b Stack Stack A BA BA A

The computations of abbb in M are as follows: State q0 q1 String abbb abbb Stack State q0 q0 q1 Stack A BA BBA BBA BA String abbb bbb bbb Stack A A State q0 q0 q0 q0 q0 q1 Stack A BA BA A

State q0 q0 q0 q0 q1 q1

String abbb bbb bb b b

c) To show that the string aaaa and baab are in L(M ), we trace a computation of M that accepts these strings.

State q0 q0 q0 q1 q1 q1

String aaaa aaa aa aa a

Stack A AA AA A

State q0 q0 q0 q1 q1 q1

String baab aab ab ab b

Stack B AB AB B

d) To show that the string aaa and ab are not in L(M ), we trace all computations of these strings in M , and check whether none of them accepts these strings. We have listed all the computations of ab in (b), and none of them accepts it. Now we trace all computations of aaa in M State String Stack State String Stack q0 aaa q0 q0 aaa aa A q1 q1 aaa aa A a q1 State q0 q0 q0 q1 q1 String aaa aa a a Stack A AA AA A State q0 q0 q0 q0 q1 String aaa aa a Stack A AA AAA AAA

Since none of the computations above is accepted, we have aaa is not in M .

3.

(7.3, 10 points) Construct PDAs that accept each of the following languages.

a) {ai bj | 0 i j} b) {ai cj bi | i, j 0} c) {ai bj ck | i + k = j} d) {w | w {a, b} and w has twice as many as as bs} e) {ai bi | i 0} a b f) {ai bj ck | i = j or j = k} g) {ai bj | i = j} h) {ai bj | 0 i j 2i} i) {ai+j bi cj | i, j > 0} j) The set of palindromes over {a, b} Solution: 4

a/A

bA/, b/ / c/ q1 bA/ / q2 cB/ / q2

a)

M: a/A

q0

b)

M:

q0 a/A

q1 bA/, b/B

c)

M:

q0

q1

d) The pushdown automaton dened by the transitions (q0 , , ) = {[q1 , C]} (q1 , a, A) = {[q2 , A]} (q1 , a, C) = {[q2 , C]} (q1 , b, B) = {[q3 , B]} (q1 , b, C) = {[q3 , C]} (q1 , a, B) = {[q1 , ]} (q1 , b, A) = {[q1 , ]} (q1 , , C) = {[q5 , ]} (q2 , , ) = {[q1 , A]} (q3 , , ) = {[q4 , B]} (q4 , , ) = {[q1 , B]} accepts strings that have twice as many as as bs. A computation begins by pushing a C onto the stack, which serves as a bottom-maker throughout the computation. The stack is used to record the relationship between the number of as and bs scanned during the computation. The stacktop will be a C when the number of as processed is exactly twice the number of bs processed. The stack will contain n As if the automaton has read n more as than bs. If n more bs than as have been read, the stack will hold 2n Bs. When an a is read with an A or C on the top of the stack, an A is pushed onto the stack. This is accomplished by the transition to q2 . If a B is on the top of the stack, the stack is popped removing one b. If a b is read with a C or B on the stack, two Bs are pushed onto the stack. Processing a b with an A on the stack pops the A. The lone accepting state of the automation is q5 . If the input string has twice as many as as bs, the transition to q5 pops the C, terminates the computation, and accepts the string.

bA/, aB/ M: q0 / q1 aA/A, aC/C /A /B q4 /B a/A M: q0 / q2 /


bB/B

q2

bC/C q3

C/ q5 bA/ q1

/ q3 b/ bA/ c/ / q2

e)
a/A M: q0 / q3

a/

q1

q4 b/B bA/

q5 cB/ b/

f)

a/ a/A M: q0 /

q1 A/ q3

b/

q2

g)

A/

h) The language L = {ai bj | 0 i j 2 i} is generated by the context-free grammar S aSB | B bb | b The B rule generates one or two bs for each a. A pushdown automaton M that accepts L uses the as to record an acceptable number of matching bs on the stack. Upon processing an a, the computation nondeterministically pushes one or two As onto the stack. The transitions

of M are (q0 , a, ) = {[q1 , A]} (q0 , , ) = {[q3 , ]} (q0 , a, ) = {[q0 , A]} (q0 , b, A) = {[q2 , ]} (q1 , , ) = {[q0 , A]} (q2 , b, A) = {[q2 , ]} The states q2 and q3 are the accepting states of M . The null string is accepted in q3 . For a nonnull string ai bj L, one of the computations will push exactly j As onto the stack. The stack is emptied by processing the bs in q2 . The state diagram of the PDA is
a/A M: q0 / q3 a/A bA/ / q1 / aA/, bB/ q1 a/A /A bA/ q2 bA/ cA/ q2 q1

i)

M:

q0

a/A, b/B

j)

M:

q0

a/, b/, /

(7.7, 10 points) Let L be the language {w {a, b} | w has a prex containing more bs than as.}. For example, baa, abba, abbaaa L, but aab, aabbab L. / a) Construct a PDA that accepts L by nal state. b) Construct a PDA that accepts L by empty stack. Solution:
a/A, bA/ a/, b/ bB/ q2 a/, b/ bB/ q2 /B

4.

a)

M:

q0

q1 a/A, bA/

b)

M:

q0

/B

q1

5. (7.12, 20 points) Use the technique of Theorem 7.3.1 to construct a PDA that accepts the languages of the Greibach normal form grammar.
S aABA | aBB A bA | b B cB | c Solution: The state diagram for the extended PDA obtained from the grammar is Q = {q0 , q1 } = {a, b, c} = {A, B} F = {q1 }
a/ABA, a/BB

(q0 , a, ) = {[q1 , ABA], [q1 , BB]} (q1 , b, A) = {[q1 , A], [q1 , ]} (q1 , c, B) = {[q1 , B], [q1 , ]}
bA/, bA/A, cB/, cB/B

q0

q1

6.

(7.15, 20 points) Let M be the PDA in Example 7.1.1. Q = {q0 , q1 } = {a, b, c} = {A, B} F = {q1 } (q0 , a, ) = {[q0 , A]} (q0 , b, ) = {[q0 , B]} (q0 , c, ) = {[q1 , ]} (q1 , a, A) = {[q1 , ]} (q1 , b, B) = {[q1 , ]}
M: q0 a/A, b/B c/ aA/, bB/ q1

a) Trace the computation in M that accepts bbcbb. b) Use the technique from Theorem 7.3.2 to construct a grammar G that accepts L(M ). c) Give the derivation of bbcbb in G. Solution: State q0 q0 q0 q1 q1 q1 String bbcbb bcbb cbb bb b Stack B BB BB B

a)

b) First we add transitions to M as follows.

(q0 , a, ) = {[q0 , A]} (q0 , a, A) = {[q0 , AA]} (q0 , a, B) = {[q0 , AB]} (q0 , b, ) = {[q0 , B]} (q0 , b, A) = {[q0 , BA]} (q0 , b, B) = {[q0 , BB]} (q0 , c, ) = {[q1 , ]} (q0 , c, A) = {[q1 , A]} (q0 , c, B) = {[q1 , B]} (q1 , a, A) = {[q1 , ]} (q1 , b, B) = {[q1 , ]} Second the rules of the equivalent grammar G and the transition from which they were constructed are presented in Table 1. [q0 , bbcbb, ] [q0 , bcbb, B] [q0 , cbb, BB] [q1 , bb, BB] [q1 , b, B] [q1 , , ] S q0 , , q1 b q0 , B, q1 bb q0 , B, q1 q1 , B, q1 bbc q1 , B, q1 q1 , B, q1 bbcb q1 , , q1 q1 , B, q1 bbcb q1 , B, q1 bbcbb q1 , , q1 bbcbb

c)

7. (7.17, 20 points) Use the pumping lemma to prove that each of the following languages is not context-free.
a) {ak | k is a perfect square} b) {ai bj ci dj | i, j 0} c) {ai b2i ai | i 0} d) {ai bj ck | 0 < i < j < k < 2i} e) {wwR w | w {a, b} } f) The set of nite-length prexes of the innite string abaabaaabaaaab ban ban+1 b Solution: a) Assume that language L consisting of strings over {a} whose lengths are a perfect square is context-free. By the pumping lemma, there is a number k such that every string in L with length k or more can be written uvwxy where 9

Transition (q0 , a, ) = {[q0 , A]} (q0 , a, A) = {[q0 , AA]}

(q0 , a, B) = {[q0 , AB]}

(q0 , b, ) = {[q0 , B]} (q0 , b, A) = {[q0 , BA]}

(q0 , b, B) = {[q0 , BB]}

(q0 , c, ) = {[q1 , ]} (q0 , c, A) = {[q1 , A]} (q0 , c, B) = {[q1 , B]} (q1 , a, A) = {[q1 , ]} (q1 , b, B) = {[q1 , ]}

Rule S q0 , , q1 q0 , , q0 a q0 , A, q0 q0 , , q1 a q0 , A, q1 q0 , A, q0 a q0 , A, q0 q0 , A, q0 a q0 , A, q1 q0 , A, q1 a q0 , A, q0 q0 , A, q1 a q0 , A, q1 q0 , B, q0 a q0 , A, q0 q0 , B, q0 a q0 , A, q1 q0 , B, q1 a q0 , A, q0 q0 , B, q1 a q0 , A, q1 q0 , , q0 b q0 , B, q0 q0 , , q1 b q0 , B, q1 q0 , A, q0 b q0 , B, q0 q0 , A, q0 b q0 , B, q1 q0 , A, q1 b q0 , B, q0 q0 , A, q1 b q0 , B, q1 q0 , B, q0 b q0 , B, q0 q0 , B, q0 b q0 , B, q1 q0 , B, q1 b q0 , B, q0 q0 , B, q1 b q0 , B, q1 q0 , , q0 c q1 , , q0 q0 , , q1 c q1 , , q1 q0 , A, q0 c q1 , A, q0 q0 , A, q1 c q1 , A, q1 q0 , B, q0 c q1 , B, q0 q0 , B, q1 c q1 , B, q1 q1 , A, q0 a q1 , , q0 q1 , A, q1 a q1 , , q1 q1 , B, q0 b q1 , , q0 q1 , B, q1 b q1 , , q1 q0 , , q0 q1 , , q1

q0 , A, q0 q1 , A, q0 q0 , A, q1 q1 , A, q1 q0 , B, q0 q1 , B, q0 q0 , B, q1 q1 , B, q1

q0 , A, q0 q1 , A, q0 q0 , A, q1 q1 , A, q1 q0 , B, q0 q1 , B, q0 q0 , B, q1 q1 , B, q1

Table 1: The rules of the equivalent grammar G and the transition from which they were constructed (Problem 7.15 (b))

10

(i) length(vwx) k (ii) v and x are not both null (iii) uv i wxi y L for all i 0. The string z = ak must have a decomposition uvwxy that satises the preceding conditions. Consider the length of the string uv 2 wx2 y obtained by pumping uvwxy. length(z) =length(uv 2 wx2 y) =lenght(uvwxy) + length(v) + length(x) =k2 + length(v) + length(x) k2 + k <(k + 1)2 Since the length of z is greater than k2 but less than (k + 1)2 , we conclude that z L and / that L is not context-free. b) Assume that language L = {ai bj ci dj | i, j 0} is context-free. By the pumping lemma, there is a number k such that every string in L with length k or more can be written uvwxy where (i) length(vwx) k (ii) v and x are not both null (iii) uv i wxi y L for all i 0. The string z = ak bk ck dk must have a decomposition uvwxy that satises the preceding conditions. Consider the string uv 2 wx2 y obtained by pumping uvwxy. Since v and x are not both null by condition (ii), we have that vwx contains at least one terminal. Without loss of generality, assume vwx contains a terminal which is either a or c (similar argument for the case that the terminal is either b or d). Condition (i) requires the length of vwx to be at most k. This implies that vwx is a string that cannot contain both a and c types of terminal. Thus uv 2 wx2 y increases the number of either as or cs, but not the both, compared with uvwxy. Hence uv 2 wx2 y L, a contradiction. We conclude that L is not context-free. / c) Assume that language L = {ai b2i ai | i 0} is context-free. By the pumping lemma, there is a number k such that every string in L with length k or more can be written uvwxy where (i) length(vwx) k (ii) v and x are not both null (iii) uv i wxi y L for all i 0. The string z = ak b2k ak must have a decomposition uvwxy that satises the preceding conditions. Consider the string uv 2 wx2 y obtained by pumping uvwxy. Since by assumption uv 2 wx2 y L, we must have that the union of v and x contains both a type and b type of terminals. Otherwise it only increases one type of terminal while keeping the other the same, thereby no longer in L. Further more, condition (i) requires the length of vwx to be at most k. This implies that the substring vwx of z cannot contain as from both sides of the bs substring. Therefore uv 2 wx2 y only increases the number of as either preceding or after bs, but not both. Hence uv 2 wx2 y L, and consequently, L is not context-free. / 11
2

d) Assume that language L = {ai bj ck | 0 < i < j < k < 2i} is context-free. By the pumping lemma, there is a number k such that every string in L with length k or more can be written uvwxy where (i) length(vwx) k (ii) v and x are not both null (iii) uv i wxi y L for all i 0. Without loss of generality, we assume k > 2, since we can always increase k while maintaining the three conditions above. Then the string z = ak bk+1 ck+2 is in L and must have a decomposition uvwxy that satises the preceding conditions. Consider the string uv k wxk y obtained by pumping uvwxy. Condition (i) requires the length of vwx to be at most k. This implies that vwx is a string containing only one type of terminal or the concatenation of either a and b types, or b and c types. If c is not contained in vwx, pumping v and x only increases the number of as or bs. Thus the new string cannot keep the number of as less than the number of bs which is less than the number of cs, i.e. k + 2. If c is contained in vwx, then a is not contained in vwx. Thus uv k wxk y would have at least (k + 2) + (k 1) = 2k + 1 number of cs while keeping the number of as the same, i.e. k. Hence uv k wxk y L, and consequently, / L is not context-free. e) Assume that language L = {wwR w | w {a, b} } is context-free. By the pumping lemma, there is a number k such that every string in L with length k or more can be written uvwxy where (i) length(vwx) k (ii) v and x are not both null (iii) uv i wxi y L for all i 0. The string z = (ak bk )(ak bk )R (ak bk ) = ak b2k a2k bk must have a decomposition uvwxy that satises the preceding conditions. By condition (ii), we have v and x have at least one terminal. Without loss of generality, assume that at least one a is in v or x (similar argument for the case of at least one b in v or x ). Condition (i) requires the length of vwx to be at most k. This implies that the substring vwx of z cannot contain as from both sides of b2k . If the as in the substring vwx of z are before b2k , then uv 2 wx2 y increases the number of as before b2k while keeping the number of as after b2k the same as 2k. Hence uv 2 wx2 y is no long in L = {wwR w | w {a, b} }. If the as in the substring vwx of z are after b2k , we have / uv 2 wx2 y L by similar argument. Therefore L is not context-free. f) Assume that the language L consisting of prexes of string abaabaaabaaaab ban ban+1 b is context-free and let k be the number specied by the pumping lemma. Consider the string z = abaab bak b, which is in the language and has length greater than k. Thus z can be written uvwxy where (i) length(vwx) = k 12

(ii) v and x are not both null (iii) uv i wxi y L for all i 0. To show that the assumption that L is context-free produces a contradiction, we examine all possible decomposition of z that satisfy the conditions of the pumping lemma. By (ii), one or both of v and x must be nonnull. In the following argument we assume that v = . Case 1: v has no bs. In this case, v consists solely of as and lies between two consecutive bs. That is, v occurs in z in a position of the form ban bai vaj ban+2 b where i + length(v) + j = n + 1. Pumping v produces an incorrect number of as following ban b and, consequently, the resulting string is not in the language. Case 2: v has two or more bs. In this case, v contains a substring ban b. Pumping v produces a string with two substrings of the form ban b. No string with this property is in L. Case 3: v has one b. Then v can be written ai baj and occurs in z as ban1 bani van+1j b Pumping v produces the substring ban1 bani ai baj ai baj an+1j b = ban1 ban baj+i ban+1 b , which cannot occur in a string in L. Regardless of its makeup, pumping any nonnull substring v of z produces a string that is not in the language L. A similar argument shows that pumping x produces a string not in L whenever x is nonnull. Since one of v or x is nonnull, there is no decomposition of z that satises the requirements of the pumping lemma and we conclude that the language is not context-free.

13

Das könnte Ihnen auch gefallen