CS 181 — Winter 2005
Formal Languages and Automata Theory
Problem Set #5 Solutions
Problem 5.1.
(10 points)
Recall the shuffle operator from Homework 2. Prove or disprove:
1. Context free languages are closed under shuffle.
Solution
We will show that context free languages are not closed under shuffle! Consider the languages
L
1
=
{
0
n
2
n

n
≥
0
}
and
L
2
=
{
1
m
3
m

m
≥
0
}
.
Then the shuffle of the two languages
(denoted
SHUFFLE
(
L
1
, L
2
)) will contain the words that have an equal number of 0’s and
2’s, and equal number of 1’s and 3’s and morover, no 2 occurs before a 0 and no 3 occurs
before a 1.
Assume that
SHUFFLE
(
L
1
, L
2
) is contextfree. Then we know that context free languages
are closed under intersection with regular languages. Consider intersecting
SHUFFLE
(
L
1
, L
2
)
with 0
*
1
*
2
*
3
*
. Then we get precisely the language
{
0
n
1
m
2
n
3
m

n, m
≥
0
}
.
However, notice that this language is not context free.
Intuitively, this is because we will
need to recall
n
before
m
even though
m
is the last number we stored. This is not possible
using a stack! To formally show this language is not context free, we can use the pumping
lemma but we leave this as an exercise.
2. Let
L
be context free and
R
regular. Then the shuffle of
L
and
R
is context free.
Solution
We will show that in fact the shuffle of
L
and
R
is contextfree. We do this by building a
PDA for
SHUFFLE
(
L, R
) given a PDA for
L
and a DFA for
R
.
Let
P
= (
Q
L
,
Σ
,
Γ
, δ
L
, q
L
, F
L
) be a PDA accepting
L
and
M
= (
Q
R
,
Σ
, δ
R
, q
R
, F
R
) be a DFA
accepting
R
.
Our construction of the PDA for shuffle will use ideas from the construction
showing the context free languages are closed under intersection with regular languages and
from our construction showing regular languages were closed under shuffle. In particular, we
define the PDA
N
= (
Q
L
×
Q
R
,
Σ
,
Γ
, δ,
(
q
L
, q
R
)
, F
L
×
F
R
)
.
The transition function nondeterministically decides whether or not to feed the next input
symbol to
P
or to
M
.
In other words, ((
q
0
, p
0
)
, b
)
∈
δ
((
q, p
)
, w, a
) if and only if one of the
following hold
•
(
q
0
, b
)
∈
δ
L
(
q, w, a
) and
p
0
=
p
. In this case, we update the PDA and let the DFA idle.