All Packages Class Hierarchy This Package Previous Next Index
Class parser.Parser
java.lang.Object

+parser.Parser
 public class Parser
 extends Object
 implements Cloneable, Function
A formula parser. A Parser object is constructed from a string which represents a mathematical formula
in standard mathematical notation. The object can then be evaluated, either to a double, or
to a partiallyevaluated expression in which some of the variables have been replaced by doubles.
The methods of Parser can throw NumberFormatException and Arithmetic Exception. A NumberFormatException
is thown when the parser detects syntax errors in the formula, while bulding the tree structure. An ArithmeticException
is thrown when the parser encounters an obstacle to evaluating the tree.
However, the evaluation routines NEVER throw an exception for impossible function
evaluations (eg sqrt(1)), they just pass it on up as a string.
Public methods deal with construction of a Parser, and its subsequent evaluation. A number of package visibility
methods allow the user to see the structure of the tree which Parser generates. This is a tree
of Parser objects, each of which has an operand (private Parser.op) and two children (private Parser.right and private Parser.left).
These are accessed by Parser.right() and Parser.left().

MULTI_CHAR_PER_VAR


ONE_CHAR_PER_VAR
 Flag to allow or forbid multicharacter variable names.

Parser()


Parser(Parser, char, Parser)
 Create a parser by composing the functions in the given two Parsers using the arithmetic
operation represented by the character m (eg new Parser(p, '+', q) adds p and q).

Parser(String)
 Create a parser from the formula in the default mode, ONE_CHAR_PER_VAR mode

Parser(String, short)
 Create a parser object from the formula in the given String.

clone()
 Copy the parser.

equals(Parser)
 Tests whether the functions represented by this and p are equal.

equalsModC(Parser)
 Determines whether two functions differ by a constant.

eval(double)
 Evaluate the formula as a function of the vaiable x.

eval(String[], double[])

The basic evaluation routine.

getVariables()
 Returns a list of the variable names used in this parser.

hashCode()

Overrides Object.hashCode().

main(String[])
 Debugging method; ignore.

printTree(Parser)
 Debugging method; ignore.

toString()
 Presents the formula as a string.
ONE_CHAR_PER_VAR
public final static short ONE_CHAR_PER_VAR
 Flag to allow or forbid multicharacter variable names.
MULTI_CHAR_PER_VAR
public final static short MULTI_CHAR_PER_VAR
Parser
public Parser()
Parser
public Parser(String formula,
short mode)
 Create a parser object from the formula in the given String. The mode is one of ONE_CHAR_PER_VAR or
MULTI_CHAR_PER_VAR.
Parser
public Parser(String f)
 Create a parser from the formula in the default mode, ONE_CHAR_PER_VAR mode
Parser
public Parser(Parser p,
char m,
Parser q)
 Create a parser by composing the functions in the given two Parsers using the arithmetic
operation represented by the character m (eg new Parser(p, '+', q) adds p and q).
main
public static void main(String args[])
 Debugging method; ignore.
printTree
public static void printTree(Parser p)
 Debugging method; ignore.
eval
public final double eval(double x)
 Evaluate the formula as a function of the vaiable x.
 Returns:
 If the formula doesn't evaluates to a double, this returns Double.NaN
eval
public final Object eval(String variables[],
double values[])
 The basic evaluation routine. Since a completely evaluated tree yields a double, while
a partially evaluated one yields a string, this routine returns an Object, which may be cast to either
a Double or a String. Doing this prevents converting doubles to strings and back again.
toString
public String toString()
 Presents the formula as a string. Note that the string will NOT be the same as was originally used
to construct the Parser; in particular, a lot of extra parenthesis will be present.
 Overrides:
 toString in class Object
clone
public Object clone()
 Copy the parser.
 Overrides:
 clone in class Object
equals
public boolean equals(Parser p)
 Tests whether the functions represented by this and p are equal.
The test declares two functions to be indistinguishable if they are
within a small percentage of each other's values at a sample of random
testpoints. Thus, false positives are possible, but false negatives are
very unlikely.
equalsModC
public boolean equalsModC(Parser p)
 Determines whether two functions differ by a constant.
hashCode
public int hashCode()
 Overrides Object.hashCode().
Because we override Object.equals(), we need to provide a hashcode that gives the same code to two
objects which equals() will return as equal. This method completely evaluates the parser to a Double
(by setting all the variables to zero) and returns the hashcode of that Double.
 Overrides:
 hashCode in class Object
getVariables
public String[] getVariables()
 Returns a list of the variable names used in this parser.
All Packages Class Hierarchy This Package Previous Next Index