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 partially-evaluated 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().


Variable Index

 o MULTI_CHAR_PER_VAR
 o ONE_CHAR_PER_VAR
Flag to allow or forbid multi-character variable names.

Constructor Index

 o Parser()
 o 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).
 o Parser(String)
Create a parser from the formula in the default mode, ONE_CHAR_PER_VAR mode
 o Parser(String, short)
Create a parser object from the formula in the given String.

Method Index

 o clone()
Copy the parser.
 o equals(Parser)
Tests whether the functions represented by this and p are equal.
 o equalsModC(Parser)
Determines whether two functions differ by a constant.
 o eval(double)
Evaluate the formula as a function of the vaiable x.
 o eval(String[], double[])
The basic evaluation routine.
 o getVariables()
Returns a list of the variable names used in this parser.
 o hashCode()
Overrides Object.hashCode().
 o main(String[])
Debugging method; ignore.
 o printTree(Parser)
Debugging method; ignore.
 o toString()
Presents the formula as a string.

Variables

 o ONE_CHAR_PER_VAR
  public final static short ONE_CHAR_PER_VAR
Flag to allow or forbid multi-character variable names.

 o MULTI_CHAR_PER_VAR
  public final static short MULTI_CHAR_PER_VAR

Constructors

 o Parser
  public Parser()
 o 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.

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

 o 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).

Methods

 o main
  public static void main(String args[])
Debugging method; ignore.

 o printTree
  public static void printTree(Parser p)
Debugging method; ignore.

 o 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
 o 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.

 o 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
 o clone
  public Object clone()
Copy the parser.

Overrides:
clone in class Object
 o 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.

 o equalsModC
  public boolean equalsModC(Parser p)
Determines whether two functions differ by a constant.

 o 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
 o getVariables
  public String[] getVariables()
Returns a list of the variable names used in this parser.


All Packages  Class Hierarchy  This Package  Previous  Next  Index