Deadline#
Your work should be committed and pushed to your forked gitlab repo by 11:59pm on 3 April 2023.
This project is worth 8% of total course mark.
Description#
Implement semantic analysis for MoJo—see Mojo semantics specification here.
Getting Started#
You should fork the Project 3 repository on gitlab.
This project includes new source code that implements scaffolding for
the semantic analysis phase of the compiler. Remember that the link
between the Project 2 and Project 3 is the correct construction of AST
nodes from Absyn.java.
- The framework for building the AST and printing out the results is
the same as before, in
src/mojo/Absyn.java, with slight modifications to add fields for the types of expressions. - You do not need to edit
src/mojo/Absyn.java.
Provided Parser Solution#
The Project repo also includes a binary that implements a complete
parser in bin/mojo/parse/*.class. You may choose to base your
Project 3 work on the provided parser, or continue using your own
parser. It is expected that you do not try to reverse engineer the
class files of the provided parser to improve your score on Project 2,
although you may run them to understand why your parser might be
broken.
- You can use your own parser by copying your
src/mojo/Parser.jjfile from Project 2 intosrc/mojofor Project 3. - You will have to modify the
Makefileto make JavaCC regenerate your parser source if you modify your .jj file while working on this project. - The grading script will work with either parser as long as you put its source in the proper place.
Where to Add Your Code#
All the action in this project is in src/mojo/Semant.java. This
includes the driver for running semantic analysis.
- You should only need to edit
src/mj/Semant.javato complete this project. - You task is to complete the various methods marked with
TODOcomments. The comments give some sense of what is needed for eachTODO. - Use the command
maketo build the project. - Run the semantic checker with the command:
java -cp bin mojo.Semant <input_file.mj>
This will print the symbol table (i.e., all scopes in the program) to
stdout. If there are errors they will be emitted to stderr.
There is Lots of Code to Help You Here#
The provided framework includes utilities that implement a
representation of the symbol table (i.e., scopes) mojo.Scope, as well
as types (i.e., classes, primitives, arrays, etc.)
mojo.Absyn.Type. You should use these classes and not write your
own. Finally, mojo.Semant has all of the structure you need to
perform type checking of declarations, expressions, and
statements. These contain all the functionality that you need to
complete this project. Be sure to read the existing code carefully as
it will illustrate use of the framework and give you ideas on how to
use it to implemeht the missing features in mojo.Semant tagged with
TODO.
Expected Output#
The class mojo.Semant contains a main method that loads files,
invokes the parser and runs the semantic analysis phase. Unlike
previous projects, the grading script checks both standard out and
standard error—it checks that your semantic analyzer builds the
proper scopes and also generates appropriate error messages.
Make sure your program does not add any extraneous output to standard output. Make sure to run against the provided tests (we will add some more). You can still add debugging output to standard error. The grading script just greps for any expected semantic error in standard error. Make sure to use the provided error methods, as they give good error messages, and the script actually checks their text!