Download presentation
Presentation is loading. Please wait.
1
Midterm (Models of Computation, Fall, 2000)
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 Type 0 Type 1 Type 2 Type 3 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
S CBSa | CBa Ba ba Cb cb Cc cc
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 cb Cc 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
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. A S A a a S b A B B B b Answers: (see next two slides)
5
Answer (a): This DFA is a copy of the key answer of problem 3-(c) of HW #3.
0,1,…,9 start B D G S . E +, - A C F S 0A | 1A A A 0A | 1A A | .B | ED | B 0C | 1C C C 0C | 1C C | ED | D +G | -G | 0F | 1F F G 0F | 1F F F 0F | 1F F | To each state we assign a nonterminal symbol as above, and construct the following regular grammar: ** Notice that E is a terminal symbol.
6
Answer (b): Regular expression: r = r1 + r2 + r3, where r1, r2, and r3 are computed as follows,
where d = (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. start S 0,1,…,9 A (a) B . C (b) (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
D G 0,1,…,9 +, - F B . E start S A C (3) Computing regular expression r3 for the language accepted by State F: Eliminate states B, C, and G. Change edge labels to regular expressions, where s = Merge edges, and eliminate states A, D, and then read off the regular expression. d sd .dd*E Regular expression for the language accepted by F: r3 = dd*(.dd*E + E )(sd + d)d*
8
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? (X, X/XX), (Y, X/), (X, Z0/XZ0), (c, X/X), (c, Z0/Z0) 2 1 (, Z0/Z0) B A (V, Z0/VZ0), (b, Z0/Z0) (V, V/VV), (W, V/), (b, V/V), start 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): 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. 3 C
9
8. What is the language accepted by each of the following PDA in (a), (b) and (c)?
start (a, Z0/aZ0 ) ( a, a/aa ) (b, a/ ) (b, a/ ) start (a, Z0/aZ0 ) ( a, a/aa ) (b, a/ ) (b, a/ ) (a) Answer: {aibj | i j 0} (b) Answer: {aibj | i j 1} start (a, Z0/aZ0 ) ( a, a/aa ) (b, a/ ) (b, a/ ) (b, Z0/Z0 ) (c ) Answer: {aibi+j | i 1, 3 j 1}
10
My DTM iteratively reads the content of each cell,
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. (b,B,R) (a,B,R) (a,b,R) (b,a,R) (a,a,R) (b,b,R) (B,a,N) (B,b,N) start 1 2 3 Answer (a): 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
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. 2 1 4 3 a b b S B C a Find deterministic transitions. b {0,1} a 2 {0,2} 3 4 {0,1,4} {0,1,3} {0,1,2}
12
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. 1 2 n’ n’+1 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).
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.