Second Summer 2002


Homework Assignment Six: A Very Basic Interpreter (Evaluator)

For this assignment you are to write an arithmetic expression evaluator that is able to perform

is able to as illustrated in the sample session below. The evaluator should also handle parentheses properly.

Here's a sample session with the program:

frilled.cs.indiana.edu%java Eval
Eval> 1 + 1
1 + 1 
2 
Result is: 2 
Eval> ( 1 + 2 ) * 3
( 1 + 2 ) * 3 
( 3 ) * 3 
3 * 3 
9 
Result is: 9 
Eval> a = 1 + 1
1 + 1 
2 
Stored 2 in a
Eval> a 
2 
Result is: 2 
Eval> a = a + 1
2 + 1 
3 
Stored 3 in a
Eval> a
3 
Result is: 3 
Eval> b = a - 4
3 - 4 
-1 
Stored -1 in b
Eval> a
3 
Result is: 3 
Eval> b
-1 
Result is: -1 
Eval> a = ( a - b ) * ( a + b )
( 3 - -1 ) * ( 3 + -1 ) 
( 4 ) * ( 3 + -1 ) 
( 4 ) * ( 2 ) 
4 * ( 2 ) 
4 * 2 
8 
Stored 8 in a
Eval> a
8 
Result is: 8 
Eval> b
-1 
Result is: -1 
Eval> b = a + b * 4
8 + -1 * 4 
8 + -4 
4 
Stored 4 in b
Eval> a / b
8 / 4 
2 
Result is: 2 
Eval> a = a / b
8 / 4 
2 
Stored 2 in a
Eval> a
2 
Result is: 2 
Eval> b
4 
Result is: 4 
Eval> exit
frilled.cs.indiana.edu%
You may safely assume that Note that in my evaluator the tokens (numbers, parens) are separated by spaces.

Here's a grading scale for this assignment:

Task Points
Reading the data line by line and storing it properly 15
Being able to support + and - 20
Supporting * and / 20
Allowing named variables to store values 15
Allowing named variables in expressions15
Properly calculating (1 + 1) * 2 + 2 * ( 1 + 1 ) 10
Providing error-checking through try-catch blocks 5
Total 100

Please let us know if you have any questions.


Last updated: Jul 19, 2002 by Adrian German for A201