You are on page 1of 3

CSE 355

Fall 2013 - Colbourn

Theory of Computing
Midterm # 2 Solutions

ID: ___________
Page 1 of 6

20 November 2013

NAME

Ima Sample

ASU ID

Answers in green, comments in red.


You have one hour and 15 minutes to complete the exam. Do not open the exam until instructed to do so. One
sheet of notes is permitted no text, no other notes. 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
Question 3
Question 4
Question 5
Total

[10]
[10]
[10]
[10]
[50]

Bonus question [1 mark]: Give an example of a language over the alphabet {0} that is not
context-free. For example, {0n : n is a power of 2}, {0n : n is a perfect square}, or {0n : n is a
prime}.

Question 1. [10 marks total]


(a) [4 marks] Consider the grammar
S R | T
R aRc | aR | T |
T aTb | aT |
Give a context-free grammar in Chomsky normal form for this language.
The start variable already does not appear on the RHS of a rule. Eliminate -rules
S R | T |
R aRc | aR | ac | a | T
T aTb | aT | ab | a
Eliminate unit rules to get S aRc | aR | ac | a | aTb | aT | ab |
R aRc | aR | ac | a | aTb | aT | ab
T aTb | aT | ab | a
Replace terminals in long RHSs with variables
S ARC | AR | AC | a | ATB | AT | AB |
R ARC | AR | AC | a | ATB | AT | AB
T a | ATB | AT | AB
A a; B b; C c
Break up long RHSs: S UC | AR | AC | a | VB | AT | AB | ; U AR; V AT
R XC | AR | AC | a | YB | AT | AB; X AR; Y AT
T a | ATB | AT | AB; A a; B b; C c
You do not need to show all of these steps in detail.

CSE 355
Fall 2013 - Colbourn

Theory of Computing
Midterm # 2 Solutions

ID: ___________
Page 2 of 6

(b) [2 marks] Is the language in the (a) part linear? Explain.


Yes, every rule of the original grammar has at most one variable on the RHS. If a language has
at least one linear grammar, it is linear. It may also have grammars that are not linear.
(c) [4 marks] Is the language in the (a) part regular? Explain.
The language is {aibjck : i j+k and i,j,k 0}. This is not regular. To explain, suppose to the
contrary that it is regular, and let p be its pumping length. Then apcp is in the language, so we
can write it as xyz with |xy| at most p, and |y| at least one so that xyiz is in the language for
every non-negative integer i. But then y contains only as, so xy0z is NOT in the language. This
is a contradiction, so the language is not regular. You cannot just write that A DFA cant do it
that does not explain at all. Also, this question is NOT asking whether the grammar is regular
it is asking whether the language is regular.
Question 2. [10 marks] Let L be a language. Then symmetric(L) = {w : w L and wrev L}.
(a) [3 marks] If L is regular, must symmetric(L) be regular? Explain.
Yes. Let Lrev be the language {w : wrev L}. We have seen that regular languages are closed
under reversal, so if L is regular so is Lrev. Now symmetric(L) = L Lrev, so because regular
languages are closed under intersection, symmetric(L) is regular whenever L is.
(b) [7 marks] If L is context-free, must symmetric(L) be context-free? Explain.
No. Let L = {aibiaj : i,j 0}. Then L is a CFL. (Heres a CFG: S TA; T aTb | ; A aA | .) But
symmetric(L) = {aibiai : i 0}. By pumping, symmetric(L) is not a CFL even though L is.
Question 3 [10 marks] Consider the language L = {aibjck: ij or jk}. Show that L is not a DCFL.
You cannot show this by showing that L is not a CFL, because it is. (Heres a CFG for it: S RC |
AT; A aA | ; B bB | ; C cC | ; R aRb | aA | bB; T bTc | cC | bB.) So this language
can be pumped because it is a CFL.
The DCFLs are closed under complementation, so consider the complement of L. It is {aibici : i
0} *ba* *ca* *cb* where = {a,b,c}. The CFLs are not in general closed under
intersection, but the intersection of a CFL and a regular language is a CFL (We saw this using the
product construction that we used to show that regular languages are closed under
intersection.) So intersect the complement of L with a*b*c* to get {aibici : i 0}. Because this is
not a CFL (by pumping, as we have seen many times), the complement of L is not a CFL. But
then because DCFLs are closed under complement, L is not a DCFL.
(You cannot argue that there is an `or which means there is a choice so it cannot be
deterministic. After all, {w {0,1}*: w contains a 0 or a 1} is not only a DCFL, it is regular!)
Question 4 [10 marks] Draw a state diagram for a deterministic pushdown automaton to
recognize {aibjcjakci+k : i,j,k 0}. Explain.
You must be careful about the cases when one, two, or all three of i, j, or k are 0. If the input is
empty, accept. Otherwise, first stack the as until the first b or c. If the next character is c,
match cs on the input with as on the stack until the end of the string. Otherwise, stack the bs
and then match against cs against them. Then stack as if there are any, and finally match all
the cs on input to as on the stack.

CSE 355
Fall 2013 - Colbourn

Theory of Computing
Midterm # 2 Solutions

ID: ___________
Page 3 of 6


(After including the picture, I see a mistake with some of the red transitions to reject. At the
lower left it should not say c,a ; instead it should say b,a and b,b . But the
transitions to reject can/should be omitted anyway.)
Question 5 [10 marks in total] In each part, answer yes or no, and explain. The grade is entirely
for the explanation given, not the yes/no answer!
(a) [2 marks] If L1 and L2 are linear languages, must L1 L2 be a linear language? Yes. When L1
and L2 have linear grammars with start variables S1 and S2 and no variables in common, take all
rules for both along with the rule S S1 | S2 to get a linear grammar for L1 L2.
(b) [2 marks] If L1 and L2 are CFLs, must L1 L2 be a CFL? No. {aibiaj : i,j 0} and {aibjaj : i,j 0}
are CFLs but their intersection is not.
(c) [2 marks] If L is a CFL, must L* be a CFL? Yes. If L has CFG with start variable S, form a new
CFG by adding a new start variable N and the rule N NS | . This is a CFG for L*.
(d) [2 marks] If L has an unambiguous context-free grammar, must every CFG for L be
unambiguous? No. (As we saw in class,) CFGs S SS | (S) | and S (S)S | generate the
same language. The first is ambiguous; the second is not.
(e) [2 marks] If L is a regular language, must {wwrev : w L} be context-free? Yes. If L is regular,
it has a regular grammar. (All rules are of the form A aB or A .) Replace every rule of the
form A aB by A aBa, and leave the A rules alone to get a CFG for {wwrev : w L}. (You
cannot just show that LLrev is a CFL, because LLrev is NOT {wwrev : w L}.)

You might also like