In this assignment you finish the semantic analysis phase of your compiler. After this step, the front-end of your Extended MiniJava compiler is completed. This means that your compiler is now capable of rejecting all invalid programs, and accepting all valid programs. In the final phase you will be able to turn these valid programs into assembly code that runs on the JVM, just like javac. This is a great achievement!
If you have any questions about this assignment please do not hesitate to ask the instructor.
A valid eMiniJava program has the following properties:
Your goal in this assignment is to enforce all the constraints not enforced already by the previous phases.
Note: The language and the type rules presented in the course may differ from the rules of Extended MiniJava. If there are any differences, please use the description on the current page for your implementation, and not the rules in the lecture. Of course, feel free to clarify with the instructor if you have any changes.
The following primary types exist in eMiniJava (note that we prefix them with T to differentiate them from the tree nodes with the same name, for instance):
Additionnally, we have object types:
We define a subtyping relation on these types. All primary types are subtypes of themselves and of no other type. For instance:
All object types are subtypes of themselves and the special “Object” object type. The subtyping relation is also transitive.
With this in mind, we give some of the non-trivial typing constraints. This is naturally not an exhaustive list of what you should implement, but we expect you to be able to deduce the other rules unambiguously yourself (if in doubt about a rule, please ask the instructor).
The “+” operator can represent integer addition, or string concatenation. If the types of e1 and e2 are T1 and T2 respectively, we have for the type Ts of e1 + e2:
All other values for T1 and T2 should result in type errors.
The “==” operator is also overloaded. Expression e1==e2 is type correct if and only if one of the following two cases apply:
Note that it is not type correct to compare a primitive type to a class type.
Note that strings and arrays of integers are considered primitive!
Consider the following code.
class A {} class B {}
Let e1:T1 and e2:T2. Then the following table summarizes some of the cases.
T1 | T2 | type checks? |
---|---|---|
int | int | yes |
String | String | yes |
String | A | no |
A | int | no |
A | B | yes |
A | int[] | no |
int | int[] | no |
The dereferenced object must be of an object type, and its class must declare the method called. The number of arguments must of course match the number of parameters. The arguments passed must be subtypes of the declared parameters (matching one-by-one).
Assignment of an expression of type T can only be done to a variable of type S such that T <: S.
Assignment to array elements can only be done through an array variable, and the index must be an integer expression. The assigned value must be an integer.
this is always considered to carry the object type corresponding to the class where it occurs.
The returned expression must be of a subtype of the declared return type.
We will consider println
calls to be valid if the argument is an integer, a Boolean or a string. The type of a println
expression is void
.
Here are the steps we suggest you take:
Since we are not imposing any code structure for your programs, it is extremely important for your compiler to exactly follow a fixed interface as described here. This allows us to uniformly run and test all the projects from different groups of students. Command-line is the primary interface for your users to interact with your compiler. A general form for the command-line interface is as follows:
emjc [options] <source file>
For this phase of assignment, there are six possible options. If the user does not provide a correct option, the compiler treats it the same as the –help option.
––help
: Prints a synopsis of options––pp
: Pretty-prints the input file to the standard output––lex
: Generates output from lexical analysis as described in Assignment 2.––ast
: Generates output from syntactic analysis as described in Assignment 3.––name
: Generates output from name analysis as described in Assignment 4––type
: Generates output from type analysis
After executing the compiler with the option ––type
for the source file filename.emj
the compiler either accepts the program as a good eMiniJava program with printing out the following line:
Valid eMiniJava Program
Or, it prints out a set of errors (such as unknown identifier). Like the previous phases the format of an error message is the following:
<line>:<column> error:<description>
where <line> and <column> indicate the beginning position of the error, and <description> details the error.
It is very important that your compiler does not stop at the first type error.
The deadline of the this assignment is April 4th at 11:59pm (https://mycourses.rit.edu). Please upload all your files as a single zip file. You should include a readme.txt file in your package to describe how your source code can be compiled and built. Ideally your project should include a user-friendly compiling technique with Makefile, Ant or similar tools.
When grading, we are serious about simple programming errors. You are writing a compiler to check the programs of other people, so your compiler should not have simple errors itself! Your compiler should never crash for an incorrect input. We expect your compiler to give a comprehensive error when the user does not provide a valid input to it.