Skip to content

Kodak1234/Parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Parser

A mathematical expression parser. Three steps is involved in parsing an expression

  1. Normalize expression to something that can be parsed.
  2. Execute function call withing expressions and replace function call with value returned by function
  3. Evaluate the expression to get the final value

These processes are accomplished through the following interfaces.

  1. ExpressionResolver
  2. FunctionExecutor
  3. 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.

Parsing Expression as Double

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);

Parsing Expression as Integer

 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);