Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gzyangkui/math-expr-parser
https://github.com/gzyangkui/math-expr-parser
Last synced: 3 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/gzyangkui/math-expr-parser
- Owner: GZYangKui
- License: apache-2.0
- Created: 2022-06-12T14:57:36.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-06-15T14:29:35.000Z (over 2 years ago)
- Last Synced: 2024-11-05T20:11:40.602Z (about 2 months ago)
- Language: Java
- Size: 23.4 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Math-expr-parser
> Mathematical expression calculation based on recursive descent analysis
## Procedural grammar
```
Add ——> Mul | Add + Mul | Add - Mul
Mul ——> Pri | Pri * Mul | Pri / Mul
Pri ——> num | (Add)
```> Note: Currently only four operations('+'、'-'、'*'、'/') are supported
## Example
```java
import cn.navclub.mexpr.parser.ASTParser;
import cn.navclub.mexpr.parser.model.ASTNode;public class Test {
public static void main(String[] args) {
String expr = "3+2-5*(((1+1)*6*(6+3))*10)+5";
//Instance math expr parser
ASTParser parser = new ASTParser(expr);
//Get math expr abstract syntax tree
ASTNode node = parser.astNode();
//Execute abstract syntax tree
double rs = parser.execute();
//Output execute result
System.out.printf("Expr execute result:%d\n",rs);
}}
```
+ Execute example code terminal will output:
```shell
Expr execute result:-5400
```