Homework Assignment 2.
CS 316, Spring 2008.

Due on Thursday, January 31.

1.) Do Exercise 2.9, parts (a) and (b) from "Programming Language
Pragmatics" (page 98). (The $$ at the end of the first production
stands for "end of file" and it is described in the first paragraph
of Section 2.3.1, page 64.)


2.) Consider the following grammar.

S -> round square | outer
round -> '(' round ')' | '(' ')'
square -> '[' square ']' | '[' ']'
outer -> '(' outer ']' | '(' inner ']'
inner -> ')' inner '[' | ')' '['

a) Give an English description of the strings defined by this grammar.
b) Show that the grammar is ambiguous.
Note: The language defined by this grammar is inherently ambiguous, that is, any
grammar for this language will be an ambiguous grammar.


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.) Given the following BNF

expr -> '(' list ')' | 'a'
list -> list ',' expr | expr

a) Write EBNF rules for this language.
b) Draw a parse tree for the string "((a, a), a, (a))".


6.) Convert the following EBNF to BNF.

S -> A { 'b' A }
A -> 'a' | 'a' [ 'c' ] A