You are on page 1of 3

Tutorial 4: Context-Free Languages

CFG, Chomsky Normal Form, PDA, Conversion CFG to PDA

Exercise 1 Consider the following CFG:

E →E+T |T
T →T ×F |F
F → (E) | a

Give parse trees and derivations for each string.

• a • a+a

• a+a+a • ((a))

Exercise 2 We admit the following result: ”The class of context-free language is not closed under
intersection”. Use DeMorgan’s law to show that the class of context-free languages is not closed
under complementation.
De Morgan’s Laws:
De Morgan’s Laws describe the relationship between set union (∪), set intersection (∩), and
set complement. They can be stated as follows:

(A ∪ B) = (Ā) ∩ (B̄) (1)


(A ∩ B) = (Ā) ∪ (B̄) (2)

Exercise 3 Answer each part for the following context-free grammar G.

R → XRX | S
S → aT b | bT a
T → XT X|X|ε
X→a|b

a. What are the variables of G ? i. True or False: T ⇒ T .


b. What are the terminals of G ? ∗
j. True or False: XXX ⇒ aba.
c. Which is the start variable of G? ∗
k. True or False: X ⇒ aba.
d. Give three strings in L(G). ∗
l. True or False: T ⇒ XX.
e. Give three strings not in L(G).
m. True or False: T ⇒ XXX.
f. True or False: T ⇒ aba.


g. True or False: T ⇒ aba. n. True or False: S ⇒ ε.

h. True or False: T ⇒ T . o. Give a description in English of L(G).

Exercise 4 Give context-free grammars that generate the following languages. In all parts, the
alphabet Σ is {0, 1}.

1
a. {w | w contains at least three 1 s }

b. {w | w starts and ends with the same symbol }

c. {w | the length of w is odd }

d. {w | the length of w is odd and its middle symbol is a 0}

e. w | w = wR , that is, w is a palindrome }




f. The empty set

g. The set of strings over the alphabet | {a, b} with more a’s than b’s

h. The complement of the language {an bn | n ≥ 0}

i. w#x | wR is a substring of x for w, x ∈ {0, 1}∗ }




j. {x1 #x2 # · · · #xk | k ≥ 1, each xi ∈ {a, b}∗ , and for some i and j, xi = xR
j

Exercise 5 • Give informal descriptions and state diagrams of pushdown automata for the
languages in Exercise 4 (a-f ).

• Give informal English descriptions of PDAs for the languages in Exercise 4(g-j)

Exercise 6 Give a context-free grammar that generates the language

A = ai bj ck | i = j or j = k where i, j, k ≥ 0 .


Is your grammar ambiguous? Why or why not?

Exercise 7 Give an informal description of a pushdown automaton that recognizes the language
A in Exercise 6

Exercise 8 Using the conversion CFG to PDA:


• Convert the CFG G4 given in Exercise 1 to an equivalent PDA

• Convert the CFG G given in Exercise 3 to an equivalent PDA,

Exercise 9 Let G = (V, Σ, R, S) be the following grammar. V = {S, T, U }; Σ = {0,# }; and R


is the set of rules:
S → TT | U
T → 0T |T 0|#
U → 0U 00 | #
a. Describe L(G) in English.

b. Prove that L(G) is not regular.

Exercise 10 Convert the following CFG into an equivalent CFG in Chomsky normal form.
A → BAB|B|ε
B → 00 | ε
Exercise 11 Give a counter-example to show that the following construction fails to prove that
the class of context-free languages is closed under star. Let A be a CFL generated by the CFG
G = (V, Σ, R, S). Add the new rule S → SS and call the resulting grammar G′ . This grammar is
supposed to generate A∗ .

2
Exercise 12 Show that the class of context-free languages is closed under the regular operations,
union, concatenation, and star.

Exercise 13 Use the results of Exercise 12 to give another proof that every regular language is
context free, by showing how to convert a regular expression directly to an equivalent context-free
grammar.

Exercise 14 We admits the following result: ”The language {an bn cn | n ≥ 0} is not context-free”.

a. Let C be a context-free language and R be a regular language. Prove that the language C ∩ R
is context free.

b. Let A = {w | w ∈ {a, b, c}∗ and w contains equal numbers of a’s, b’s, and c’s }. Use part
(a) to show that A is not a CFL.

Exercise 15 Let CFG G be the following grammar.

S → aS b| bY |Y a
Y → bY |aY |ε

Give a simple description of L(G) in English. Use that description to give a CFG for L(G), the
complement of L(G).

Exercise 16 Let A/B = {w | wx ∈ A for some x ∈ B}. Show that if A is context free and B is
regular, then A/B is context free.

Exercise 17 • Let Σ = {a, b}. Give a CFG generating the language of strings with twice as
many a’s as b’s. Prove that your grammar is correct.

• Let C = {x#y | x, y ∈ {0, 1}∗ and x ̸= y}. Show that C is a context-free language.

You might also like