CS 48, Fall 2010
Homework 1 Solutions
Problem 1
Louden, Exercise 3.5
Write a grammar for Boolean expressions that includes the constants true and false, the operators and,
or, and not, and parentheses. Be sure to give or a lower precedence than and and and a lower precedence
than not and to allow repeated not’s, as in the Boolean expression not not true. Also be sure your
grammar is not ambiguous.
Solution
E ! E or T j T
T ! T and F j F
F ! not F j B
B ! true j false j ( E )
Problem 2
Louden, Exercise 3.9
Show that the following attempt to solve the dangling else ambiguity is still ambiguous (compare with the
solution on page 121):
statement ! if ( exp ) statement j matched-stmt
matched-stmt ! if ( exp ) matched-stmt else statement j other
exp ! 0 j 1
Here is the solution given on page 121:
statement ! matched-stmt j unmatched-stmt
matched-stmt ! if ( exp ) matched-stmt else matched-stmt j other
unmatched-stmt ! if ( exp ) statement j if ( exp ) matched-stmt else unmatched-stmt
exp ! 0 j 1
1
2
Solution
Consider the following string:
if ( 0 ) if ( 0 ) other else if ( 0 ) other else other
We can show two possible leftmost derivations for this, demonstrating the grammar’s ambiguity:
Derivation 1
statement ) if ( exp ) statement
) if ( 0 ) statement
) if ( 0 ) matched-stmt
) if ( 0 ) if ( exp ) matched-stmt else statement
) if ( 0 ) if ( 0 ) matched-stmt else statement
) if ( 0 ) if ( 0 ) other else statement
) if ( 0 ) if ( 0 ) other else matched-stmt
) if ( 0 ) if ( 0 ) other else if ( exp ) matched-stmt else statement
) if ( 0 ) if ( 0 ) other else if ( 0 ) matched-stmt else statement
) if ( 0 ) if ( 0 ) other else if ( 0 ) other else statement
) if ( 0 ) if ( 0 ) other else if ( 0 ) other else matched-stmt
) if ( 0 ) if ( 0 ) other else if ( 0 ) other else other
Derivation 2
statement ) matched-stmt
) if ( exp ) matched-stmt else statement
) if ( 0 ) matched-stmt else statement
) if ( 0 ) if ( exp ) matched-stmt else statement else statement
) if ( 0 ) if ( 0 ) matched-stmt else statement else statement
) if ( 0 ) if ( 0 ) other else statement else statement
) if ( 0 ) if ( 0 ) other else if ( exp ) statement else statement
) if ( 0 ) if ( 0 ) other else if ( 0 ) statement else statement
) if ( 0 ) if ( 0 ) other else if ( 0 ) matched-stmt else statement
) if ( 0 ) if ( 0 ) other else if ( 0 ) other else statement
) if ( 0 ) if ( 0 ) other else if ( 0 ) other else matched-stmt
) if ( 0 ) if ( 0 ) other else if ( 0 ) other else other
3
Problem 3
Louden, Exercise 3.21
A unit production is a grammar rule choice of the form A ! B, where both A and B are nonterminals.
a. Show that unit productions can be systematically eliminated from a grammar to yield a grammar with no
unit productions that generates the same language as the original grammar.
Solution
The following algorithm removes unit productions from the grammar G D .T; N; P; S/:
1 while no changes occur
2 for each unit production A ! B
3 for each production B ! ˛
4 if A ! ˛ … P
5 add A ! ˛ to P
6 for each unit production A ! B
7 remove A ! B from P
To prove that this algorithm does indeed remove all unit productions yet still recognizes the same lan-
guage, consider the grammar that contains the unit production A ! B and productions B ! ˛0 j ˛1 j
j ˛n, where ˛0; ˛1; : : : ; ˛n 2 .T [ N /. Now consider some string
Aı such that S
Aı and
)
; ı 2 .T [ N /. The following derivation occurs using the original grammar:
Aı )
Bı
)
˛ı ; where ˛ 2 f˛0 j ˛1 j j ˛ng :
If we eliminate A ! B using the algorithm above, that rule is replaced with rules of the form A ! ˛
for ˛ 2 f˛0; ˛1; : : : ; ˛ng. Given these new rules, the derivation
Aı )
Bı )
˛ı becomes the direct
derivation
Aı )
˛ı.
b. Do you expect unit productions to appear often in the definition of programming languages? Explain.
Solution
Yes. As we have seen, one way to disambiguate expression grammars involves adding productions such
as E ! E C T j T .
Moreover, even if we were to disambiguate expression grammars in some other way, we would expect to
see unit productions of the sort
stmt ! expr j if -stmt j for-stmt j while-stmt j do-stmt j switch-stmt
4
Problem 4
Slight modification of Louden, Exercise 3.22
C
We write that ˛ ) ˇ if and only if there exists a sequence of derivation steps ˛1 ) ˛2 ) ) ˛ such
C
that n 1, ˛ D ˛1, and ˇ D ˛n. In other words, ) is like ), except that we require at least one step.
C
A cyclic grammar is one in which there is a derivation A ) A for some nonterminal A.
a. Show that a cyclic grammar is ambiguous.
Solution
We will prove this claim by constructing a cyclic grammar and two derivations thereof that produce the
same string. Consider the following grammar snippet, which is trivially cyclical:
A ! Bja
B ! Ajb
Here are two derivations of the string a:
A)a
and
A)B
)A
)a
b. Would you expect grammars that define programming languages to be cyclic? Explain.
Solution
No. Parsing would be a mess because you could construct an arbitrary number of parse trees that have
the same part, repeated.
Problem 5
(Modification of a problem by Pratt and Zelkowitz)
In the South Pacific lies the remote speck of land known as Revis Island. Revis Islanders have their own
language, Totok, and it contains only three sounds: the consonants k and t, and the vowel o (the short form,
5
as in “box”). The grammar is also quite simple, where # indicates a space:
sentence ! sentence # word j word
word ! syllable word syllable j syllable
syllable ! combination j combination consonant j vowel combination j vowel consonant
combination ! consonant vowel
consonant ! k j t
vowel !o
Three people approach Darrelle, a Revis Island native, and attempt to speak Totok. Two of these people
are also Revis Island natives and speak perfect Totok, but one is not and speaks flawed Totok. Here is what
each person says:
Randy says ko#okokototo#kot#tokkoto
Terrell says oktokootok#oto
Chad says tot#ot#okootot#kototkoot
Because he knows the grammar of Totok, Darrelle can pick out the non-native Totok speaker. Which
one of Randy, Terrell, and Chad is the non-native Totok speaker? Show that your answer is correct.
(You might have noticed that names on Revis Island are not necessarily constrained to the Totok lan-
guage. That’s OK. After all, not all names in the U.S. are constrained to the English language.)
Solution
Chad is the non-native speaker. By inspection, we see that a sentence consists of words separated by spaces
(#), in which Chad is correct. Furthermore, a word is either a syllable or a word surrounded by a pair
of syllables, and thus a word must consist of an odd number of syllables. Consider Chad’s fourth word:
kototkoot. We will show that this is not a word in the Totok language by demonstrating that it does not
have an odd number of syllables.
Examining the grammar rules, we see that two vowels may not be adjacent in the same syllable, and
thus the final syllable must be ot. Likewise, two consonants may not be adjacent in the same syllable, and
thus the second-to-last syllable must be ko. The third-to-last syllable can be either ot or tot, which leaves
us the task of creating an odd number of syllables out of kot or ko, which we cannot do according to the
rules of the language.
For completeness, derivations of the two valid sentences are given below.
Leftmost derivation of Randy’s utterance:
sentence ) sentence # word
) sentence # word # word
) sentence # word # word # word
) word # word # word # word
) syllable # word # word # word
) combination # word # word # word
6
) consonant vowel # word # word # word
) k vowel # word # word # word
) ko# word # word # word
) ko# syllable word syllable # word # word
) ko# vowel combination word syllable # word # word
) ko#o combination word syllable # word # word
) ko#o consonant vowel word syllable # word # word
) ko#ok vowel word syllable # word # word
) ko#oko word syllable # word # word
) ko#oko syllable syllable # word # word
) ko#oko combination consonant syllable # word # word
) ko#oko consonant vowel consonant syllable # word # word
) ko#okok vowel consonant syllable # word # word
) ko#okoko consonant syllable # word # word
) ko#okokot syllable # word # word
) ko#okokot vowel combination # word # word
) ko#okokoto combination # word # word
) ko#okokoto consonant vowel # word # word
) ko#okokotot vowel # word # word
) ko#okokototo# word # word
) ko#okokototo# syllable # word
) ko#okokototo# combination consonant # word
) ko#okokototo# consonant vowel consonant # word
) ko#okokototo#k vowel consonant # word
) ko#okokototo#ko consonant # word
) ko#okokototo#kot# word
) ko#okokototo#kot# syllable word syllable
) ko#okokototo#kot# combination consonant word syllable
) ko#okokototo#kot# consonant vowel consonant word syllable
) ko#okokototo#kot#t vowel consonant word syllable
) ko#okokototo#kot#to consonant word syllable
) ko#okokototo#kot#tok word syllable
) ko#okokototo#kot#tok syllable syllable
) ko#okokototo#kot#tok combination syllable
) ko#okokototo#kot#tok consonant vowel syllable
) ko#okokototo#kot#tokk vowel syllable
) ko#okokototo#kot#tokko syllable
7
) ko#okokototo#kot#tokko combination
) ko#okokototo#kot#tokko consonant vowel
) ko#okokototo#kot#tokkot vowel
) ko#okokototo#kot#tokkoto
Leftmost derivation of Terrell’s utterance:
sentence ) sentence # word
) word # word
) syllable word syllable # word
) vowel consonant word syllable # word
) o consonant word syllable # word
) ok word syllable # word
) ok syllable word syllable syllable # word
) ok combination word syllable syllable # word
) ok consonant vowel word syllable syllable # word
) okt vowel word syllable syllable # word
) okto word syllable syllable # word
) okto syllable syllable syllable # word
) okto combination syllable syllable # word
) okto consonant vowel syllable syllable # word
) oktok vowel syllable syllable # word
) oktoko syllable syllable # word
) oktoko combination syllable # word
) oktoko vowel consonant syllable # word
) oktokoo consonant syllable # word
) oktokoot syllable # word
) oktokoot vowel consonant # word
) oktokooto consonant # word
) oktokootok# word
) oktokootok# syllable
) oktokootok# vowel combination
) oktokootok#o combination
) oktokootok#o consonant vowel
) oktokootok#ot vowel
) oktokootok#oto
8
Problem 6
(Modification of a problem by Sethi)
The following grammar generates numbers in binary notation:
S ! S 0 j A1 j 0
A ! B0 j S1 j 1
B ! A0 j B1
The numbers that this grammar generates have a particular property. What is this property? Prove that
this grammar generates all numbers with this property, and that it generates only numbers with this property.
Solution
For any string x over f0; 1g, let Œx denote the number represented by x in binary. For example, Œ0101 D 5.
The property of the strings generated by the grammar is that x is in the language generated by the
grammar if and only if Œx 0 and Œx 0 .mod 3/. In other words, the grammar generates all strings that,
interpreted as binary numbers, are nonnegative and divisible by 3, and it generates only these numbers.
To see why the grammar has this property, we claim that for any nonempty string x over the terminals
0 and 1,
1. S ) x if and only if Œx 0 .mod 3/,
2. A ) x if and only if Œx 1 .mod 3/,
3. B ) x if and only if Œx 2 .mod 3/.
To prove the claim, let jxj denote the length of x. The proof is by induction on jxj.
Basis: The basis occurs when jxj D 1, so that x D 0 or x D 1. If x D 0, then the only way to generate x
with the grammar uses the derivation S ) 0. If x D 1, then the grammar cannot generate x, because since
x ends with a 1, the first derivation step would have to be S ) A1. But any string derived from A has at
least one terminal, and so we cannot derive from S a string of length 1 that ends with a 1. Thus, if S ) x
and jxj D 1, then Œx 0 .mod 3/.
We can derive A ) 1, and as we’ve seen, the only length 1 string we can derive from A is 1. Thus if
A ) x and jxj D 1, then Œx 1 .mod 3/.
Because the productions for B are B ! A0 and B ! B1, and because any string derived from A or B
has at least one terminal, we see that any string derived from B has length at least 2. Thus, it is vacuously
true that if B ) x and jxj D 1, then Œx 2 .mod 3/.
Inductive step: Let jxj D k > 1, and assume that the claim holds for all strings of length k 1. Since
jxj > 1, we can write x D ya, where a 2 f0; 1g and y is a string of k 1 terminals. By the inductive
hypothesis, S ) y if and only if Œy 0 .mod 3/, A ) y if and only if Œy 1 .mod 3/, and B ) y if
and only if Œy 2 .mod 3/. We have six cases, based on the productions:
9
If x D y0 and Œy 0 .mod 3/, then by the inductive hypothesis, S ) y. Since x D y0, we have
that Œx D 2 Œy 0 .mod 3/. We have the derivation S ) S0 ) y0 D x, and so we have S ) x
and Œx 0 .mod 3/.
If x D y1 and Œy 0 .mod 3/, then by the inductive hypothesis, S ) y. Since x D y1, we have
that Œx D 2 Œy C 1 1 .mod 3/. We have the derivation A ) S1 ) y1 D x, and so we have
A ) x and Œx 1 .mod 3/.
If x D y0 and Œy 1 .mod 3/, then by the inductive hypothesis, A ) y. Since x D y0, we have
that Œx D 2 Œy 2 .mod 3/. We have the derivation B ) A0 ) y0 D x, and so we have B ) x
and Œx 2 .mod 3/.
If x D y1 and Œy 1 .mod 3/, then by the inductive hypothesis, A ) y. Since x D y1, we have
that Œx D 2 Œy C 1 0 .mod 3/. We have the derivation S ) A1 ) y1 D x, and so we have
S ) x and Œx 0 .mod 3/.
If x D y0 and Œy 2 .mod 3/, then by the inductive hypothesis, B ) y. Since x D y0, we have
that Œx D 2 Œy 1 .mod 3/. We have the derivation A ) B0 ) y0 D x, and so we have A ) x
and Œx 1 .mod 3/.
If x D y1 and Œy 2 .mod 3/, then by the inductive hypothesis, B ) y. Since x D y1, we have
that Œx D 2 Œy C 1 2 .mod 3/. We have the derivation B ) B1 ) y1 D x, and so we have
B ) x and Œx 2 .mod 3/.
These six cases together prove the claim.
By the first part of the claim (S ) x if and only if Œx 0 .mod 3/), we have that x is in the language
generated by the grammar if and only if Œx 0 .mod 3/.