Class Parser

java.lang.Object
math.parser.Parser

public class Parser extends Object

Name: Parser

Description: Simple Java arithmetic expression parser and ELL(1) grammar syntactical analyzer and evaluator. Parser supports detection of implicit multiplication when a constant is followed by a variable or function.

Grammar:
S -> E (('+' | '-') E)*
E -> G ('*' G)*
G -> H ('/' H)*
H -> T ('%' T)*
T -> F | '-' F
F -> U ('^' U)*
U -> function '(' S ')' | number | '(' S ')' | variable

Date: 08/dic/06 Time: 13:58:28

Version:
1.0
Author:
Bertoli Marco
  • Constructor Details

    • Parser

      public Parser(String str)
      Class constructor
      Parameters:
      str - Input string to be analyzed
    • Parser

      public Parser(String str, boolean error)
      Class constructor
      Parameters:
      str - Input string to be analyzed
      error - if true, an exception is raised when value is get from a variable not initialized. If false, that variable is considered as zero.
  • Method Details

    • convertInput

      protected char[] convertInput(String str)
      Converts input string in a character array and removes unsupported characters
      Parameters:
      str - input string
      Returns:
      converted string, terminated by TERM character
    • getTree

      public ExpressionNode getTree() throws ParseError
      Returns the tree of the function generated by the parser
      Returns:
      Node root node of parsed tree
      Throws:
      ParseError - if parsed input string was malformed
    • setVariable

      public void setVariable(String name, double value) throws ParseError
      Sets the value for a given variable
      Parameters:
      name - name of the variable to be set (case sensitive)
      value - value for the variable
      Throws:
      ParseError - if parsed input string was malformed
    • getValue

      public double getValue() throws ParseError, EvaluationException
      Returns the value for evaluated expression
      Returns:
      value of expression
      Throws:
      ParseError - if parsed input string was malformed
      EvaluationException - if one variable was not initialized and parser was created with error = true
      See Also:
    • getExpression

      public String getExpression() throws ParseError
      Returns a string representation of parsed expression with the right parenthesis
      Returns:
      a string representation of parsed expression with the right parenthesis
      Throws:
      ParseError - if parsed input string was malformed
    • getParsedFunctions

      public HashSet<String> getParsedFunctions() throws ParseError
      Returns a Set of all functions parsed in input string
      Returns:
      a set with all parsed functions
      Throws:
      ParseError - if parsed input string was malformed
    • getParsedVariables

      public HashSet<String> getParsedVariables() throws ParseError
      Returns a Set of all variables parsed in input string
      Returns:
      a set with all parsed variables
      Throws:
      ParseError - if parsed input string was malformed
    • getInputString

      public String getInputString()
      Returns input string, without invalid characters
      Returns:
      input string without invalid characters
    • getEngine

      public RandomEngine getEngine()
    • setEngine

      public void setEngine(RandomEngine engine)