UCLA
CS 239 Homework Winter 2004
The grammars for MicroJava, NanoJava, Interfaces, and MiniScheme
are needed for the homework.
Notice that the grammars are available both in html and in JavaCC notation.
Homework 1: Java Interpreter
Use JTB/GJ and JavaCC and write in Java one or more visitors which interpret a
MicroJava program.
You can assume that the MicroJava program can be compiled by javac.
Your main file should be called Interpret.java, and if P.java
contains a program to be interpreted,
then
java Interpret < P.java
should
have the same behavior as if
one compiles P.java with javac,
and then runs P.class with java.
Homework 2: Java Structural Subtyping
If A,B are names of interfaces, then a subtype query is of the form
A <= B ?
This query has the answer Yes if A is a subtype
of B in the structural subtyping order for Java described below,
and No otherwise.
A grammar for a list of Java interfaces together with a list of subtype queries
is given in the file interface.jj.
Use JTB/GJ and JavaCC and write in Java one or more visitors which decides
the subtype queries.
Your main file should be called DecideSubtyping.java, and if P
contains an input file
then
java DecideSubtyping < P
should print a sequence of Yes and No, each on a new line.
In this Homework, we view a Java interface as an infinite, regular tree
which is obtained by replacing occurrences of interface names
by their definition.
A path is in the set
(Identifier {0,1})*
where the identifiers are method names,
0 denotes ''argument type'',
and 1 denotes ''result type''.
We use id to range over Identifier.
Labels are from the set S = {Int,Bool,Void,->,Interface}.
An interface can now be represented as a partial function
t : (Identifier {0,1})* -> S
with domain dom(t) satisfying
-
dom(t) is nonempty and prefix-closed;
-
if t(a) = ->, then a0, a1 are in dom(t);
-
if a0 is in dom(t), then t(a0) is in {Int,Bool,Void,Interface};
-
if a1 is in dom(t), then t(a1) is in {Int,Bool,Void,Interface};
-
if a id in dom(t), then t(a id) = ->.
We can now define a structural subtyping order of Java interfaces
by combining the ideas from structural subtyping of function types
with the idea that an interface with a certain number of fields
should be a subtype of an interface with fewer fields.
For example, we want to have
A <= B ? // No
B <= A ? // Yes
C <= D ? // No
D <= C ? // Yes
interface A {
A m(int a);
}
interface B {
B m(int a);
void p(boolean a);
}
interface C {
int q(B a);
}
interface D {
int q(A a);
void r(int a);
}
We leave it to the reader to work out the details of the definition
of structural subtyping.
Homework 3: Scheme Type Inference
Consider a type system where types are generated from the grammar:
t ::= t -> t | int | boolean | a
where a ranges over a set of type variables.
Use JTB/GJ and JavaCC and write in Java one or more visitors which from
a MiniScheme program produces the principal type,
or decides that no type exists.
Your main file should be called Infer.java, and if P.scm
contains a MiniScheme program,
then
java Infer < P.scm
should print either the principal type,
or
Program does not type check
Homework 4: Java CPS Transformer
Use JTB/GJ and JavaCC and write in Java one or more visitors which translate
a MicroJava program to a NanoJava program.
You can assume that the MicroJava program can be compiled by javac.
Your main file should be called CPS.java, and if P.java
contains a MicroJava program to be translated,
then
java CPS < P.java > Q.java
creates the NanoJava program Q.java which has the same behavior as P.java.
Homework 5: Java Flow-Directed Inliner
The goal of the homework is to write a MicroJava optimizer
which inlines some method calls.
The idea is to do a flow analysis
and then use the computed information as the basis for replacing
method calls with inlined code.
You should only inline method calls for which the flow analysis
has determined that there is a unique target method.
Use the strategy described in the paper
Type-Safe Method Inlining
by Neal Glew and Jens Palsberg.
Use JTB/GJ and JavaCC and write in Java
one or more visitors which transforms
a MicroJava program to a MicroJava program in such a way that some method calls
get inlined.
You can assume that the MicroJava program can be compiled by javac.
Your main file should be called Optimize.java, and if Prg.java
contains a program to be optimized,
then
java Optimize < Prg.java > OptimPrg.java
creates the program OptimPrg.java which has the same
behavior as Prg.Java,
and which is like Prg.Java,
except that some method calls have been inlined.