The Semantic Analyzer Checkpoint

Due:
11:00 pm, Monday April 5, 2021

Max grace days: 2

Overview

For this assignment you will write a partial semantic analyzer. Among other things, this involves traversing the abstract syntax tree and the class hierarchy. You will reject all Cool programs that do not comply with the Cool type system.

Specification

You must create two artifacts:

  1. A program that takes a single command-line argument (e.g., file.cl-ast). That argument will be an ASCII text Cool abstract syntax tree file. Your program must either indicate that there is an error in the input or emit file.cl-type, a class map. Your program will consist of a number of OCaml files. The starter code contains a file named semantic_analysis.ml; this is file that you need to edit.
  2. A plain ASCII text file called README describing your design decisions. See the grading rubric. A few paragraphs should suffice.

Considerations:

Remember that you do not have to match the English prose of the reference compiler's error messages at all. You just have to get the line number right.

Semantic checks are unordered — if a program contains two or more errors, you may indicate whichever you like. You can infer from this that all of our test cases will contain at most one error.

The .cl-type File Format

If there are no errors in file.cl-ast your program should create file.cl-type and serialize the class map to it.

The class map is described in the Cool Reference Manual.

A .cl-type file consists of one section for this assignment:

  1. The class map.

We will now describe exactly what to output for the class map. The general idea and notation (one string per line, recursive descent) are the same as in the previous assignment.

The Class Map

Detailed .cl-type Example

Now that we've formally defined the output specification, we can present a worked example. Here's the example input we will consider:

class Main inherits IO {
  my_attribute : Int <- 5 ;
  main() : Object {
    out_string("Hello, world.\n")
  } ;
} ;

Resulting .cl-type class map output with comments:

class_map
6              -- number of classes
Bool           -- note: includes predefined base classes
0
IO
0
Int
0
Main
1              -- our Main has 1 attribute
initializer
my_attribute  -- named "my_attribute" Int with type Int
2              -- initializer expression line number
Int            -- initializer expression type (NOT PART OF THIS ASSIGNMENT)
integer        -- initializer expression kind
5              -- which integer constant is it?
Object
0
String
0

Commentary

This is a checkpoint (a partial implementation) of the complete Semantic Analysis assignment. The implementation of the checkpoint should do the following:

Thus you should build the class hierarchy and check everything related to that. For example:

You can do basic testing with something like the following:

linux> cool.exe --parse file.cl
linux> cool.exe --out reference --class-map file.cl
linux> my-checker file.cl-ast
linux> diff -b -B -E -w file.cl-type reference.cl-type

However, the reference implementation produces expressions with type annotations (an extra line of output per expression), so the diff command will report differences; you need to examine the differences. You can output the diff results side-by-side by passing the -y flag.

Getting the Assignment

The starter code for the assignment is on the Linux server at the path:

/export/home/public/schwesin/csc310/semantic-analyzer-checkpoint-handout

Turning in the Assignment

You must turn in a zip file containing these files:

  1. ast.ml
  2. serialize.ml
  3. deserialize.ml
  4. semantic_analysis.ml
  5. main.ml
  6. README

There is a makefile provided with this assignment. To submit the assignment, execute the command:

    make submit

from within the assignment directory.

Grading Criteria

Grading (out of 100 points):