You are on page 1of 3

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 as, and xy2z contains more as than bs 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: ij or jk}.
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)}