CS 200 P3: Parsing infix double expressions, building and evaluating expression trees

Overview

The object of this assignment is to parse infix double expressions, convert them to expression trees, and evaluate these expression trees. Modify (and perhaps first finish) your TokenIter from P2. Debug mode will no longer be enforced, or tested.

The syntax for infix expressions is:

  expr = term ( ("+" | "-") term )*

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

  factor = "abs" factor | "(" expr ")" | flopon

  flopon = digits ("." digits)? ("e" ("+"|"-") digits)?

  digits = [0-9]+

As discussed in class, this iterative (not left recursive) grammar can be transformed directly into a predictive parser, where every non-terminal becomes a method parsing that non-terminal, and produces the TreeNode associated with the expression tree.

You need to work on the following code:

Tree

Your Tree code needs a proper implementation of the postorderEval(TreeNode node) method , that is called by:
    // if tree empty return null
    // else evaluate the tree by postorder traversal 
    // and return its value
    public Double postorderEval(){
    	Double res = null;
    	if (!isEmpty())
	    res = postorderEval(root);
    	return res;
    }
which evaluates the (double expression) Tree. It evaluates the operands, and then evaluates the operator. All nodes return a Double value. (Yes, “Double”, as opposed to “double”.)

Testing and submitting your code