CS3050 Theory of Computation
Assignment 3 (Context-Free Grammars)
September 25, 2019
The short quiz for this assignment will be on Monday 30/Sep/2019.
Problems
1. Give context-free grammars for the following languages
(a) The set of odd-length strings with middle symbol a.
(b) The set of even-length strings with middle two symbols equal.
(c) The set of odd-length strings with the first, middle and last symbol equal.
(d) All strings which have unequal number of a’s and b’s
(e) L = {ai bj : i ≤ j ≤ 2i}
(f) L = {ai bj : i/2 ≤ j ≤ 3i/2}
(g) L = {ai bj ck : i = j}
(h) L = {ai bj ck : i = k}
(i) L = {ai bj ck : i > k}
(j) L = a∗ b∗ c∗ − {ai bi ci : i ≥ 0}
Further, prove that none of the above languages is regular.
2. Consider the grammar G below
S → S + S | S − S | S ∗ S | S/S | (S) | a
(a) How many distinct parse trees does the string a + a ∗ a + a/a have in G?
(b) How many distinct parse trees does the string a + (a ∗ a) + (a/a) have in G?
1 2n
(c) ? Show that string a + · · · + a (n pluses) has n+1 n distinct parse trees in G?
(Hint. Catalan numbers)
(d) Design an unambiguous grammar for L(G).
3. Show that the following grammar G generates the language L = {x : na (x) = nb (x)} over the
alphabet Σ = {a, b}.
G : S → aBS | bAS | Λ
A → a | bAA
B → b | aBB
Further, prove that G is unambiguous.
4. Let L be the language of all balanced parentheses strings over the alphabet Σ = {(, )}. Show that
the usual grammar
S → SS | (S) | Λ
for L is ambiguous. Design an unambiguous grammar for L and prove that it is unambiguous.
Let L0 be the language of prefixes of L That is,
L0 = {x ∈ Σ∗ : ∃y ∈ Σ∗ such that xy ∈ L}.
(Equivalently, L0 consists of all those strings which can be completed to a balanced parentheses
string.) Design an unambiguous grammar for L0 .
5. Prove Theorem 4.28 (on eliminating unit productions) in Martin’s book.
6. Convert the following CFG to Chomsky Normal Form
S → Sab C | ASbc
Sab → aSab b | Λ
Sbc → bSbc c | Λ
A → aA | Λ
C → cC | Λ
Call the resulting grammar in Chomsky Normal Form as G.
Show a run of the CYK algorithm with G as the grammar and x = aabbcc as the input.
7. Converting a CFG to Chomsky Normal Form involves four steps:
(S1). Eliminating the null productions,
(S2). Eliminating the unit productions,
(S3). Introducing a new variable for each symbol and using it to replace any symbol occurrences
with variable occurrences in long (length at least 2) productions, and
(S4). Replacing all long productions (length at least 3) with a chain of binary productions.
Can we do these four steps in any other order? For example, S2, S1, S3, S4? or S3, S4, S1, S2?
The length of a production rule A → α is defined as |α|. The length of a grammar is the sum of
the lengths of each of its production rules. Show that for every context-free grammar G of length
l, there exists a grammar G0 such that (i) G0 is in Chomsky Normal form, (ii) L(G0 ) = L(G) \ {Λ},
and (iii) length of G0 is O(l2 ). Hint. Play with the order of S1 to S4.