CSE 355
Fall 2015 - Colbourn
Theory of Computing
Midterm # 2
NAME ASU ID ID: ___________
Page 1 of 5
19 October 2015 Ima Sample You have 50 minutes to complete the exam. Do not open the exam until instructed to do so. No text and no other notes permitted. Computers, calculators, and communication devices are not permitted. Write all answers on the examination paper itself. BUDGET YOUR TIME WELL! SHOW ALL WORK! Question 1 [10] Question 2 [10] Question 3 [10] Question 4 [10] Total [40] If you need a clarification about one of the responses, ask during office hours or recitations. If you think there is an addition error, see the instructor. If you want to question a grade, first read these solutions carefully to understand why you may not have earned the grade you expected. If this does not resolve your question, then provide a clear explanation of your reasons together with the original of your test paper prior to 10/30/15 start of class. Then the entire paper will be regraded, and the grade on the test may increase, decrease, or stay the same. Bonus question [1 mark]: Give an example of a language over the alphabet {0} that is not regular. One example is {0n : n is a perfect square}. Question 1. [10 marks total] (a) [3 marks] Enumerate the steps used in general to convert a CFG into a CFG in Chomsky normal form. (1) make start variable not appear on RHS of any rule, (2) eliminate epsilon transitions except from start variable, (3) eliminate unit rules, (4) replace each terminal on RHSs with more than one symbol by a new variable that generates that terminal, (5) break up RHSs that consist of more than two variables. (b) [4 marks] Consider the grammar S → A | B A → aAc | Ac | B | ε B → aBb | Bb | ε Give a context-‐free grammar in Chomsky normal form for this language. (1) is done already. After (2) we get S → A | B | ε A → aAc | ac | Ac | c | B B → aBb | Bb | ab | b After (3) we get S → aAc | ac | Ac | c | aBb | Bb | ab | b | ε A → aAc | ac | Ac | c | aBb | Bb | ab | b CSE 355
Fall 2015 - Colbourn
After (4) we get After (5) we get Theory of Computing
Midterm # 2
ID: ___________
Page 2 of 5
B → aBb | Bb | ab | b S → XAZ | XZ | AZ | c | XBY | BY | XY | b | ε A → XAZ | XZ | AZ | c | XBY | BY | XY | b B → XBY | BY | XY | b X → a; Y → b; Z → c S → XT | XZ | AZ | c | XU | BY | XY | b | ε A → XT | XZ | AZ | c | XU | BY | XY | b B → XU | BY | XY | b X → a; Y → b; Z → c; T → AZ; U → BY (c) [3 marks] Is the language in the (b) part regular? Explain. No! The language is {aibjck : i ≤ j+k}. Suppose to the contrary that it is regular. Intersect it with a*b* to get {aibj : i ≤ j}; by closure under intersection this must be regular. Let p be the pumping length and consider w = apbp = xyz. Then y contains only a’s, and xy2z contains more a’s than b’s and so is not in the language. Hence it is not regular. Question 2. [10 marks] Let L1 and L2 be context-‐free languages. You cannot simply say that CFLs are closed under union, concatenation, star, and reversal – that is what you are asked to show. Also the question cannot be answered by giving examples; the question asks how to show these in general. Consider CFGs for L1 and L2 with no variables in common, start variables S1 and S2. (a) [2 marks] Show that L1 ∪ L2 is context-‐free. Add a new start variable S and rules S → S1 | S2. As mentioned in class, one could combine the two PDAs. (b) [2 marks] Show that L1L2 is context-‐free. Add a new start variable S and rules S → S1S2. As mentioned in class, combining the two PDAs may not work because the stack contents may mess things up. (c) [2 marks] Show that L1* is context-‐free. Add a new start variable S and rules S → S1S | ε. As mentioned in class, restarting the PDA may not work because the stack contents may mess things up. (d) [4 marks] Show that reverse(L1) is context-‐free. Reverse the symbols on the RHS of every rule in the CFG for L1 to get a CFG for reverse(L1). Reversing the transitions in a PDA may not work; think about the case that in the original PDA the last transition pushes but does not pop – then reversing means that the first transition pops an empty stack! Question 3 [10 marks in total] Consider the language L = {aibjck: i≠j or j≠k}. Describe precisely a PDA that recognizes L. Describe precisely a PDA that CSE 355
Fall 2015 - Colbourn
Theory of Computing
Midterm # 2
ID: ___________
Page 3 of 5
recognizes L. One such PDA has start state q0, final states {accept,accept1} and the following transition function. δ(q0,ε,ε) δ(q1,a,ε) δ(q1,ε,ε) δ(q2,b,ε) δ(q2,ε,ε) δ(q3,ε,x) δ(q3,c,x) δ(q3,c,$) δ(accept,c,$) {(q1,$),(q4,$)} {(q1,ε)} {(q2,ε)} {(q2,x)} {(q3,ε)} {(accept, x)} {(q3, ε)} {(accept, $)} {(accept, $)} δ(q4,a,ε) δ(q4,ε,ε) δ(q5,b,x) δ(q5,b,$) δ(q5,ε,x) δ(q6,b,$) δ(q6,ε,ε) δ(accept1,c,ε) {(q4,x)} {(q5,ε)} {(q5,ε)} {(q6,$)} {(accept1,x)} {(q6,$)} {(accept1,ε)} {(accept1,ε)} Question 4 [10 marks] Consider the grammar S → A | B A → aAc | Ac | B | ε B → aBb | Bb | ε Using the method described in class, show a transition diagram for a PDA that recognizes the language generated by this grammar. Explain your steps. I am typing so will make a table instead. First I use a PDA variant that can push many symbols in one transition; let me suppose that pushing abc, for example, means pushing c then b then a. I make a PDA variant that makes a transition for each rule, and so keeps the derivation on the stack, matching terminals against the input. Here is the transition function: δ(r,ε,$) {(accept,ε)} δ(q0,ε,ε) {(r,S$)} δ(r,a,a) {(r,ε)} δ(r,b,b) {(r,ε)} δ(r,c,c) {(r,ε)} {(r,A),(r,B)} δ(r,ε,S) δ(r,ε,A) {(r,aAc),(r,Ac),(r,B), (r,ε)} δ(r,ε,B) {(r,aBb),(r,Bb), (r,ε)} I break up the multiple pushes by adding new states to get a PDA δ(q0,ε,ε) δ(r,a,a) δ(r,ε,S) δ(q2,ε,ε) δ(q5,ε,ε) {(q1,$)} {(r,ε)} {(r,A),(r,B)} {(q3,A)} {(q6,B)} And I am done. δ(q1,ε,ε) δ(r,b,b) δ(r,ε,A) δ(q3,ε,ε) δ(q6,ε,ε) {(r,S)} {(r,ε)} {(q2,c),(q4,c),(r,B), (r,ε)} {(r,a)} {(r,a)} δ(r,ε,$) δ(r,c,c) δ(r,ε,B) δ(q4,ε,ε) δ(q7,ε,ε) {(accept,ε)} {(r,ε)} {(q5,b),(q7,b), (r,ε)} {(r,A)} {(r,B)}
© Copyright 2026 Paperzz