Skip to content

For boolean expressions and comparisons on numbers. 3 evaluators are implemented: for tracing, ast creation and boolean evaluation

Notifications You must be signed in to change notification settings

curiosag/expressionparser

Repository files navigation

ExpressionParser

Parses boolean expressions and comparisons on numbers using a generic evaluator. Otherwise the Scala black magic level is trivial, the ordinary application of parser combinators. 3 evaluators are implemented for

  • Tracing and a simple string representation of the parse result
  • Abstract syntax tree creation (AstEvaluator)
  • Boolean evaluation taking an evaluation environment as constructor parameter which interprets numbers, variable values and function results. It deals with the possibility that evaluation may fail anywhere due to missing or flawed data. It shows a pretty paranoid relationship to the parser as well.

There's a converter to generate a DOT graph description from the AST for visualization (Ast2Dot). Here's an example.

By the way at another place there's an implementation for a boolean evaluation environment operating on raw data in a web scraper environment.

That's covered:

identifiers: x, y ...
boolean literals: false, true
numeric literals: 1, -1, 1.1 
boolean expressions: ! false & true | (false | x)
numeric comparisons and numeric intervals: ! 1 > 2 & 0 <= x < 100
function call syntax, allowing only identifiers as params: passes(refvalue, filterid) & someFct(a, x, y)

Note that "&" and "|" have equal precedence as defined here

 <b-expression>::= <b-term> ["|"|& <b-term>]*
 <not-factor>  ::= [!] <b-factor>
 <b-factor>    ::= <booleanliteral> | <identifier> | numRelOp | numInterval | functionCall | (<b-expression>) 
 <functionCall>::= <identifier> (<identifier> [<listSep> <identifier>]*)
 <numRelOp> 	  ::= <numericTerminal> <relOp> <numericTerminal>
 <numInterval> ::= <numericTerminal> <relOp> <numericTerminal> <RelOp> <numericTerminal>
 <booleanliteral>   ::= true | false 
 <numericTerminal> ::= <identifier> | <numericLiteral>
 <relOp>				::= > | < | >= | <= | ==
 <listSep>			::= ,

About

For boolean expressions and comparisons on numbers. 3 evaluators are implemented: for tracing, ast creation and boolean evaluation

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages