Homework Assignment 3.
CS 316, Spring 2003.

Due on Tuesday, February 11, 2003.

1.) Do Exercise 2.6, parts (a) and (b) from "Programming Language
Pragmatics" (page 99). (You can ignore the $$ at the end of the first
production. It stands for "end of file" and it is described in the first
paragraph of Section 2.2.3, page 51.)


2.) Consider the following, one production grammar.

S -> SS | (S) | ( )

a) Give an English description of the words defined by this grammar.
b) Show that the grammar is ambiguous.


3.) Use the following grammar to parse the following strings.
If a string does not parse, briefly explain why.

expr -> expr + term | expr - term | term
term -> term * factor | term / factor | factor
factor -> ( expr ) | number | identifier
number -> digit | number digit
digit -> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
identifier -> letter | identifier letter | identifier digit
letter -> a | b | ... |x | y | z

a) x - y + z

b) x - (y + z)

c) x / ( 3 * y ) / z

d) x * ( - y )


4.) a) Modify the grammar in the last example so that it allows
unary minuses in the following way. At most one unary minus is
allowed before a number, an identifier, or a left parenthesis, so
- 2 + - 3
2 - - x
x * - ( - 2 )
- y
are all allowed but - - 3 and x + - - y are not allowed.

b) Use your grammar from part (a) to parse x * - ( - 2 + z ).

c) Does your grammar give the unary minus higher precedence than
the binary operators? Explain your answer.


5.) Write a grammar for the language consisting of strings that
have n copies of the letter a followed by the same number of copies
of the letter b, where n > 0. For example, the strings ab, aaabbb,
aaaaaabbbbbb, are in the language, but a, abb, ba, aaabb are not.