Git Product home page Git Product logo

math-expr-parser's Introduction

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

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:
  Expr execute result:-5400

math-expr-parser's People

Contributors

gzyangkui avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.