A mathematical expression parser. Three steps is involved in parsing an expression
- Normalize expression to something that can be parsed.
- Execute function call withing expressions and replace function call with value returned by function
- Evaluate the expression to get the final value
These processes are accomplished through the following interfaces.
- ExpressionResolver
- FunctionExecutor
- ExpressionEvaluator
In other to create an instance of Parser. The interface mention above must be provided.The api provides 2 implementation of the interfaces. One for parsing integer expressions and the other for parsing double expressions. To add new functionality users can extend implementation provided by the api or create their own implementation from scratch by directly implementing the interfaces listed above.
String x = "cos(2*(pi))-1"; Parser p = new Parser.Builder() .defaultDouble()//use interface implementation provided by the api .build(); double s = p.parse(x);//parse the expression System.out.println(s);
String x = "10/2+4*2"; Parser p = new Parser.Builder() .defaultInt()//use interface implementation provided by the api .build();int s = p.parse(x);//parse the expression System.out.println(s);