Sie sind auf Seite 1von 5

Homework set 3 with solutions

March 29, 2010


1
1 Context-Free grammars (C)
Give CFGs for the following languages
The language of strings of the form a
n
b
k
a
n
where n is any natural number
and k is a multiple of 3
The language of strings of the form a
2n
b
n
where n is any natural number
Solutions:
For the rst language
S aSa | B
B bbbB |
For the second language
S aaSb|
2 More Context-free grammars (C)
Give a CFG for the language of correct unary additions, that is the language of
all strings of the form 1
k
+1
l
= 1
k+l
for positive naturals k, l (here the alphabet
is {1, +, =}).
Solution:
S 1S1 | + A
A 1A1 | =
3 PDA (C)
Give a PDA for the language of exercise 2.
Solution: See gure 1.
4 CFLs playing with regular languages (C)
Show that if a language L
1
is context-free and a language L
2
is regular, then
L
1
L
2
is also context-free.
Solution:
If L
2
is regular it is certainly also CF. Thus, L
1
L
2
is CF because CFLs
are closed under union.
2
Figure 1: PDA for problem 3. Informal description: push a $ on the stack to
mark the beginning. Then, while 1s are being read push them on the stack. If
a + is read only go on if a 1 has also been read (notice the second transition).
Keep reading 1s and pushing them on the stack until a = is read. Now for every
read 1, pop a 1 from the stack. If the stack is empty at the end of input accept.
(There is one detail missing from this PDA. Can you spot it and x it?
5 CFLs and complements (B)
Assuming the fact that the class of CFLs is not closed under intersection, prove
that it is also not closed under complementation.
Solution:
Suppose that the class of CFLs was closed under complementation. Then,
take any two CFLs L
1
and L
2
. L
c
1
and L
c
2
would also be CF, as would be
L
c
1
L
c
2
, because CFs are closed under union. Then (L
c
1
L
c
2
)
c
would also be
CF as the complement of a CF language. But by DeMorgans law, this is equal
to L
1
L
2
, so since we can do this for any two CF languages this would prove
that CFLs are closed under intersection, a contradiction.
6 DPDAs (B)
A DPDA is a deterministic PDA, i.e. a special case of a PDA such that for
every state, every input symbol read and every symbol on the top of the stack
there exists exactly one transition.
Are DPDAs more powerful than NFAs? Show that there exists a non-
regular language which can be recognized by a DPDA.
Are PDAs more powerful than DPDAs? Try to prove this indirectly. The
class of languages recognized by PDAs is the class of CFLs. This class is
not closed under complementation. Is the class of languages recognized
by DPDAs closed under complementation?
Solution:
The language a
n
b
n
is not regular so no NFA exists to decide it. However, it
is possible to construct a DPDA for it: push a symbol on the stack while as are
being read and pop a symbol for every b. If the stack ends up empty, accept.
To prove that non-determinism is essential for CFLs, observe that it is pos-
sible, given a DPDA D, to construct a DPDA which accepts the complement
language: simply exchange accepting with non-accepting states. Therefore, the
3
class of languages accepted by DPDAs is closed under complement, which means
it cannot be equal to the class of CFLs.
7 Complements of CFLs continued (B)
We know that generally the class of context-free languages is not closed under
complementation. However, if L is context-free this does not necessarily mean
that L
c
is not context-free. It could well be the case that both L and L
c
are
context-free.
Give an example of a CFL whose complement is also CF.
Let L be the language of all strings of the form a
n
b
n
. Show that L
c
is
context-free.
Solution:
Any regular language can serve as an example of a CFL whose complement
is also CFL.
Now, let L
1
= (a b)

ba(a b)

, L
2
= a
i
b
j
, i > j and L
3
= a
i
b
j
, i < j. It is
not hard to see that L
c
= L
1
L
2
L
3
, so it suces to show that these three
are CF. L
1
is regular so certainly CF. L
2
is the concatenation of a
+
(which is
regular, thus CF) with L itself, so L
2
is CF. Finally, L
3
is the concatenation of
L with b
+
.
8 CFLs and intersection (A)
Use the fact that the language L = {w | w = a
n
b
n
a
n
} is not context-free to
prove that the class of CFLs is not closed under intersection. (Hint: nd two
CFLs whose intersection is L)
Solution:
Let L
1
= a
n
b
n
a
i
and L
2
= a
i
b
n
a
n
. It is not hard to see that both are CF as
the concatenation of CF languages. Also L = L
1
L
2
, so if CFLs were closed
under intersection, L would be CF.
9 CFLs playing with regular languages (A)
Show that if a language L
1
is context-free and a language L
2
is regular, then
L
1
L
2
and L
1
\ L
2
are also context-free. Is L
2
\ L
1
also context-free? Why?
(Hint: remember that A \ B = A B
c
for any sets A, B)
Solution:
Since L
1
is CF, there exists a PDA for it. We will combine this PDA with the
DFA that exists for L
2
to build a PDA for L
1
L
2
. The idea is again to create
a machine that keeps track of the states of both machines at the same time. If
Q
1
, Q
2
are the sets of states of the two machines then Q
1
Q
2
will be the set
of states of the new one. Suppose that the rst machine allows the transition
4

1
(q, a, b) = (q

, c) and the second machine has the transition


2
(r, a) = r

. Then
in the new machine include the transition ((q, r), a, b) = ((q

, r

), c). Repeat
this for all pairs of transitions and set the accepting states to be F
1
F
2
, where
F
1
, F
2
are the accepting states of the two machines.
The fact that L
1
\ L
2
is CF follows immediately from the above since L
c
2
is
also regular and L
1
\ L
2
= L
1
L
c
2
.
Finally, L
2
\ L
1
is not necessarily CF. Suppose that we could prove the
contrary. Then for the special case L
2
=

(which is certainly a regular


language) we would have that

\L
1
is CF, but this is equal to L
c
1
, so we would
have shown that CF languages are closed under complement, a contradiction.
5

Das könnte Ihnen auch gefallen