boolean expression parser in java

后端 未结 5 775
北恋
北恋 2020-11-27 21:30

Are there any java libraries or techniques to parsing boolean expressions piecemeal?

What I mean is given an expression like this:

T &

5条回答
  •  -上瘾入骨i
    2020-11-27 22:04

    mXparser handles Boolean operators - please find few examples

    Example 1:

    import org.mariuszgromada.math.mxparser.*;
    ...
    ...
    Expression e = new Expression("1 && (0 || (0 && 1))");
    System.out.println(e.getExpressionString() + " = " + e.calculate());
    

    Result 1:

    1 && (0 || (0 && 1)) = 0.0
    

    Example 2:

    import org.mariuszgromada.math.mxparser.*;
    ...
    ...
    Constant T = new Constant("T = 1");
    Constant F = new Constant("F = 0");
    Expression e = new Expression("T && (F || (F && T))", T, F);
    System.out.println(e.getExpressionString() + " = " + e.calculate());
    

    Result 2:

    T && (F || (F && T)) = 0.0
    

    For more details please follow mXparser tutorial.

    Best regards

提交回复
热议问题