CS 200, Fall 2016
P4: Parsing and evaluating equations

Due and late dates and times

Overview

The object of this assignment is to parse and evaluate equations. Equations are of the form:
  equation ::=   identifier "=" expr
 
  expr     ::=   term ( ("+" | "-") term )*

  term     ::=   factor ( ("*" | "/") factor )*

  factor   ::=   "-" factor | identifier | number | "(" expr ")"

Notice that meta symbol "::=" is used in the above grammar rules to disambiguate it from symbol "=" in the equations.

An identifier is a letter followed by zero or more letters or digits:

 identifier = [A-Za-z]([A-Za-z0-9])*

Here is an example of 3 equations, each one on a line:

a = 3-4 - -5
b = a*7 + 11
c = a+-b

The goal of this project is to read lines, that are either empty or contain one equation, evaluate the expression on the right hand side, store the key and value in a Symbol, where the key is the identifier on the left hand side, and the value is the result of evaluating the right hand side, and store the symbol in a symbol table. The symbol table is implemented using a Binary Search Tree.

Notice that we now have two very different trees: an expression tree to represent and evaluate expressions, and a Binary Search Tree to implement a symbol table that stores (String identifier, Integer value) pairs.

The identifiers in an expression are defined in previous equations. Identifiers are not redefined, i.e. there is only one definition of each identifier. When during the postorder evaluation of an expression tree an identifier is encountered, its value must be looked up in the symbol table, and used in the evaluation of the expression.

For example, when parsing and evaluating a file with the three equations above, the following output is generated by EquationDriver (see below):

next line: a = 3-4 - -5
result: [a: 4]
next line: b=a*7+11
result: [b: 39]
next line: c = a+-b 
result: [c: -35]

Work with the following codes:

Testing and submitting your code

The P4 prelimanary tester and the grader will not test your code in debug mode.

Use the Checkin webserver to exercise and submit your code. Submit a P4.jar file containing TokenIter.java, BST.java, Tree.java , Equation.java **ONLY**.

Make sure you put ***java** files in your jar (not class files).