CSE 460 - Solutions to Sample Final Exam Exam # Name
Transcription
CSE 460 - Solutions to Sample Final Exam Exam # Name
CSE 460 - Solutions to Sample Final Exam Exam # Fall Semester, 2000 Name The The The The 1.0 2.0 3.0 4.0 points points points points on on on on this this this this exam exam exam exam sum sum sum sum to to to to 26. 23. 21. 16. There is one question on Chomsky Normal Form worth a total of 3 extra credit 4.0 points. 1. Draw the computation tree of PDA M on input string aabb. Is the input string accepted? [4, 1.0] • Q = {B, C, D, E, F, G, H} • Σ = {a, b} • Γ = {a, X, Y, Z} • q0 = B • Z0 = Z • A = {E, H} • δ: Move Current Input Top of Next Stack Number State Symbol Stack State Update --------------------------------------------------------1 B /\ Z C X 2 B /\ Z F Y 3 4 5 6 7 C C C D D a a b b /\ X a a a X C C D D E aX aa /\ /\ X 8 9 10 11 F F F G a a b b Y a a a F F G G aaY aaa /\ /\ 1 12 G /\ (C, (C, (C, (D, (D, (E, Y H Y (B, aabb, Z) / \ / \ aabb, X) (F, aabb, Y) abb, aX) (F, abb, aaY) bb, aaX) (F, bb, aaaaY) b, aX) (F, b, aaaY) /\, X) (F, /\, aaY) /\, X) The input string aabb is accepted because we reach configuration E, λ, X). 2. Consider the following grammar G1 defined in abbreviated format. S → SSS | b. (a) Give the 4 shortest strings in L(G1 ). [2, 1.0] b, bbb, bbbbb, bbbbbbb (b) Draw a leftmost derivation of the string bbbbb. [2, 1.0] S ⇒ SSS ⇒ bSS ⇒ bbS ⇒ bbSSS ⇒ bbbSS ⇒ bbbbS ⇒ bbbbb (c) Draw the derivation tree that corresponds to your leftmost derivation above. [2, 1.0] S /|\ / | \ S S S / / /|\ / / / | \ b b S S S | | | | | | b b b (d) Draw the rightmost derivation that corresponds to your leftmost derivation above. [2, 1.0] S ⇒ SSS ⇒ SSSSS ⇒ SSSSb ⇒ SSSbb ⇒ SSbbb ⇒ Sbbbb ⇒ bbbbb (e) Prove that G1 is an ambiguous grammar. [2, 2.0] I can prove G1 is ambiguous by coming up with an alternative leftmost derivation of bbbbb such as S ⇒ SSS ⇒ bSS ⇒ bSSSS ⇒ bbSSS ⇒ bbbSS ⇒ bbbbS ⇒ bbbbb (f) What is L(G1 )? [2, 1.0] L(G1 ) is the set of strings with an odd number of b’s. (g) Give a regular expression r such that L(r) = L(G1 ). [2, 1.0] b(bb)∗ 3. Given the following two grammars G1 and G2 , construct a new grammar G3 such that L(G3 ) = L(G1 ) ∪ L(G1 )L(G2 ). [2, 2.0] 2 • G1 : S → SSS | a • G2 : S → b | bb | bbb We could do this by first applying the concatenation construction and then the set union construction. However, we can do this shorter by observing this language is also L(G1 )(λ ∪ L(G2 ). This leads to • V = {S, T, U } • Σ = {a, b} • Start variable S • S → T U | T , T → T T T | a, U → b | bb | bbb 4. Consider the following grammar G. • V = {S, U } • Σ = {a, b} • Start variable is U • Productions – U → bbU | S – S → aS | λ Suppose M is the PDA that results from applying the construction given in class so that L(M ) = L(G). List all the transitions in M . [2, 2.0] Current Input Top of Next Stack State Symbol Stack State Update ---------------------------------------------q0 /\ Z q1 UZ q1 /\ Z q2 Z q1 /\ U q1 bbU q1 /\ U q1 S q1 /\ S q1 aS q1 /\ S q1 /\ q1 a a q1 /\ q1 b b q1 /\ 5. Questions about regular language constructions (a) Suppose we apply the construction covered in class to construct an NFA-λ M3 from NFA-λ M1 with 8 accepting states and NFA-λ M2 with 7 accepting states such that L(M3 ) = L(M1 ) ∪ L(M2 ). How many accepting states does M3 have? [2, 2.0] 15 accepting states (b) Suppose we apply the construction of Theorem 4.1 and construct an FSA M5 from NFA M4 with 3 states total, 1 of which is accepting, such that L(M5 ) = L(M4 ). How many accepting states does M5 have counting all states that are unreachable from the initial state? [2,2.0] 4 accepting states 3 6. C ++ Program P and language Y (P ) (a) For any program P , which of the following represents Y (P ): [2, 1.0] • The set of strings P accepts. • The set of strings P rejects. • The set of strings P loops on. The set of strings P accepts. (b) Suppose program P loops on input string aa. Is it possible that Y (P ) is solvable? Why or why not? [2, 3.0] Yes, it is possible. Y (P ) is solvable if some program solves L(P ). This program cannot be P since P loops on aa, but some other program might decide L(P ) which would make it solvable. 7. In each of the following scenarios concerning problems P1 and P2 , describe what additional facts must be true about problems P1 and P2 . The correct answer should be one of {none, P1 is solvable, P2 is solvable, P1 is not solvable, P2 is not solvable}. [2, 3.0] (a) P2 is not solvable and P1 ≤ P2 . None. (b) P2 is solvable and P1 ≤ P2 . P1 is solvable. (c) P1 is not solvable and P1 ≤ P2 . P2 is not solvable. (d) P1 is solvable and P1 ≤ P2 . None. 8. Classify the following statements as true or false. No proof is necessary. [7, 3.0] (a) Every FSA is an NFA. True (b) There exists a regular language L such that L cannot be accepted by a deterministic FSA. False (c) There exists a CFL L such that L cannot be accepted by a deterministic PDA. True (d) If L1 ⊆ L2 and L2 is regular, then L1 must be regular. False (e) The Halting problem where the input program is an FSA is unsolvable. False (f) Every regular language can be accepted by some FSA with only 3 states. False (g) Every CFL can be accepted by some PDA with only 3 states. True 9. Give a CFG G such that L(G) is the set of strings over {a,b} with two more a’s than b’s. [2, 4.0] • V = {S, E} • Σ = {a, b} • Start variable S • S → EaEaE, E → aEbE | bEaE | λ 4 10. Consider the following incomplete construction. • Input – PDA M1 = (Q1 , Σ, Γ1 , q1 , Z1 , A1 , δ1 ) – PDA M2 = (Q2 , Σ, Γ2 , q2 , Z2 , A2 , δ2 ) • Output – PDA M3 = (Q3 , Σ, Γ3 , q3 , Z3 , A3 , δ3 ) such that L(M3 ) = L(M1 ) ∪ L(M2 ). (a) Fill in the values for each of the components of M3 . You may assume that no renaming is needed. [2, 4.0] • • • • • • Q3 = Q1 ∪ Q2 ∪ {p} where p is a new symbol not in Q1 ∪ Q2 . Γ3 = Γ1 ∪ Γ2 ∪ {Z} where Z is a new symbol not in Γ1 ∪ Γ2 . q3 = p Z3 = Z A3 = A1 ∪ A2 δ3 = δ1 ∪ δ2 ∪ {(p, λ, Z, q1 , Z1 ), (p, λ, Z, q2 , Z2 )} This construction is a generalization of the one used to combine two NFA’s together to handle set union. (b) What does this construction help us prove? [2, 3.0] That CFL’s are closed under set union. 11. Applying closure properties (a) Which one of the following five closure property “proofs” actually proves that the language L IS context-free? [2, 1.0] (ii) (b) Which one of the following five closure property “proofs” actually proves that the language L IS NOT context-free? [2, 3.0] (v) i. We show L1 ∩ L = L2 where L1 is a context-free language and L2 is a half-solvable but not solvable language. ii. We show that L1 ∩ L2 = L where L1 is a context-free but not regular language and L2 is a finite language. iii. We show that L1 ∩ L = L2 where L1 is a context-free language and L2 is not a context-free language. iv. We show that L1 ∩ L2 = L where L1 is a context-free language and L2 is a contextfree language. v. We show that L1 ∩L = L2 where L1 is a regular language and L2 is not a half-solvable language. 12. Consider the language L = {ai bj | j < i}. Let n be the integer from the pumping lemma. For each of the following strings, state whether or not they can or cannot be used in a pumping lemma proof that L is not regular. For those which can be used, give a value of k that works. For those which cannot be used, describe why they cannot be used. [3, 2.0] and [2, 4.0] (a) an bn Cannot be used because it is not in L (b) a11 b10 Cannot be used because it does not hold for large n 5 (c) a2n bn Cannot be used because it “pumps.” Let u = λ, v = a, and w = a2n−1 bn . For all values of k ≥ 0, uv k w ∈ L. The problem is when k = 0, we can only guarantee one a is eliminated which still leaves us in the language. (d) a2n+1 b2n This can be used, and the only value of k which works is 0. (e) (ab)n a Cannot be used because it “pumps.” Let u = λ, v = ab, and w = (ab)n−1 a. For all values of k ≥ 0, uv k w ∈ L. Since v contains an equal number of a’s and b’s, no matter how many times v is pumped, we stay in the language. 13. Provide the start of a table which proves that all the strings in aa∗ are pairwise distinguishable with respect to the language T W OCOP IES = {ww | w ∈ {a, b}∗ }. [2, 4.0] bab bbaabb bbbaaabbb ... -------------------------------------------------a IN OUT OUT ... aa OUT IN OUT ... aaa OUT OUT IN ... ... ... ... ... ... 14. Consider grammar G = ({S}, {a, b}, S, S → ab|aSb) and the language AB = {an bn | n ≥ 1}. (a) In order to prove that L(G) ⊆ AB, which of the following statements would we try and prove? Circle your answer. [2, 2.0] • S ⇒∗G x implies x ∈ AB • x ∈ AB implies S ⇒∗G x The first choice is correct. (b) Modify the above statement so that it represents an infinite set of statements parameterized by n, the number of steps required to derive string x. [2, 2.0] ∀n ≥ 1 S ⇒nG x implies x ∈ AB (c) Suppose we prove your answer to part (b) is correct using induction on n. Prove the base case. [2, 2.0] The base case is n = 1. The only string which can be derived in 1 step is the string ab which clearly is in AB. Thus the base case holds. (d) Now consider the inductive case. Is a strong induction hypothesis required? Why or why not? [2, 3.0] No, it would not. The reason is that I will end up with S ⇒ aSb ⇒n x which means that x has the form aub where S ⇒n u. Since it takes exactly n steps, a normal induction hypothesis will suffice. 15. Suppose your are using a debugger to check the correctness of a program P . First describe how debuggers such as ddd and gdb operate on P . Next suppose the halting problem is solvable. Describe how this might change the way a debugger operates on P . [2, 3.0] 6 Debuggers such as ddd and gdb operate on P by executing P on an input x of your choosing step by step. They allow you to set breakpoints and view the current configuration of P on x at any time during the computation. They do not perform any analysis of program P . If the halting problem were solvable, we might be able to create a debugger which analyzes program P to determine if it is correct rather than simply executing it on an input string. 16. Ambiguous grammars (a) Define what it means for a CFG G to be ambiguous. [2, 1.0] A grammar is ambiguous if there exists some string x ∈ L(G) such that there are two distinct parse trees for string x. (b) Identify which one of the following statements most accurately represents why we do not like ambiguous grammars. Circle your answer. [2, 3.0] • If the grammar for a programming language is ambiguous, there is more than one way to interpret the meaning of some programs. • If the grammar for a programming language is ambiguous, there is more than one way to write a program to solve some problems. • If the grammar for a programming language is ambiguous, there is more than one way to build a compiler for that programming language. The first choice is correct. 17. Let EM P T Y R and EM P T Y G be defined as follows: • EM P T Y R – Input: regular expression r – Question: Is L(r) = {}? • EM P T Y G – Input: CFG G – Question: Is L(G) = {}? Suppose f is a function that shows that EM P T Y R ≤ EM P T Y G. (a) Give a specification for f . That is, state what its input and output should be and how they should be related to each other. [2, 2.0] The input should be a regular expression r. The output should be a CFG G such that L(G) = L(r). (b) Give an algorithm f that shows that EM P T Y R ≤ EM P T Y G. You do not need to specify all the details of your algorithm, but your answer should be fairly complete. One possibility is to describe this algorithm in terms of another algorithm we have seen in class highlighting where this algorithm is different from the previous construction. [2, 4.0] One way to do this is to modify the algorithm which converts a regular expression into an NFA-λ into one which converts a regular expression into a CFG. The base cases are easily handled by grammars of the form S → a or S → λ or a grammar with no productions. The recursive cases are handled by applying the CFG constructions for union, concatenate, and Kleene closure in place of the corresponding NFA-λ constructions. 7 18. For program P 000 , assume that the input alphabet is {0, 1}. Consider the following program P3 • Input: A program P 0 and an integer x which is input for program P 0 • Output: A program P 000 as follows: bool main(string z) { P’(x); if ((|z| % 2) == 0) return(no); else return(yes); } bool P’(int x) /* Details unspecified */ (a) If P 0 halts on x, what is Y (P 000 )? [2, 1.0] Y (P 000 ) is the set of odd length strings over {0, 1}. (b) If P 0 loops on x, what is Y (P 000 )? [2, 1.0] Y (P 000 ) = {} (c) This program P3 can be used to prove that the halting problem transforms to some of the following problems. Identify all such problems, and for those which this does not apply, state whether or not there is a Yes→No violation or a No→Yes violation. You must get at least 5 correct to receive any credit. [2, 4.0] i. ii. iii. iv. v. vi. Input: Input: Input: Input: Input: Input: Program Program Program Program Program Program P. P. P. P. P. P. Yes/No Yes/No Yes/No Yes/No Yes/No Yes/No Question: Question: Question: Question: Question: Question: Is Y (P ) nonempty? Is Y (P ) is infinite? Is Y (P ) is regular? Is Y (P ) is context-free? Does Y (P ) contain 00? Does Y (P ) contain 000? It works for the first, second, and sixth options. The third has a No→Yes violation. The fourth has a No→Yes violation. The fifth has a Yes→No violation. 19. Let me define the NotAccepting Problem N as follows: • Input: Program P , integer x which is an input to program P . • Yes/No Question: Does P not accept x? Consider the following incomplete diagonalization proof that N , the NotAccepting Problem, is not solvable. • Assume N is solvable. • Let P N be the program which solves N . – P N exists by the definition of solvable languages. • We construct a program D using P N which cannot exist. – Construction of D to be filled in. – Argument that D cannot exist omitted. • Contradiction. Our assumption that N is solvable must be false. • We conclude that N must not be solvable. 8 (a) Define what it means if P N (P, y) = yes for a program P and an integer y. A correct answer should be a single, very specific sentence about P and y. [2, 2.0] This means program P does not accept integer y. (b) Both of the following programs D cannot be used in the above proof. For each program, choose one clause of the IF-THEN-ELSE statement which causes a problem and explain why that clause is a problem. [2, 4.0] • D1 bool main(int y) { program P = calculate(y); if (PN(P,y)) return(no); else return(yes); } program calculate(int x) /* Details unspecified */ bool PN(program P, int x) /* Details unspecified */ Both clauses are incorrect for this choice of D. If P N (P, y) = yes, this means P does not accept integer y. We need to do the opposite for the THEN case which means we need D to accept y to make D behave differently on input y. Likewise if P N (P, y) = no, this means P accepts input y which means we need D to not accept y. • D2 bool main(int y) { program P = calculate(y); if (PN(P,y)) while (1>0) {}; else return(no); } program calculate(int x) /* Details unspecified */ bool PN(program P, int x) /* Details unspecified */ In this case, only the first clause is incorrect. We need to accept y in this case. The ELSE clause correctly does not accept y. (c) Give a program D which would work in the above proof. [2, 4.0] bool main(int y) { program P = calculate(y); if (PN(P,y)) return(yes); else return(no); } program calculate(int x) /* Details unspecified */ bool PN(program P, int x) /* Details unspecified */ 20. To receive any credit for the following problem, you must use the algorithm described in the textbook. Consider the following grammar with start variable S. S → T U V, T → λ | bT, U → λ | aU, V → λ | ccV (a) Show the grammar that results after eliminating null productions. [1, extra credit 4.0] • S → T | U | V | TU | TV | UV | TUV • T → b | bT • U → a | aU 9 • V → cc | ccV (b) Show the grammar that results after eliminating unit productions. [1, 4.0] • • • • S → b | bT | a | aU | cc | ccV | T U | T V | U V | T U V T → b | bT U → a | aU V → cc | ccV (c) Show the grammar in Chomsky Normal Form. [1, 4.0] • • • • • • • • • S → b | BT | a | AU | CC | CD | T U | T V | U V | T E T → b | BT U → a | AU V → cc | CD A→a B→b C→c D → CV E → UV 10