Answer for Exercises (Page 45, Theory of Computer cience: Definitions and Examples. 3 rd Ed.) Part A Multiple Choices Questions 1. B 2. B 3. D 4. C 5. D 6. D 7. D 8. C 9. D 10. A 11. A 12. C 13. C 14. B 15. C 16. B 17. A 18. A 19. D 20. D 21. B 22. D 23. B 24. C 25. C 26. A 27. D 28. A 29. D 30. C 31. B 32. D 33. C 34. A 35. B 36. B 37. B 38. A 39. A 40. A 41. A 42. B 43. C 44. C 45. D 46. D 1 P a g e
Part B tructured Questions 1. a) AB ABAB λabab λabab λabbab λabbbab λabbλab λabbλaab λabbλaab λabbλaabb λabbλaabλ abbaab b) AB AB λab ABAB λaab λabab λaab λabab λaaλ λaλab aa λaλab λaλaλ aa c) A B λ a A λ a A B A B a λ λ a λ d) (a*ab*)* 2 P a g e
2. b) Derivation Rule abc abc ababcc abc ababacc A ababcadcc A cad ababccddcc A cd c) The derivation tree corresponding to the preceding derivation is a b c a b c A c A d c d d) L(G) = {(ab) n c m d m c n n 0; m > 0} 3. a) Derivation Rule AB AB aabb A aab aaabbb A aab aa bbb A aa bb B aa bb ba B ba aabbba b) Derivation Rule AB AB AbBa B bba Abbaa B ba AABbbaa AB AAbBabbaa B bba AAb abbaa B AAABb abbaa AB AAA b abbaa B AAA b abbaa 3 P a g e
AAaAb b abbaa A aab AAaaAbb b abbaa A aab AAaa bb b abbaa A AaAbaa bb b abbaa A aab Aa baa bb b abbaa A a baa bb b abbaa A abaabbbabbaa c) A B a A b b a a A b A B A B b B a a A b A B b B a b a a A b a A b d) L(G) = {a n1 b n1 a nk b nk b m1 a m1 b ml a ml n i, m i > 0; k, l 0; k l} 4. a) AB aab aab aaab aaab aaab 4 P a g e
b) AB AAB AaB Aab aaab aaab 5. et notations {a 2i b i b*, i 0} or {(aa) i b j i > 0, j i} { } or {a 2i b j i > 0, j i} { }. {a n c + b 2n, n 0} or {a n c m (bb) n n 0, m > 0} or {a n c m b 2n n 0, m > 0}. {(ab) n (cd) m (ba) m (dc) n, n 1, m 0} {a n c m a p b p d m b n, m 1, n 0, p 1} {a k b j 0 < k j 2k} Descriptions The rules aab and generate an equal number of leading aa's and trailing B's. Each B is transformed into one or more b's. ince at least one application of the rule aab is necessary to generate b's, strings consisting solely of b's are not in the language. The language of the grammar is {(aa) i b j i > 0, j i} { } or we can write {a 2i b j i > 0, j i} { }. The recursion of rules abb generate an equal number of leading a's and trailing bb's. The recursion is terminated by an application of the rule A. Each A is transformed into one or more c's. The language of the grammar is {a n c m (bb) n n 0, m > 0} or we can write {a n c m b 2n n 0, m > 0}. Repeated applications of the recursive rule abdc produce a string of the form (ab) i (dc) i, i 0. The recursion is terminated by an application of the rule A. The A rules produce strings of the form (cd) j (ba) j, j 0. The language generated by the rules is {(ab) i (cd) j (ba) j (dc) i i 0, j 0}. Repeated applications of the recursive rule ab produce a string of the form a m b m, m 0. The recursion is terminated by an application of the rule A. The A rules produce strings of the form c n Ad n, n > 0. While the B rules produce strings of the form a i Bb i, i > 0. The language generated by the rules is {a m c n a i b i d n b m m 0; n, i > 0} The rule ab ab generates strings of the form a k B k, by applying the first option of the rewrite rule k times, or a k abb k by applying the second option to replace. The rule B b bb, applied k times to the strings generated above will eventually yield a k ab(b bb) k. This can be written as L = {a k b j 0 < k j 2k}. hortest string and 3 other strings aab aabb, aaaabb, aaaabbb c aacbbbb accbb acccbb abdc / cdba abcdbadc, abcdcdbabadc, ababcdcdbabadcdc cabd aacccabdddbb, acaabbdb, aacaabbdbb ab abb, abbb, aabb 5 P a g e
6. AB A aabb abb B cb c 7. The language consisting of the set of strings {a n b m c 2n+m m, n > 0} is generated by acc aacc A bac bc For each leading a generated by the rules, two c's are produced at the end of the string. The rule A rules generate an equal number of b's and c's. 8. This grammar can be constructed by first generating as many a's as c's to the left and right. ubsequently generate as many b's as c's in the middle. This gives us as many a's and b's as c's. Then we create possibly more c's. ac B Generate a n Bc n, n 0 by applying left option n times. B bbc C Use a n Bc n and this rule to generate a n b m Cc n+m, n, m 0. C cc Use a n b m Cc n+m and this rule to get a n b m c j c n+m, n, m, j 0}. This gives us the language L = {a n b m c j c n+m n, m, j 0}. Let i = j + n + m to get: {a n b m c i 0 n + m i}. 9. AAab λ or Ab λ A a λ A a aa aaa 10. LR L alb λ R bla λ 11. The grammar of this language is ab ba 12. An a can be specifically placed in the middle position of a string using the rules A aaa aab baa bab a The termination of the application of recursive A rules by the rule A a inserts the symbol a into the middle of the string. Using this strategy, the grammar aaa aab bba bbb a b A aaa aab baa bab a B aba abb bba bbb b generates all strings over fa; bg with the same symbol in the beginning and middle positions. If the derivation begins with an rule that begins with an a, the A rules ensure that an a occurs in the middle of the string. imilarly, the and B rules combine to produce strings with a b in the first and middle positions. 13. a) (a + b)*aa(a + b)* b) AaaA A aa ba λ 6 P a g e
14. a) a*bb b) Abb A aa λ 15. AA aba abba abbba abbλa abbλab abbλaλ abba A A a B a B b B λ b B λ 16. axb abbab abbabaxb abbabaabbb abbabaabbbaxb abbabaabbbabbab a X b a X b a X b b b a a b b b b a 17. U111 U 0U 1U λ 18. a) AB ABAB λabab λaabab λaabab λaabbab λaabλab λaabλaab λaabλaaab λaabλaaab λaabλaaabb 7 P a g e
λaabλaaabbb λaabλaaabbλ aabaaabb b) A B A B a A b B λ a A b B a A b B a λ a λ c) A B A B a A b B A B a b B a A b B λ a λ λ a λ d) (a*ab*)* 19. a) Analysis: the language will produce strings with exactly 3 a s and end with a. in between of the a s can have as many b s as we like. The pattern of the string will be : BaBaBa. b aa A ba ab B bb a b) Analysis: the language will produce strings with at least one a and one b. The pattern of the string will be : AbBBaA. a ba A bba ab B ab λ 20. a) aa A aa ab 8 P a g e
B ba b) aa aaa aaaa aaaab aaaaba aaaabab aaaababa aaaababaa aaaababa. 21. L(G 1 ) = {b m a n b m : n 0, m 0} 22. G1 = ({a, b, }, {a, b},, P) with P defined by: abb λ 23. a) ba bb bba failed. b) a aba abaa abab ababba ababb ba baa baaa baab baaba baababa baabab a b A b A a A a A a A b b b A a b A 24. a) a(a + b)*b b) aa*bb* or a + b + c) a*bba* d) (a* + ba*b)* 25. a aa A ba bb B cb 26. The language (a b)*aa(a b)*bb(a b)* is generated by G 1 : 1 a 1 b 1 aa A ab B ab bb bc C bd D ad bd 9 P a g e
G 2 generates the strings (a b)*bb(a b)*aa(a b)* G 2 : 2 a 2 b 2 be E bf F af bf ag G ah H ah bh A grammar G that generates (a b)*aa(a b)*bb(a b)* (a b)*bb(a b)*aa(a b)* can be obtained from G 1 and G 2. The rules of G consist of the rules of G1 and G2 augmented with 1 2 where is the start symbol of the composite grammar. The alternative in these productions corresponds to the in the definition of the language. While the grammar described above generates the desired language, it is not regular. The rules 1 2 do not have the form required for rules of a regular grammar. A regular grammar can be obtained by explicitly replacing 1 and 2 in the rules with the right-hand sides of the 1 and 2 rules. The rules of the new grammar are a1 b1 aa a2 b2 be The strategy used to modify the rules 1 2 is an instance of a more general rule modification technique known as removing chain rules. 27. a b aa A ab B ab bb ac C ad D ad bd 28. G 1 generates the strings (a b)*ab(a b)*ba(a b)* G 1 : 1 a 1 b 1 aa A bb B ab bb bc C ad D ad bd G 2 generates the strings (a b)*ba(a b)*ab(a b)* G 2 : 2 a 2 b 2 be E af F af bf ag G bh H ah bh Thus a grammar G that generates (a b)*ab(a b)*ba(a b)* (a b)*ba(a b)*ab(a b)* a1 b1 aa a2 b2 be 1 a 1 b 1 aa A bb B ab bb bc C ad 10 P a g e
D ad bd 2 a 2 b 2 be E af F af bf ag G bh H ah bh or G: a b aa be A bb B ab bb bc C ad D ad bd E af F af bf ag G bh H ah bh 29. b aa λ A ba ab B bb a 30. The language ((a )b(a ))* is generated by the grammar aa bb A bb B a bb This language consists of all strings over {a, b} in which every a is preceded or followed by a b. An a generated by the rule aa is followed by a b. An a generated by B a is preceded by a b. 31. When we start and we read a b then we are no closer to reading aba and we remain in the same state. However, if we read an a, we may encounter ba next, so we proceed to a state A, where we may not read ba next. If we read an a in this state A there still might follow Ba, so we remain in state A. On the other hand, when we read a B in state A, we end up in a state where we may not read an a because we have just read ab. If the character we read then is a b we are back at the initial state where the string to follow may not be aba. Of course, we are always allowed to stop reading and accept that word in any state. This yields the following grammar b aa A aa bb B b 32. G: b aa ac A ab B bb bd C b D ab string to follow may not be aba string to follow may not be ba string to follow may not be a 11 P a g e
33. G: a aa bd bg A bb B ab bc C ac D ae E ae bf F af G ag bh H ai I ai 34. The variables of the grammar indicate whether an even or odd number of ab's has been generated and the progress toward the next ab. The interpretation of the variables is Variable Parity Progress toward ab even none A even a B odd none C odd a The rules of the grammar are aa b A aa bb B ac bb C ac b A derivation may terminate with a -rule when a B or a C is present in the sentential form since this indicates that an odd number of ab's have been generated. 35. The objective is to construct a grammar that generates the set of strings over {a, b} containing an even number of a's or an odd number of b's. In the grammar, aoa bea bob aeb Ea aoa bea Oa aea boa Ob aob beb Eb aeb bob the Ea and Oa rules generate strings with an even number of a's. The derivation of a string with a positive even number of a's is initiated by the application of an rule that generates either Ea or Oa. The derivation then alternates between occurrences of Ea and Oa until it is terminated with an application of the rule Ea. In a like manner, the Ob and Eb rules generate strings with an odd number of b's. The string aab can be generated by two derivations; one beginning with the application of the rule aoa and the other beginning with aeb. 36. a) {a n b m b n n 0, m > 0} or we can write {a m b n n > m 0}. b) {a n b 2m d 2m a 2n n 0, m > 0} c) {a n a m b n n, m 0} 12 P a g e
37. a) (a + b + ) + b) The rules a and b allow the generation of leading a's or trailing b's in any order. Two leftmost derivations for the string aabb are a b ab ab aabb aabb c) The derivation trees corresponding to the derivations of (b) are a b b a a b a b 38. The regular expression for this language is a + b + b. 39. {a m a n b n m > 0, n 0} or we can write {a m b n m > n 0}. 40. 41. a) The language {a, b}* b) The set {a, b}*{a} c) The set {ba}*{b} d) The set {b*ab*} e) {a}*{b}{a}*{b}{a}*, the set of strings in *{a, b} containing exactly two b s f) The set of strings over {a, b} that are not palindromes but could be made into palindromes by changing one a to b or vice versa g) The set of even-length strings in {a, b}* h) The set of odd-length strings in {a, b}* a) ab b b) ab b b c) abb d) ab abb e) abb ab a f) aab aa; A aabb aab aa 13 P a g e
42. Table 1. Table 2. No. Language No. s CFG or RG 1. (a + b)* 6 aa A aa bb B bb λ RG 2. (a + b) + 5 Abb A aa λ 3. (a + b) 2 a b a b RG 4. (ab) 10 5. a*bb 4 6. aa*bb* 9 7. a*bba* 8 AaaA A aa ba λ aa A b a b ba A bb B ab bb λ aa A aa ba b 8. a(a + b)*b 1 a b λ RG 9. (a + b)*bb(a + b)* 7 a ba A bb B ab λ 10. (a + b)*aa(a + b)* 3 a b RG RG CFG RG RG RG RG 43. Table 3. Table 4. No. Language No. s CFG or RG Generate 3 possible strings 1. L = {ww R : w (a + b)*} 4 ab ba CFG ab, ba, aaba 2. L = {a n b 2m c n n, m > 0} 3 3. L = (bb*a + ab*b)* 5 4. Let L be the language of all strings over {a, b} that have the same number of a s as b s ba ab λ A ba a B bb b X Y X axb ab Y bya ba AC B A a C c B bbb bb 5. L = {a n b n n > 0} {b n a n n > 0} 1 aa bb CFG 2 RG CFG CFG 14 P a g e