Due Date: Feb 19, 2009
In this project you will build a very simple arithmetic expression evaluator without using any canned software from the net; that is, you may only use the basic Java IO library to read characters and the Integer.parseInt() method.
I am testing not only your basic ability to write software in Java, but your ability to precisely follow instructions--a crucial skill for a developer. Your code must exactly process input and generate output according to this specification.
Your program will accept input from "standard in" System.in and print to "standard out" System.out the correct value indicated by the expression on a line by itself. For example, for input 3+4, your program should print 7 on a line by itself and nothing else.
For empty input (an empty file or string), return 0 on a line by itself.
You must handle expressions of arbitrary length with multi-digit integers as basic expression atoms and with operators plus and multiply. You must handle nested parentheses as well. More formally, here is the grammar you must recognize:
expression : addExpression EOF ; addExpression : multExpression ( '+' multExpression )* ; multExpression : atom ( '*' atom )* ; atom: INTEGER | '(' addExpression ')' ;
Don't forget to handle the operator precedence properly; i.e., 3+4*5 is 23 not 35 due to the higher precedence of the multiply operator. Operations in parentheses must be done before the surrounding operations.
Your expressions must be terminated by EOF, which means that nothing can follow your expression. If you see 3 + 4 5, your program must emit an error that it expected EOF but found 5.
Your program will evaluate a single expression each run.
You must ignore whitespace characters: '\t', ' ', and '\n'. Note, however, that 3 4 is two numbers, not 34. Recall that PC's are insane and use "\r\n".
You may implement this project any way you want, but you must execute your program by invoking the main method in a class called Eval. You must read from stdin and write to stdout.
Do not put your code in a Java package.
You may not use the StringTokenizer Java built-in class.
For parsing errors, emit errors of the form:
Eval error: any error message you want on one line
Since we will test your program via main(), we can't really throw exceptions.
Other than the answer and errors of this form, your program must not emit any other output.
You must place expr.jar into expr/trunk/lib in svn.
The expr.jar jar must contain both Java and .class files for your project.
Do not use a package for your code. The main class must be Eval.
Please bring a printout of your project to class at 3:30.
I will grade your projects automatically using a unix box via a series of tests like this:
java -cp expr.jar Eval < expression-input.txt
Your grade is a floating point number from 0..10.