CSCI 5106: Programming Languages
Spring 2006, University of Minnesota
Homework 2
Posted: January 31, 2006
Due: Before class on February 14, 2006
Problem 1
Problem 2.10 in the book. Your grammar should be one that, in the
sense discussed in class, assigns a parse tree to each expression
that correspond closely to its intended abstract syntax.
Note: One may sometimes want to distinguish between expressions that
yield values of different types within a grammar. For example,
noticing that and is an operator that works only on boolean
expressions, one may want to distinguish between those expressions
that yield boolean values and those that yield integer values. This
kind of `type checking' can be difficult to do through syntax rules
and is typically not the way things are organized in compilers: in
compilers, expressions are constructed in a type neutral way and then
type checking is done over these structures to decide whether or not
they are semantically well formed. Think about this issue and feel
free not to build type checking into your semantic rules; actually a
type neutral grammar would be my own preferred choice.
Problem 2
Draw parse trees with respect to the grammar you have described in
Problem 1 for each of the expressions in Problem 2.11 in the book
and the following expressions:
(i >= 0 + j) and x or y
(i >= 0 + j) and (x or y)
Would your grammar accept the first of the above expressions if the
parentheses were left out? If yes, show the parse tree for it. If no,
explain briefly why not.
Problem 3
Identify the abstract syntax corresponding to each
of the expressions in Problem 2.
Problem 4
Problem 2.15 in the book.
Problem 5
- Describe an abstract syntax representation for each
statement kind described by the syntax rules in Problem 2.14 of the
book. In doing this problem you should first correct a typo: there
should be no semicolon at the end of the first rule for SL
- We would like to represent the abstract syntax that you have
just described internally in a computer; such a representation may
be needed, for instance, by a compiler as it translates statements
into machine code that realize their actions. Explain how you would
represent such abstract syntax in Java or Scheme. If you use
Java, write down a set of class definitions that can be used to
realize this representation. You may assume that you have available
to you an abstract class called Expr that represents the abstract
syntax of expressions that appear in the syntax rules.
Problem 6 (Extra credit)
First read Sections 13.1 and
13.2 in the book.
- Building on Problem 5, write function or method definitions in
the language you chose there that would produce the abstract
syntax of each statement type from the abstract syntax of the
relevant subcomponents. For instance, in Scheme,
you may define a function called genassign that takes
the representation of two abstract syntax trees e1 and
e2 as arguments and returns the representation of an
abstract syntax tree for an assignment statement that has e1
as its lefthand side and e2 as its righthand side and
similarly for the other statement types. In Java, these
`constructors' would be part of relevant class definitions.
- Construct an attribute grammar based on the context free grammar
in Problem 2.14 of the book that produces abstract syntax trees for
the statements that it accepts/parses. You will need to use the
functions defined in the first part in setting attribute values for
expressions.
Last updated on Jan 31, 2006 by gopalan@cs.umn.edu