|
Assignment 5
|
|
|
Evaluator - Part 2: Symbol Tables
|
Due: December 14
|
Objectives
In this assignment you will add identifiers to HL, as well as all remaining
functionality that depends on them: assignments, function calls and returns, loops, set formers,
and, obviously, all declarations.
However, you still do not need to handle errors in programs.
This functionality will be addressed in the final assignment.
With this assignment you should become much more familiar with symbol tables and scoping schemes.
The learning objectives covered in this assignment are:
- 7Cb (which automatically includes 7Da and 7Ca)
- 7Bb (which automatically includes 7Db and 7Ba)
- 7Ab (which replaces a component of 7Bb and includes it)
Some of these learning objectives will be identified in the description of this assignment,
and some in the tests.
Solutions to the previous assignment, assignment 4, will not be provided.
You will therefore need to complete the level objectives of that assignment
at the same level as the ones on this assignment as a base for this one and for the one that follows.
(i.e. you need the A4 C-level objectives for the A5 C-level objectives, and so on)
Preparation
This assignment is mostly a continuation of the last one.
Your working directory should have the same structure and content as the
fourth assignment.
A few extra files specific to this assignment are provided in the
Handouts directory:
- Revised IdBoolToken.java, IdNumToken.java, and IdSetToken.java
and a new superclass for them called IdentifierToken.java.
This is to support the changes that you need to make to the scanner
starting at level C.
Take a look at the three files for the specific tokens.
As you can see, the only work now done by these three classes is to record the
type of token that is being scanned. Everything else has been moved to their
superclass IdentifierToken.java.
This reorganization does not affect the operation of the scanner.
- A new java file: EvaluationException.java.
This is a subclass of Exception that is specific to evaluation exceptions.
This class will be mostly used in the next assignment to handle evaluation errors,
but it does have one use for this assignment:
you should define a ReturnException subclass of EvaluationException
to be used in the evaluation of return statements to end the execution of the
function's body and return the return value to the evaluator of the function call.
- The two test scripts for this assignment: runtests, and the unit test t.
Assignment
In this assignment you will be adding functionality to your interpreter
to support the use of variables and functions in the language HL.
In addition to the semantic of HL described in
HL semantics - Part 1
handed out in the fourth assignment,
the remaining semantics of HL can be found in
HL semantics - Part 2.
Below are the components that you will need to implement in this assignment.
As you do so, remember that, as with previous assignments,
you can continue to assume that the HL programs are error free. This means that
your program does not need to handle invalid uses of variables or function calls or returns yet.
7Cb: Global variables and while statement
For this learning objective, you will need to implement the simplest possible configuration
of variables in a language, which is a monolithic block structure
where the language has no functions and all variables are global.
Here is what you need to accomplish to meet this learning objective:
- Implement a global identifier/name table, and modify your identifier tokens and ASTs so
that their value is the identifier's key into that table instead of the name of the identifier.
- Implement a minimal (i.e. global) symbol table for your identifiers
where you will keep the value of each identifier in addition to its information.
- Write eval visitors for declarations, assignments, and identifiers used in expressions and conditions.
- Write the eval visitor for while statements.
Implementation Suggestions:
- Because the name table defines a relationship between the lexeme of an identifier
and a key representing that lexeme,
and because the interaction between the interpreter and the name table primarily takes place
during scanning,
the natural place to locate the name table functionality is with the scanner,
particularly in the class IdentifierToken, where the name table can be a static table in that class.
In the Javadoc comments of the
IdentifierToken.java handout
there is a proposed API that can support all the requirements of this assignment at all levels.
-
This proposed API assumes that a function call HLSymbTab.newIdName(key) will be triggered
whenever a new name is encountered.
newIdName would be a static method in a class HLSymbTab where the symbol table is implemented.
This function call will be needed for learning objectives C and B,
and can possibly be moved elsewhere when implementing static scoping for the A learning objective.
No matter how far you go in implementing the symbol table mechanisms in this assignment,
you will need Java classes to support this functionality.
In all implementations you will need a class for the symbol table entries.
I have called this class HLSymbTab (but no handout is provided for it).
An instance of this class keeps information about a single variable.
The functionality of this class is very minimal at the C level when all variables are global.
It will increase in complexity with the implementation of the other scoping mechanisms.
If an instance of this class is a symbol table entry,
a symbol table is simply an table of these entries, i.e. an ArrayList of these entries.
At the C level all that is needed is a single symbol table for the entire program.
This ArrayList can simply just be a static variable in this class.
7Bb: Dynamic Scoping and for loop
Here is what you need to accomplish to meet the 7Bb learning objective:
- Implement the evaluation of for statements.
- Implement the evaluation of function calls (including Boolean function calls which are parsed separately),
return statements, and variable declarations,
assuming that the HL language is dynamically scoped.
If you are feeling very confident, you can skip this particular component
of the 7Bb learning objective, and implement 7Ab, which is static scoping, instead.
However, many of the comments below also apply to 7Ab.
Implementation Suggestions:
- You will need a few additional classes to support the symbol table functionality
described in the lectures. Here are the ones I implemented:
- As described in the lectures, return statements are implemented by throwing exceptions.
For this purpose a generic EvaluationException class is provided in handouts to be used
for all exceptions thrown during evaluation.
This class will be used for evaluation errors in the next assignment,
but you should also define a subclass for return exceptions that is more focused
on supporting the evaluation of return statements including passing the return value
to the evaluator of the function call.
7Ab: Static scoping
To meet this learning objective:
Implementation Suggestions:
- At this level the parser will be involved in implementing some of the scoping functionality.
You will therefore need to modify HL.jjt to interact with the symbol table.
I have also found it useful to modify the main program in TestHL.java to initialize the global scope.
You can see two lines I have added before the parse-eval loop to do this
in the TestHL.java provided in the
Aonly Handouts directory:
- For static scoping it is useful to think of the two types of occurrences of identifiers
in the HL.jjt parser as being parsed differently.
- Declaring an identifier creates a new entry for that identifier
in the symbol table for the currently active scope.
- When an identifier is being used, then the ASTidentifier needs a reference to the scope of
that identifier in addition to its name key
- A word of warning when implementing the evaluation of function calls:
make sure that you evaluate all the parameters being passed to the function before
pushing a new scope onto the activation stack. This is particularly important
with recursive function calls because parameters may contain expressions that depend on
local variables and parameters in the calling scope.
7A+b: Set formers
Implementing this requirement in addition to static scoping will count as an additional
blue learning objective at the A level.
To meet this learning objective:
- Implement the evaluation of set formers.
Implementation Suggestions:
- Please read carefully the semantics of set formers in the
HL semantics - Part 2 document.
- You will be implementing block scopes for set formers. You can reuse a lot of the functionality
you developed for function scopes. However, these scopes are much simpler: they only have a single entry
for the set former variable, and they are also unnamed which means that no entry in the symbol table
will point to these scopes (unlike function scopes where the symbol table entry defining the function
points to a function's scope).
References
I have found the following Java classes useful:
Testing your assignment
The testing process for this assignment is similar to the previous assignment:
There is a single test script which simply tests all the learning objectives in order.
You can find this test script runtests, and the unit test t in the
Handouts directory.
The tests and results are in the
Tests directory.
Submitting the assignment
To submit this assignment:
- Zip up together HL.jjt, IdentifierToken.java and all the other java files that you have modified into A5C.zip,
A5B.zip, or A5A.zip to indicate which learning objectives should be assessed.
There is no need to include EvaluationException.java, SimpleNode.java, TestHL.java, and the java token file other
than IdentifierToken.java as we will provide these,
or the AST files because they will be recreated automatically when we compile your submission.
However, if you do change one of these files, you should include them in your zip file.
- Submit electronically your A5 zip file.
If you had not previously submitted assignment 4, or if you want to resubmit it because you ended up
doing more work on the assignment 4 learning objectives at the same time as this one,
then you can also submit this assignment as an assignment 4 submission.
To do this:
- Make a copy of your A5 zip file and call it A4A.zip, A4B.zip, or A4C.zip,
depending on how far you got in the learning objectives for assignment 4 and submit that A4 zip file.
- Send an email to cps710@cs.torontomu.ca telling us that you have resubmitted assignment 4
because we won't know otherwise.
Be sure to include your moons userid in the email, so we can find your assignment.
This page is maintained by
Sophie Quigley
(cps710@cs.torontomu.ca)
Last modified
Thursday, 28-Nov-2024 12:36:57 EST