Class Parser
java.lang.Object
math.parser.Parser
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected char[]convertInput(String str) Converts input string in a character array and removes unsupported charactersReturns a string representation of parsed expression with the right parenthesisReturns input string, without invalid charactersReturns a Set of all functions parsed in input stringReturns a Set of all variables parsed in input stringgetTree()Returns the tree of the function generated by the parserdoublegetValue()Returns the value for evaluated expressionvoidsetEngine(RandomEngine engine) voidsetVariable(String name, double value) Sets the value for a given variable
-
Constructor Details
-
Parser
-
Parser
Class constructor- Parameters:
str- Input string to be analyzederror- 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
Converts input string in a character array and removes unsupported characters- Parameters:
str- input string- Returns:
- converted string, terminated by TERM character
-
getTree
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
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
Returns the value for evaluated expression- Returns:
- value of expression
- Throws:
ParseError- if parsed input string was malformedEvaluationException- if one variable was not initialized and parser was created witherror = true- See Also:
-
getExpression
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
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
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
Returns input string, without invalid characters- Returns:
- input string without invalid characters
-
getEngine
-
setEngine
-