Git Product home page Git Product logo

grammar's Introduction

Build Status Download

grammar

grammar is a shunting-yard based expression parser to generate an Abstract Syntaxt Tree (AST). At its heart it is a library to allow you to quickly create your own expression language. But off course, since it is so easy, grammar also ships with some default expression parsers and evaluators for basic math and boolean support.

The tokenizer, parser and also evaluators are highly customizable. For example, you can configure the operators, the terminal symbols and customize about almost everything else.

Out of the box math and boolean parser/evaluator support

grammar is able to evaluate expressions like:

Boolean expression examples

res = eval("!(true | false) | (false & true)")
res = eval("not(true or false) or (false and true)")

Math expression examples

res = eval("10 + 5.6 / 20 * (40 % 2)")

Variable support

If your expression contains variables, grammar supports them aswell:

EvalContext<Double> context = new EvalContext<>();
context.setVariable("alpha", 15d);
res = eval("90 - alpha", context);

Just include a EvalContext<Double> context in your eval argument, and variable-reference nodes are substituted for you.

Custom Parser

This is an example of the default math expression parser configuration - you can create one for your own needs:

Math Parser

public class MathExpressionParser extends ExpressionParser {

    public MathExpressionParser(){
        super(new OperatorSet(
                new Operator("+", 2, true, Arity.Binary),
                new Operator("-", 2, true, Arity.Binary),
                new Operator("*", 3, true, Arity.Binary),
                new Operator("/", 3, true, Arity.Binary),
                new Operator("^", 4, false, Arity.Binary),
                new Operator("%", 4, false, Arity.Binary),

                new Operator("+", 99, true, Arity.Unary),
                new Operator("-", 99, true, Arity.Unary)
                        ),
                new Token(TokenType.Whitespace, " "),
                new Token(TokenType.Whitespace, "\t"),
                new Token(TokenType.Parentheses_Open, "("),
                new Token(TokenType.Parentheses_Closed, ")"),
                new Token(TokenType.Whitespace, ",")

        );
    }
}

Boolean Parser

public class BooleanExpressionParser extends ExpressionParser  {

    public BooleanExpressionParser(){
        super(new OperatorSet(
                    new Operator("&", 3, true, Arity.Binary, "and"),
                    new Operator("|", 3, true, Arity.Binary, "or"),
                    new Operator("^", 3, true, Arity.Binary, "xor"),
                    new Operator("!", 99, true, Arity.Unary, "not", "~")
                ),
                new Token(TokenType.Literal, "true"),
                new Token(TokenType.Literal, "false"),
                new Token(TokenType.Whitespace, " "),
                new Token(TokenType.Whitespace, "\t"),
                new Token(TokenType.Parentheses_Open, "("),
                new Token(TokenType.Parentheses_Closed, ")")
        );
    }
}

How to get started

You should have a look at our Unit tests here to get a feeling how to use this library.

grammar's People

Contributors

isnull avatar

Watchers

 avatar James Cloos 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.