jwo.utils.expression
Class MathParser

java.lang.Object
  extended by jwo.utils.expression.MathParser

public final class MathParser
extends Object

Converts a mathematical expression (using 'normal' infix ordering) into a postfixed stack. Used for evaluation of mathematical expressions. Modified from original code described at www.devx.com/premier/mgznarch/javapro/2001/05may01/ps0105/ps0105.asp. Modifications allow unary operators (e.g. trig functions) to be parsed and arbitrary variable substitutions to be added.

Version:
2.3, 16th February, 2001, modified 4th September, 2008.
Author:
Original code Daniel Savarese, extensively modified by Jo Wood.

Constructor Summary
MathParser()
          Creates a new math parser that can process a mathematical expression.
MathParser(boolean useVars)
          Creates a new math parser that can process a mathematical expression.
 
Method Summary
 void addCommand(String cmdName)
          Adds a permitted command/function name to those looked for when parsing expression.
 void addVariable(String varName)
          Adds a permitted variable name to those looked for when parsing expression.
 Stack parse(String input)
          Parses an mathematical infix expression storing the operators and operands on a postfixed processing stack.
 boolean usesZ1()
          Reports whether a 'z1' substitution has been made.
 boolean usesZ2()
          Reports whether a 'z2' substitution has been made.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MathParser

public MathParser()
Creates a new math parser that can process a mathematical expression. This version does not use the shortcut variables x,y,z1 and z2. For variable substitution use addVariable() and implement a Substitutable interface. Variable substitution should use one or other of these approaches and not both.


MathParser

public MathParser(boolean useVars)
Creates a new math parser that can process a mathematical expression. There are two ways of providing evaluation-time variable substitution. The shortcut variables x,y,z1 and z2 can be specified as a constructor to the evaluator if this parser sets useVars to true. The preferred and more general solution is to use addVariable() and implement a Substitutable interface. Variable substitution should use one or other of these approaches and not both.

Parameters:
useVars - If true, the shortcut variables 'x', 'y', 'z1' and 'z2' can be substituted at evaluation time.
Method Detail

addVariable

public void addVariable(String varName)
Adds a permitted variable name to those looked for when parsing expression.

Parameters:
varName - Name of permitted variable.

addCommand

public void addCommand(String cmdName)
Adds a permitted command/function name to those looked for when parsing expression.

Parameters:
cmdName - Name of permitted command or function.

parse

public Stack parse(String input)
            throws ParseException
Parses an mathematical infix expression storing the operators and operands on a postfixed processing stack. expr -> term moreterms | term
moreterms -> + term | - term
term -> exponent moreexponents | exponent
moreexponents -> * exponent | / exponent
exponent -> factor morefactors | factor
morefactors -> ** factor
factor -> number | ( expr ) | unary factor

Parameters:
input - Mathematical expression to store.
Returns:
stack containing postfixed representation of the expression.
Throws:
ParseException - If problem parsing text.

usesZ1

public boolean usesZ1()
Reports whether a 'z1' substitution has been made. This is useful if we need to know whether the contents of z1 needs to be explained or error checked.

Returns:
true if a 'z1' substitution has been made.

usesZ2

public boolean usesZ2()
Reports whether a 'z2' substitution has been made. This is useful if we need to know whether the contents of z2 needs to be explained or error checked.

Returns:
true if a 'z2' substitution has been made.


Copyright Jo Wood, 1996-2009, last modified, 17th April, 2009