1 / 12

Midterm (Models of Computation, Fall, 2000)

Type 0. Type 1. Type 2. Type 3. ID: ____Key_________ Name:_Answers_________. Midterm (Models of Computation, Fall, 2000). Answer:. Grammar Type. 1. What is the type of each of the following grammars? Put the name (G i ) of each grammar in the proper column of the

cameo
Télécharger la présentation

Midterm (Models of Computation, Fall, 2000)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Type 0 Type 1 Type 2 Type 3 ID: ____Key_________ Name:_Answers_________ Midterm (Models of Computation, Fall, 2000) Answer: Grammar Type 1. What is the type of each of the following grammars? Put the name (Gi) of each grammar in the proper column of the grammar type table at right. G1: S  abcB | Sa B   G2: S  ABC |  AB  ab bC bc G3: S  AAA A  a |  G4: S  ABC AB  a aC  c BC  b G1 G2 G3 G4 G2 G1 G3 2. Answer each of the following questions; (a) What is the language of the following grammar G? Answer: {aibj | 0  i  3, 0  j  2 } (b) Show a context-sensitive grammar G’ such that L(G) = L(G’), i.e., the new grammar generates the same language. (c) Show a regular grammar G’ such that L(G) = L(G’). G: S  AAABB A  a |  B  b |  Answer for both (b) and (c): S   | a | aa | aaa | b | bb | ab | abb | aab | aabb | aaab | aaabb Notice that by definition, this grammar is regular and context-sensitive.

  2. 3. What is the language generated by the following context-sensitive grammar? Write the language in terms of set property, and briefly explain how the members of the language is generated. S CBSa | CBa Ba  ba Cb cbCc cc BC CB Bb bb Answer: the language is { cibiai | i > 0 } The grammar generates the language as follows; By S it generates a string of pattern (CB)iai, for i > 0. The rule BC CB moves all B’s to the right, and all C’s to the left. The rightmost B with a to its right changes to b by rule Ba  ba, and by Bb  bb all the B’s are changed to b. When the rightmost C sees b to its right, it is changed to c By rule Cb  cb, and by rule Cc cc, all the remaining C’s are changed to c. (This grammar uses the same idea of the type 1 grammar in the handout which generates the language { aibici | i > 0 }.)

  3. 4. Show the result of the set operation (L1 - L3 - L4)  L2 with the languages below, and briefly describe your reasoning which leads to your answer. L1 = {a, b, c}*, L2 = { aibjck | i,j,k  0 }, L3 = { x | x  {a, b, c}* and in x, the number of a’s and number of b’s are different}, and L4 = { x | x  {a, b, c}* and in x, the number of b’s and number of c’s are different}. Answer: { aibici | i  0 } Reasoning: Since L1 = {a, b, c}*, i.e., it contains every string in {a, b, c}*, by subtracting L3 and L4 from L1, all strings, where the number of a’s and number of b’s are different, or the number of b’s and number of c’s are different, are eliminated. This result intersected with L2 will give the answer. ,

  4. a A B b A S a S b A B 5.Draw a syntax diagram (syntax flow graph) for the language generated by the following grammar. S  aSb | AB A  aA | a B  Bb | b 6. (a) Construct a regular grammar which generates the language of unsigned numbers defined in Pascal programming Language. You should show how you got your answer. (b) Construct a regular expression which denotes the language of unsigned numbers defined in Pascal programming language. You should show the procedure that you took to get your answer. B Answers: (see next two slides)

  5. 0,1,…,9 0,1,…,9 0,1,…,9 E C . B 0,1,…,9 D A +, - E S G 0,1,…,9 start 0,1,…,9 To each state we assign a nonterminal symbol as above, and construct the following regular grammar: F S  0A | 1A . . . 9A A  0A | 1A . . .9A | .B | ED |  B  0C | 1C . . .9C C  0C | 1C . . .9C | ED |  D  +G | -G | 0F | 1F . . .9F G  0F | 1F . . .9F F  0F | 1F . . .9F |  0,1,…,9 Answer (a): This DFA is a copy of the key answer of problem 3-(c) of HW #3. ** Notice that E is a terminal symbol.

  6. 0,1,…,9 0,1,…,9 0,1,…,9 0,1,…,9 C . B A 0,1,…,9 0,1,…,9 A S S (b) start (a) start Answer (b): Regular expression: r = r1 + r2 + r3, where r1, r2, and r3 are computed as follows, where d = 0+1 . . .+9. (1) Computing a regular expression r1 for the language accepted by state A: We only need the transition in figure (a) below. Clearly, r1 = dd* is the regular expression. (2) Computing regular expression r2 for the language accepted by state C: We only need the transition in figure (b) above. We can directly read off the following regular expression from this graph. r2 = dd*. dd*

  7. (3) Computing regular expression r3 for the language accepted by State F: 0,1,…,9 0,1,…,9 0,1,…,9 E C . B 0,1,…,9 A D +, - E S start G 0,1,…,9 Eliminate states B, C, and G. Change edge labels to regular expressions, where s = + + -. 0,1,…,9 d F .dd*E sd D A d 0,1,…,9 E F S d start Merge edges, and eliminate states A, D, and then read off the regular expression. d Regular expression for the language accepted by F: r3 = dd*(.dd*E + E )(sd + d)d*

  8. In the transition, let X, Y  {a, b}, and V, W  {a, c}. In the figure, (X, X/…) implies (a, a/…) or (b,b/…), and (Y, X/…) implies (a, b/…) or (b, a/…). (V, V/…) implies (a, a/…) or (c, c/…), and (W, V/…) implies (a, c/…) or (c, a/…). Answer (a): (X, X/XX), (Y, X/), (X, Z0/XZ0), (c, X/X), (c, Z0/Z0) (c, Z0/Z0) (, Z0/Z0) 3 2 (X, Z0/XZ0), (c, Z0/Z0) 1 (V, V/VV), (W, V/), (V, Z0/VZ0), (b, V/V), (b, Z0/Z0) (c, Z0/Z0) (, Z0/Z0) (, Z0/Z0) C B 0 start (V, Z0/VZ0), (b, Z0/Z0) (, Z0/Z0) A Answer (b): The PDA is nondeterministic. At the start state the machine takes a nondeterministic transition to state 1 and state A. Upper part of the machine recognizes L1, and the lower part recognizes L2. If the stack is empty (in state 2 and B), the PDA nondeterministically chooses to read the input further, or enter into the accepting state when the stack is empty. 7. (a) Construct a PDA (either NPDA or DPDA, whichever possible) which recognizes L1 L2 of the following two languages: L1 = { x | x  {a, b, c}+, the number of a’s and the number of b’s in x are same } L2 = { x | x  {a, b, c}+, the number of a’s and the number of c’s in x are same } (b) Briefly explain why your PDA is DPDA or NPDA?

  9. ( a, a/aa ) ( a, a/aa ) (b, a/ ) (b, a/ ) (a, Z0/aZ0 ) (b, a/  ) (a, Z0/aZ0 ) (b, a/  ) start start ( a, a/aa ) (b, a/ ) (a, Z0/aZ0 ) (b, a/  ) (b, Z0/Z0 ) start (b, Z0/Z0 ) (b, Z0/Z0 ) 8. What is the language accepted by each of the following PDA in (a), (b) and (c)? (a) Answer: {aibj | i  j  0} (b) Answer: {aibj | i  j  1} (c ) Answer: {aibi+j | i  1, 3  j  1}

  10. Answer (a): (a,a,R) 1 (B,a,N) (a,B,R) 3 (a,b,R) 0 (b,a,R) start 2 (b,B,R) (B,b,N) (b,b,R) 9. Construct a DTM which, given a string x {a, b}+ , moving only once from left to right (i.e., it can never return) shifts x one cell to the right and enters and accepting state. As usual assume that the TM head is on the left end of the input string. You should show your TM in terms of a state transition graph, and explain briefly how your machine works. Answer (b): My DTM iteratively reads the content of each cell, carries it to the right by one cell, and exchange it with that cell’s content. (This transition graph is a part of the graph from problem 5 of HW#2 with L changed to R, symbol 0 to a and 1 to b.) Detailed description: Reading the first symbol, the DTM replaces it by blank(B) and moves to the right cell entering state either 1 or 2, respectively, depending on the symbol read is a or b. In state 1, if current cell has a, the DTM stays in the same state. If the cell has b, the DTM enters state 2. In state 1, the machine writes a, irrelevant to the symbol read. If the current cell contains B (the blank), the machine writes a and enters accepting state 3. In state 2, the DTM does the opposite of what it does in state 1.

  11. a 1 a 3 a a a a b b 0 a S a b 2 b B 4 C a a b b b a a 3 4 a {0,1,2} {0,1,3} a a b {0,1} a a a b 0 {0,1,4} a b {0,2} 2 b b 10. Construct a DFA which recognizes the language of the following grammar. You should also show the procedure that you took to get your answer. S  aS | abB B  bB | bS | | aC |  C  aaB Answer: Direct transformation from the grammar to an FA. For convenience, we rename the states as follows. Find deterministic transitions.

  12. n’ 1 n’+1 2 . . . . . 11. Using the proof by induction technique, prove that the pigeonhole principle is true. Answer: (1) Base: when the number of pigeonholes n = 1, and the number of items m >n. Obviously, the principle is true, because all the items should be in that one pigeonhole. (2)Induction hypothesis: Suppose that the pigeonhole principle is true for any number of pigeonholes n’ < n. (3) Induction step: We show that when the number of pigeonholes is n’+1 and the number of items m > n’+1, the principle also holds. Consider the array of n’+1 pigeonholes as shown below. Case (a): When the last pigeonhole contains no more than one item (i.e., either zero or one item). The first n’ pigeonholes together contain more than n’ items. By the above induction hypothesis, among the first n’ pigeonholes there exists at least one pigeonhole which contains more than one item. The principle holds. Case (b): When the last pigeonhole contains more than one item. This last pigeonhole witnesses that the principle is true. 12. Imagine n basketball players passing a basketball among themselves. Using the pigeonhole principle show that in at least n passes of the ball, there exists a player who has kept the ball more than once. Answer: Consider each player as a pigeonhole, and “holding the ball” as an item put into the player pigeonhole. Since the player who passes the ball first should hold the ball, if there are n passes, there are n+1 items (“holds”) put into the pigeonholes. By the pigeonhole principle, there should be at least one pigeonhole (i.e., player) which contains at least two items (i.e., holds the ball more than once).

More Related