Harvey Mudd College
Computer Science 60
Assignment 7, Due Monday, November 5, 11:59pm



Unicalc and Unilang

Here is the starter code for hw7: hw7.zip

This homework asks you to use Java to implement the Unicalc application that you completed in Scheme in weeks 2 and 3. You will use the OpenList data structure as the fundamental building block - OpenList, after all, is designed to implement Racket-like list structures. Because Java is organized around creating and combining data structures, we will Java-ize Unicalc in four parts:

Why so much code provided? I'd prefer to write the application myself... .

Please do! With the support code we are balancing two valid concerns: on one hand, there is a deep understanding that only a from-scratch implementation can provide. On the other hand, we want to provide enough examples and framework to build familiarity with what are certainly intricate - and important! - computational ideas. There is also an example languages: the MiniMath language that we extended in class.

Here is a link to that MiniMath example as extended in class, in a zip file...

Feel free to use as much - or as little - of the support code as you wish. One important thing we do ask if you implement from scratch is to use (or replicate) our toString conventions, so that we can check your results efficiently!  Also, please adhere to the interfaces we provide (names of functions, parameters, return types, etc).

The goal of this assignment is that you have hands-on experience with (and, we hope, insights into) the processing that is part of implementing all computer languages. Using the starter code or building from scratch, we believe, can both provide that foundation.

Submitting your code

Please submit the following files zipped in an archive named hw7.zip:

Submit your zip archive in the usual way, under homework #7.

Because the .java files you write this week will be interdependent, keep them all in a single folder/directory named hw7 That way, java will be able to find classes as it needs to. Also, it will be easy to zip up that directory to submit.

Part 1: Unicalc in Java via Quantity

30 points

This is the implementation - really a port - of Unicalc's functionality into a Quantity class in Java.

Since this problem was also the extra-credit last week, its details and tests are explained on this page.

There are both tests in the provided main and in the separate QuantityTester.java file in order to check if your implementation of quantity lists is correct.


Part 2: Tokenizer

10 points

A Tokenizer.java file provided for you in the code at the top-level assignment page. You will want to improve it so that the Unicalc language's tokens are found correctly whether or not spaces are inserted around the operators. Other things are possible here, too, if you decide to add your own features to the language, but for the required part of the language, this file will require the fewest changes.

Notes on the tokenizer:


Part 2: Parsing the Unicalc language via Parser

30 points

This problem asks you to implement a Parser for the Unicalc language, whose grammar and parse trees are are summaried in this picture.

Unicalc grammar 

More detail on some fo these are below... .

Notes on this grammar

Symbols listed in red are terminal symbols. Similarly, doubles, ints and strings are also terminals.

The rule for Q uses the asterisk, not as "times," but meaning "zero or more of the preceding symbol." It also uses the plus sign, not as addition, but meaning "one or more of the preceding symbol." This Q rule is already implemented in the provided Parser.java file. D, I, and V are also already implemented. You will only need to implement rules U and above.

Parser.java implements a recursive-descent parser for part of the above grammar; your task is to extend it to the rest of the grammar.

Reminders about parsing:



Part 4: Evaluating the Unicalc language via Evaluator

30 points

This part completes the unicalc language. Here, you should build/extend the Evaluator class that will convert parse trees into objects of type Quantity.

What does everything evaluate to?    Well, every valid statement in the Unicalc language evaluates to an object of type Quantity, that is, a quantity list. For example, the parse tree which is simply a list of a Quantity naturally evaluates to that Quantity object.

Notes on the Evaluator:



Testing your code

When the graders run your file, they will make sure to enable the printing of all of the tokens, parse trees, evaluations, and the environment in order to get a full picture of the behavior of your language.

Here are a pretty extensive list of examples, including the printout of the input line, the tokens, parse tree, value, and - when of interest - the environment association list in this.env each time. They're the same as the parse trees above, but now with the evaluation results.

Assignment 7's tests including evaluation and (when applicable) the this.env environment.

These are the tests we will use in grading the code, so if it passes all of these - perfect!



Optional extra credit: personalization or functionalization!

Personalizing For up to +5 (or more) points, improve the unicalc-language with whatever feature you'd like... . Here are the rules, however:

Functions! For up to an additional +20 points, provide an implementation of functions in the Parser and Evaluator of the unicalc language. How to design and implement this is 100% up to you.

Warning: If you try this, be sure not to edit the only copy of your unicalc language files! -- implementing functions is tricky, and it's happened that people have broken their language in the attempt to do so!

Be sure to work on a copy of your files -- and then, if you submit this, please place those new files into a folder named extra and zip that folder. There is a spot in the submission site to upload extra.zip for this extra-credit challenge.

Be sure to include a comment at the top of your new Evaluator.java on how we could best test your code - give the graders a couple of examples to try!



Here is a (very small) image of one possible grammar for introducing functions into the language. It's not the only approach, to be sure. Click on the image for a larger version.