Harvey Mudd College
Computer Science 60
Extra-credit Assignment 12, Due Friday, May 9th at 11:59pm



Unicalc and Unilang

Here is the starter code for hw12: hw12.zip

Grading notes:


The files in question are 
submissions@knuth:~/submissionsFall13Files/handbuiltScripts/cs60_wk12_files/xxin/hw12/src/junit-4.10.jar and
  .../hamcrest-core-1.3.jar

Then, 
cp ~/*.jar .
javac -cp .:./junit-4.10.jar *.java

javac -cp .:./junit-4.10.jar *.java

javac -cp .:./junit-4.10.jar QuantityTest.java
javac -cp .:./junit-4.10.jar TokenizerTest.java
javac -cp .:./junit-4.10.jar ParserTest.java
javac -cp .:./junit-4.10.jar EvaluatorTest.java


java -cp .:./junit-4.10.jar org.junit.runner.JUnitCore QuantityTest
java -cp .:./junit-4.10.jar org.junit.runner.JUnitCore TokenizerTest
java -cp .:./junit-4.10.jar org.junit.runner.JUnitCore ParserTest
java -cp .:./junit-4.10.jar org.junit.runner.JUnitCore EvaluatorTest

if in the src package, go up one directory and run

java -cp .:./src/junit-4.10.jar org.junit.runner.JUnitCore src.TokenizerTest
and so on...

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 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 showed briefly in class (and is not fully working, though it does give an example of all of the important ideas!)

Here is a link to that MiniMath example code, 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 hw12.zip:

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

Because the .java files you write this week will be interdependent, keep them all in a single folder/directory named hw12 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

+10 points

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

The implementation will be in the Quantity class.
In order to ensure that toString and other important methods are uniform, there is a starter file for Quantity.java

Notes on Quantity    


Additional Notes

Presuming the numerators and denominators are sorted    The constructors of Quantity should make sure that the numerator and denominator units are in alphabetical (sorted) order. Our starter file provides this functionality.

In addition, the file QuantityTest.java has a set of tests that you'll be able to compile and run once Quantity's methods are written.


Part 2: Tokenizer

+5 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 3: Parsing the Unicalc language via Parser

+15 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

+12 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

We have provided JUnit test cases that should enable you to test your methods.

The following examples are identical to those tested in the JUnit test cases, but you can run them interactively by running the method main in Evaluator.java 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 10'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 +3 (or more) points, improve the unicalc-language with whatever feature you'd like... . Here are the rules, however:

Functions! For up to an additional +8 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.