CS3050 Theory of Computation: Assignment 3 (Context-Free Grammars) September 25, 2019
CS3050 Theory of Computation: Assignment 3 (Context-Free Grammars) September 25, 2019
Problems
1. Give context-free grammars for the following languages
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
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,
(Equivalently, L0 consists of all those strings which can be completed to a balanced parentheses
string.) Design an unambiguous grammar for L0 .
S → Sab C | ASbc
Sab → aSab b | Λ
Sbc → bSbc c | Λ
A → aA | Λ
C → cC | Λ
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.