Skip to content

IRuuy/MathExpressionParser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MathExpressionParser

JUnit Java Maven

Russian-language documentation

The purpose of the work

This project solves the following problem: in the case when there is a need to build, syntactically bypass the abstract tree of mathematical expressions, with easy customization of tokens, priorities of operations without the need to edit the grammar and re-engineer the parser code.

This solution has one non-obvious drawback. The parser works according to the sorting station algorithm, because of this, there is a need for a separate operation for the unary minus - "~".

Table of content

Detailed description of the logic of the components

Example of constructing a syntactically abstract tree

Statement: (field_1 > 5 AND field_1 < 10) AND NOT (field_2 = "value1" OR field_2 = "value2")

Syntactically abstract tree: Синтаксически абстрактное дерево

Get Started

Working with the parser

For the default case, you can use the MathExpressionParserFacade object:

MathExpressionParserFacade parserFacade = new MathExpressionParserFacade();
parserFacade.parse(statement);

Underneath the facade lies the following logic, it is recommended to use the following method to work with the parser.

Lexer lexer = new Lexer();
Parser parser = new MathExpressionParserFactory().create();
TokenStream stream = lexer.getTokenStream(statement);
ExpressionNode node = parser.parse(stream);

Traversing a constructed syntactically abstract tree

The parsing result can be bypassed using - DefaultExpressionNodeVisitor:

ExpressionNodeVisitor visitor = new DefaultExpressionNodeVisitor();
node.accept(visitor);

You can also add DefaultExpressionNodeVisitor to variables, which will be taken into account when traversing the tree:

Map<String, VisitorData> data = new HashMap<>();
data.put("key", new VisitorData("value"));

ExpressionNodeVisitor visitor = new DefaultExpressionNodeVisitor(data);
node.accept(visitor);

References

  1. https://en.wikipedia.org/wiki/Shunting_yard_algorithm
  2. https://habr.com/ru/articles/524874/

About

Parser of mathematical expressions, witch working on the marshalling yard algorithm.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages