CDT314 Formal Languages, Automata and Theory of Computation, Mälardalen University – School of Innovation, Design and Engineering,
12 Dec 2013 Solutions
Midterm 2 – Context Free Languages - Solutions
Problem 1 (4p) For a Context-free grammar G = (S, A, B, 0, 1, P, S) where P is the set of
productions:
S 0A | 1B |
A 1S | 0AA
B 0S | 1BB
a) What language does the grammar generate?
b) Give a leftmost derivation of the string 0011.
c) Give a rightmost derivation of the string 001011.
d) Draw a parse tree for the string 1001.
Solution
a) From the production rules we see that A generates strings with one more 1 than 0, B generates strings
with one more 0 than 1, and S generates the strings with equal numbers of 0's and 1's.
b) S 0A 00AA 001SA 001A 0011S 0011
c) S 0A 00AA 00A0AA 00A0A1S 00A0A1 00A01S1 00A011 001S011
001011
d)
S
/ \
1
B
/ \
0
S
/ \
0
A
/ \
1
S
|
CDT314 Formal Languages, Automata and Theory of Computation, Mälardalen University – School of Innovation, Design and Engineering,
12 Dec 2013 Solutions
Problem 2 (4p) For the following language
L2 = {w : na(w) = nb(w) + n c(w)}, Σ = {a, b, c}.
(a) Give a Push-down automaton with a short explanation.
(b) Give a Context-free grammar with a short explanation.
Solution
(a) For the PDA, we use an X on the stack to record the number of as, and we use Y to record the joint
number of bs and cs. When we see an a, we have a choice: either push an X or pop a Y (if it is on the
stack). When we see a b, we can either pop an X or push a Y. If there are as many bs and cs together as
there are as, then the number of Ys will match the number of X s.
(b) The following is a CFG that will produce strings with na(w) = nb(w) + n c(w):
S → SS | aSc | cSa | aSb | bSa |
CDT314 Formal Languages, Automata and Theory of Computation, Mälardalen University – School of Innovation, Design and Engineering,
12 Dec 2013 Solutions
Problem 3 (6p)
For each of the following languages, show whether the language is Context-free. Justify your answer
(e.g. give a PDA, a CFG, or apply the Pumping lemma).
(a) L1 = {an bm : n ≥ 0, n m}, Σ = {a, b}.
(b) L2 = {w R #z : w is a substring of z, w, z ∈ {a, b}∗ }, Σ = {a, b, #}.
(c) L3 = {p#s : p is a substring of s, p, s ∈ {a, b}∗ }, Σ = {a, b, #}.
Solution:
(a) L1 = {an bm : n ≥ 0, n m}, Σ = {a, b} IS Context-free.
We can construct the following NPDA:
L = { anbm : n ³ 0, n ¹ m}.
a,z/1z
a,1/11
q0
b,1/ l
q1
b,1/ l
l,1/1
b,z/ z
b,z/ z
q3
b,z/ z
q2
CDT314 Formal Languages, Automata and Theory of Computation, Mälardalen University – School of Innovation, Design and Engineering,
12 Dec 2013 Solutions
(b) L2 = {w R #z : w is a substring of z, w, z ∈ {a, b}∗ }, Σ = {a, b, #} IS Context-free.
R
∗
We can give a CFG for L2 . The strings in L2 have the form w #uwz, where u, w, z ∈ {a, b} .
A string S of this form can be written as Az, where z ∈ {a, b}∗ and A has the form w R #uw. We
can write A as w R Bw, where B has the form #(a + b)∗ and w ∈ {a, b}∗ . This gives the
following CFG:
S → Sa | Sb | A
A → aAa | bAb | B
B → Ba | Bb | #
(c) L3 = {p#s : p is a substring of s, p, s ∈ {a, b}∗ }, Σ = {a, b, #} is NOT Context-free.
We will prove it by Pumping Lemma. Consider an arbitrary pumping length m, and choose a
string w = am bm #am bm . Then w ∈ L3 but we will show that no matter how we write w =
uvxyz with |vyx| ≤ m, |vy| > 0, we can pump it out of L3 . We look at three cases:
Case 1: vyx is in the first half (before #) of am bm #am bm . We choose i = 2, thus uv2 xy2 z
looks like p#s where |p| > |s| so that p cannot be a substring of s. Hence uv 2 xy2 z is not in
L3
Case 2: vwx is in the second half (after #) of am bm #am bm. We choose i = 0, thus
uv 0 xy0 z looks like am bm #ai bj where i < m or j < n (or both). an bn can not be a substring of
ai bj .
Case 3: vwx is in the middle part of am bm #am bm (not intersecting the initial block of as and
the final block of bs). If vy contains #, then we choose i = 0, thus uv 0 xy0 z contains no #
so that uv 0 xy0 z is not in L3 . Else, v must be in left bs of # and x in the right as of #.
If v ≠ we choose i = 2, then there are more bs on the left of # than on the right of # in
uv 2 xy2 z. If x ≠ , we choose i = 0, then there are more as on the from # than on the right of
# in uv 0 xy0 z. In those cases, uvi wxi y is out of L3.
This covers all the cases, so L3 is not Context-free.
CDT314 Formal Languages, Automata and Theory of Computation, Mälardalen University – School of Innovation, Design and Engineering,
12 Dec 2013 Solutions
© Copyright 2026 Paperzz