Skip to content

Latest commit

 

History

History
38 lines (23 loc) · 1.7 KB

File metadata and controls

38 lines (23 loc) · 1.7 KB

A mathematical function parser for java

Supported syntax

It can parse + - * / ^ ( ) as in 53 * 34 or (2+3)*4^2

It can parse sqrt log logten sin cos tan asin acos atan as in sqrt(9) or cos(343) (log is log to base of 2, and logten is log to the base of 10)

It can parse pi e to return the constants pi and e.

Finally it can parse any variable name matching ^\w+$.

How to use it

mvn compile
java -cp target/classes de.oglimmer.math.FunctionParser "((2+3)*-3)+2+x*y*sin(pi/2)" "x" "-34" "y" "3"

(First parameter is the function, any 2nd and 3rd is a variable and its value)

How it works

FSM

This project defines a generic FSM framework in the package fsm. It's used by LexicalAnalyzer.java, where this package also implements the Event and Action interfaces defined by the FSM framework and the package stateImpl implements all the FSM states.

Math parser

Step 1 - the lexical analysis (aka the tokenizing) is done in LexicalAnalyzer.java and the resulting objects are Token.java.

Step 2 - the conversion from tokens to AST is done from a method inside of Token.java public ASTNode toASTNode() {...} and by using the method Expression add(ASTNode toAdd) on the first Expression.java with the subsequent ASTNode.java.

Diagram