Infix Expression Evaluation

Beta 4.5, May 23, 2008

The InfixEvalDemo demonstrates the buildFromInfix and evaluate methods in ExprEvaluator is intended to be an easy use library type class like the classes in the JDK that can be used in other programs.If "Stacks" or "Debug" is selected, extra output is sent to System.out. If you are using a browser, you will need to open the Java console in order to see the output. (Select "Tools" | "Java Console" in Mozilla Firefox, "Tools" | "Sun Java Console" in Internet Explorer). The "Stacks" output may be useful to see how stacks are used to process the expression. "Debug" is only useful for debugging the code.

The ExprEvaluator class is intended to be easy to use class. InfixEvalDemo just demonstrates some of its capabilities. It could be used in other Java programs whenever it is desirable to allow the user to input a prefix, infix or postfix expression that will be evaluated by the program.

Three modes of operation are demonstrated this demo. When "Use variables t, x, y without '='" is selected, the first three text boxes are labeled "t", "x", and "y" and the expressions in those boxes are automatically assigned to those variables. Actually the "=" operator can be used but it provides no benefits. Four distinct ExprEvaluator objects are used in this mode so assigning a value to a variable in one text box does not effect the calculation in another. The order of evaluation is t, x, y, and finally Infix.

When "Use variables a, b, ..., z with '='" is selected, the text boxes are not assigned to any particular variable, Hence you typically will want to use the "=" operator to assign the value of an expression to a variable. In this mode, only one ExprEvaluator object is used so assigning a value to a variable in one box effects its value in the following boxes. The order of evaluation is 1st, 2nd, 3rd, and finally Infix.

When "Create a table" is selected, you can supply up expressions for up to 4 functions of x and a table will be created showing the values of the functions for x - 0, 1, 2, 3, and 4. In this case, only 1 ExprEvaluator object is created but a separate ExptTree is created for each of the expressions.

The demo requires a Java 1.5 or higher add-on to be installed in your browser. If needed, you can down load a free copy from

Examples using the above InfixEvalDemo

In the "Use variables t, x, y without '='" mode, the demo uses 4 instances of ExprEvaluator - one or each of t, x, y, and infix. This allows several interesting uses. Here are some examples:

   sqrt(3^2 + 4^2) Evaluates the hypotenuse of a right triangle with sides 3 and 4.
 34sqrt(x^2 + y^2) Evaluates the hypotenuse of a right triangle with sides x and y.
 3(4/3)*xsqrt(x^2 + y^2) Evaluates the hypotenuse of a right triangle with sides x and (4/3)x.
 0 (x-4)/(x-5) Evaluates (x-4)/(x-5) when x = 0.
 x+.1 (x-4)/(x-5) Evaluates (x-4)/(x-5) with x increasing by .1 each time the expressions are evaluated.
30cos(toRadians(t))sin(toRadians(t))  Calculates the x and y coordinates of the point on the unit circle at the angle of 30 degrees.
t + 10cos(toRadians(t))sin(toRadians(t))sin(2*toRadians(t))^2 Calculates the x, y, and z coordinates of the point above the unit circle with angle t increasing by 10 degrees each time the expressions are evaluated. The z value is [sin(2t)]2

In the "Use variables a, b, ... z with '='" mode, the demo uses only 1 instances of ExprEvaluator . Here are a few examples:

 a = 3b = 4sqrt(a^2 + b^2)Evaluates the hypotenuse of a right triangle with sides a and b.
a = 3b = 4c = 2  Evaluate a polynomial with 3 constant parameters. This is a two part example. First assign values to a, b, and c. Then follow up with assigning values to x and y. Can be repeated with new values of x and y.
 x = 3y = 4a*x^2 + b*x*y + c*y^2

Comments about the ExprEvaluator class

In the case of illegal calculations such as 1/0, log(-1), or sqrt(-1), the result will typically be infinity, -infinity, or NaN (Not a Number).

ExprEvaluator originally was developed for a Data Structures class that demonstrated stacks and binary trees. It only operated with numbers and the binary operators +, -, *, /. It could build binary expression trees from both infix and postfix expressions. This version expands it bty allowing the ^ operator, functions, unary operators + and -, variables a, b, c, ..., z, as well as constants PI and E.

ExprEvaluator can be used in nearly any program where it is desirable to allow the user to input an prefix, infix or postfix expression that needs to be evaluated. The class was designed to be very easy to use as illustrated by Suppose that the infix expression is contained in the String expr and the values of t, x, and y are stored in tVal, xVal, and yVal. All that is needed to evaluate the expression into a variable exprVal are these lines of code:

   ExprEvaluator evaluator = new ExprEvaluator();
   try {
      exprVal = evaluator.evaluate(tVal, xVal, yVal);
   }catch (Exception e) {
      System.out.println ("Error:  namely: " + e);

When it is desired to evaluate several different functions the following code style could be used:

   ExprEvaluator evaluator = new ExprEvaluator();
   ExprTree [ ] tree = new ExprTree[3];
   String [ ] expr = new String[3];
   double x;
   double [ ] val = new double[3];
    expr[0] = ...
    expr[1] = ...
    expr[2] = ...
    try {
        for (int i = 0; i < 3; i++)
            tree[i] = evaluator.buildFromInfix(expr[i]);
        for (x = ...; x < ...; ...)
           for (i = 0; i < 3; i++)
              val[i] = evaluator.evaluate(tree[i]);
   }catch (Exception e) {
      System.out.println ("Error:  namely: " + e);

If the expression is prefix or postfix instead of infix, the only change would be to be to replace "buildFromInfix" by "buildFromPrefix" or "buildFromPostfix".

For additional information, please look at the Javadoc files for ExprEvaluator.

Downloading the files

Down load the beta copy as a .zip file. Included files:

The class requires Java 1.5 or higher

Comments and questions

Please send any comments, corrections, questions, or suggestions to James Brink

Copyright James Brink, 2008. Permission to use the package for non-commercial use is hereby granted.

Revised 6/21/2008