Mid-Semester Test # 2 (Fall 2006) Solutions

advertisement
CS 454 Theory of Computation
Fall 2006
Solutions to Mid-term # 2
1.
State formally the transition function of a PDAin terms of  the stack
alphabet and , the input alphabet.
: Q x  x  P (Q x *)
2. Let L be a regular language. Circle the true statements in the following
list:
(a) L is context-free.
(b) L can be accepted by a deterministic pushdown automaton.
(c) the complement of L is context-free.
All are true.
3. Define formally the statement: “=> ”with respect to a context-free
grammar G. (and  are two sentential forms.)
=>  if there exist sentential forms , , , and such that  =
 A ,  =   , and there is a rule in the grammar A  .
4. Let A and B be two languages over {0,1} such that A is a subset of B.
Consider the following statements:
(1) If B is finite, then A is finite.
(2) If B is context-free, A is context-free.
(3) If the complement of B is context-free, then the complement of A is
context-free.
Which of the above statements is true? Circle the correct option.
(There is only one correct option.)
(A) 1 only (B) 1 and 2 only (C) 1 and 3 only (D) 2 and 3 only (E) all of
them are true.
5. Under which of the operations are the context-free languages closed?
(1) union (2) intersection (3) homomorphism
Circle the correct assertion. (Exactly one of them is correct.)
(A) 1 only (B) 2 and 3 only (C) 1 and 3 only (D) 1 and 2 only (E) all of
them are true.
6.
Let P be the PDA <Q, , q0, Z0, F>. Write the starting configuration
(ID) of P.
On input w, the starting configuration is (q0, w, Z0).
7. For the same PDA in (6), write the set of all accepting configurations.
The set of accepting configurations is { (q, , ) | q is in F,  is in * }
8. For the PDA <Q, , q0, Z0> that accepts by empty stack, write down
the set of accepting configurations.
The set of accepting configurations is { (q, , ) | q is in Q }
9. Define precisely what it means for a string w to be accepted by a PDA P =
<Q, , q0, Z0, F>.
w is accepted by P if (q0, w, Z0) can lead to, via a sequence of moves, to
an accepting configuration in the set { (q, , ) | q is in F,  is in * }
10. Give an informal English description of the set of strings accepted by the
following context-free grammar:
S  1S | 0A |, A  1S
The set of strings that do not have 00 as a substring and end with a 1.
Section 2
1. Write a context-free grammar for the set of prefix expressions with + and * as
operators and a and b as operands. For example, + * a a b is a valid string in this
language. Is this language regular? Justify your answer.
Solution: The grammar is S  O S S | a | b, O  + | *
It can be shown by induction that the number of operands in any
prefix expression is one less than the number of operators. We will now
show using pumping lemma that L(S) is not regular: Let n be the constant
in the pumping lemma. Choose the string w = +^n a^(n+1) in L(S). For
every partition of w into x, y and z such that |y| > 0 and |xy | <= n, we can
pump twice to get the string +^(n+j) a^(n+1) where j = |y|> 0. This string is
not in L(S).
2. Describe a PDA for the language L = { aj bk | j != k and j != 2k }. First describe the
PDA informally, then present its state transition diagram.
Solution: (informal outline). We can write L as L1 U L2 U L3 where
L1 = { aj bk | j < k },
L2 = { aj bk | k < j < 2k } and
L3 = { aj bk | j > 2k }.
PDA for L1 will work as follows: On a, push a symbol 1, and on b, pop.
When the stack top is Z0, if the input head is reading a 1, accept.
PDA for L2: For every a, push a 1 or two 1’s. Make sure that both types of
moves are used at least once. Then, pop on every b. If the input has been
completely read when the stack contains no 1, then accept.
PDA for L3: push a 1 for every a, and pop two 1’s for every b. If the stack
is non-empty when the input has been completely read, accept.
3. (a) Let L = { an bm | m <= n <= 2m }. We showed in the class that L is context-free.
Show that its complement is also context-free.
Solution: The complement of L is L1 = { an bm | n < m } U L2 = { an bm | n >
2m } U L3 = { w | w is not of the form a*b*}. A context-free language for
each of L1, L2 and L3 are given by:
G1: S1  aSb | A1, A1 bA1 | b
G2: S2  aS2bb | A2, A2  aA2 | a
G3: S3  CbaC, C  aC | bC | .
Finally, the grammar for the complement of L is G that includes the
Rules above along with S  S1 | S2 | S3.
(b) It was shown in the class that the context-free languages are not closed under
complement. Explain why the result in (a) does not contradict this assertion.
There is no contradiction since “not closed under complement” means there
EXISTS a CFL L for which L complement is not a CFL. More explicitly, “not
closed under complement” does not mean FOR ALL CFL L, L complement is
not a CFL. It only means that FOR SOME CFL’s L, L complement is not a CFL.
4. Consider the language L containing a single string, L = {a25 }. Construct a contextfree grammar in Chomsky Normal Form for L with as few production rules as
possible. You need not prove that your grammar has the fewest number of rules.
Solution: The following CFL with 8 rules generates L:
S  A F, B  AA, C  BB, D  CC, E  DD, F  DE, A a
Showing that no smaller grammar exists is tedious and was not required.
5. Let R be a regular language and L be a context-free language. Show that LR is
context-free. Hint: first describe informally a PDA M for the language LR, in
terms of a PDA for L and a DFA for R. Then, describe M formally. You need not
prove that M accepts LR.
Solution: We showed that every regular language is context-free and so R is a
CFL. We also showed that CFL’s are closed under concatenation so LR is
context-free.
6. Is the following language context-free? Justify your answer: L = { w | w has an
equal number of a’s and b’s and the length of w is a multiple of 3 }.
Solution: Yes. L = L1 intersection L2 where
L1 = { w | w has an equal number of a’s and b’s } and
L2 = { w | the length of w is a multiple of 3 }.
L1 can be generated by the CFG: S  aSb | bSa | SS | .
L2 is regular since L2 can be generated by the regular expression
((a+b)(a+b)(a+b))*.
It was shown in the class that CFL’s are closed under intersection with
regular languages. Thus L is a CFL.
7. Show that the grammar given below is unambiguous:
S  A1B
A  0A | 
B  0B | 1B | 
Construct a right-linear grammar for this language.
Solution: L(S) can be easily seen to be L(S) = 0*1(0+1)*. Let w be a string
in L(S) and w = 0j 1 w’. This means the left-most occurrence of 1 in w
occurs in position j+1. The unique leftmost derivation for w involves
applying the rule S  A1B, then applying the rule A  0A j times to
get the sentential form 0j A1B. Now use the rule A  e. From now on,
apply the rule involving B suitably to generate the string. There are
options to use other rules (for example, to apply A  e either too soon
or too late etc.) But it is obvious that any such erroneous application
will result in not being able to generate w. Thus, the above grammar is
unambiguous.
Right-linear grammar for L:
S  0S | 1A, A  0A | 1A, .
Download