# P6: HP Calculator Interpreter

### Due Monday, October 28th, 12noon

In this assignment, you'll modify code for an HP-calculator emulator.  Any questions about the semantics of a calculation (e.g., what order do you place values on a stack) will be resolved by using the HP-35 calculator emulator below (just click on the image to pull up the calculator itself):

The goals of this assignment are for you to learn:

• the interpreter design pattern (by building part of an interpreter)
• how to add methods to a class hierarchy to process, recursively, operations on hierarchy instances

In class, I sketched how an interpreter could be built to emulate an HP Calculator.  In this assignment, you will:

• install its codebase in a NetBeans or Eclipse Project

• you will need a jar file jakarta.jar

• I generalized the grammar of the Calculator: it supports the operations { +, -, *, / } as before, but now it supports the additional operations  % (modulo), ^ (power), ++ (increment), and -- (decrement).  These changes are indicated below

`/ non-negative integer postfix calculator// grammar.b : simple calculator// Simple tokens specified in simple format"+" ADD"-" SUB"*" MUL"/" DIV"%" MOD"^" POW"++" INC"--" DEC// Regular expression tokens specified in JavaCC formatTOKEN :{<INTEGER: ["0"-"9"] (["0"-"9"])* >}Exp : Action [Exp] :: Expr;Action : INTEGER :: Push| ADD :: Add| SUB :: Sub| DIV :: Div| MUL :: Mul| MOD :: Mod // computes x%y (where x is on top of stack and y is immediately beneath top)| POW :: Pow // computes x^y (raises x to the power y)| INC :: Inc // computes x++ (increments value on the top of the stack)| DEC :: Dec // computes x (decrements the value on the top of the stack);`

I have created the parser from this grammar and have generated all the classes that you'll need.  What is missing, and your task, is to fill in some methods.

Please note in the above grammar that the name of the class that is instantiated for a production is after the double colon (::).  So if there is a divide operation in an expression that you enter, you will see an instance of the Div class in a parse tree.  It is this naming convention that you should keep in mind as you proceed.

Note: if you have any questions on the order in which arguments are to be entered, consult the HP-35 simulator at the top of this web pagre.  Any discrepancies between what I have written above and the HP simulator, go with the simulator.

### Part 1: Supply the execute() methods for the Mod, Pow, Inc, and Dec Classes

These new classes were generated because of the extensions I made to the right-hand-side of the Action production above.  Right now, there are no useful execute() methods in these classes (although a default execute() is inherited -- but it is useless).  Your task is to fix these classes by supplying a correct execute() method.  Hint: Look at how existing execute methods are defined and you should have no problem. When you get this part to work, you'll be able to type in an expression and get its output, such as:

`kalc> 3 4 5 + * > .`
`3 4 5 + * = 27kalc> 3 4 ^ -->.3 4 ^ -- = 80`

And be able to type in such expressions for all operations.  Remember to terminate a line (expression) with dot ("."), otherwise kalc will keep asking for more input.

### Part 2: Supply a prettyPrint() method for all classes in the Action and Exp Hierarchies

Your task now is to add a void prettyPrint(Stack S) method to all classes in the Action and Exp hierarchies.  The goal of this method is to pretty-print the expression so that it is not post-fix.  I've already added some prettyPrint() methods for you -- but the most interesting code you must to provide.  Right now, all classes in the Action and Exp hierarchies have a prettyPrint() method defined, or a default is inherited.  The default is useless, and these are the methods that you need to replace.  Hint: look at how some prettyPrint() methods are defined, compare that to their execute() counterparts.  You'll see that their structure is quite similar.

When you get this part to work, you'll be able to type in an expression, get its output, and a normal in-fix representation, such as the text that is highlighted below:

`kalc> 3 4 5 + *> .3 4 5 + * = ( ( 5 + 4 )*3 ) = 27kalc> 5 6 7 * -> .5 6 7 * - = 5-7*6 = -37`

By writing your prettyPrint() methods differently, you'll get different but equivalent outputs, like: ( ( 5 + 4 )*3 ) or (3*(4 +5)) and so on.  Use an order that reflects the order of computation.

You will need to add PrettyPrint methods to the Action and Exp hierarchies, and modify the Main class to invoke the prettyPrint() method on the root of the parse tree.  Note also, you'll need to create a stack of strings.  I've created that for you in MyStringStack.   To see what your code should output, check the video I presented in class -- it has this pretty printing capability.

### What You Are To Submit

You are to submit a working Calculator that parses and correctly evaluates the following script.  You can invoke this script in Kalc by typing:

`> java Kalc.Main -f testscript.txt`

Your program may be subjected to other scripts when being graded.  Specifically:

• A zip file with all source code.  The zip file must unzip into <yourName>/<YourFilesAndDirectories>.

• A bash script to invoke your refactored program

• A README file that explains
• what you have done (beyond what was asked of you).
• how to invoke your bash script
• anything else I (the grader) needs to know.

A critical part of any design is clarity and understandability.   Hence, you will be graded on the clarity of your project and its ability to work correctly.  Sloppy code, documentation, or anything that makes grading or understanding your program difficult will cost you points!  Beware, some of these "beauty" points are subjective.

No late assignments/submissions will be accepted.