Logical Expression Parser

I am trying to create a logical expression parser for expressions like: ((VariableA โ†’ VariableB) AND NOT VariableC) The parser should be able to return whether the result is true or false for the given values โ€‹โ€‹of the variables.

Basically, expressions will only contain variables, logical operators (or, and, implication, equivalence, negation and parentheses).

I would like to ask what is the best way to implement such a parser (using the AST tree or Reverse Polish Notation)? Or maybe some open source parsers already exist that can do the job?

+3
source share
6 answers

What language are you targeting?

, , ANTLR . Java, ( #, ), . (ANTLRWorks), , .

+2

RPN, . - , , . , . , postfix (AKA RPN).

, , , .

2 ยข:)

+1

, , ( ), . , Bison (YACC, C) ANTLR ( , Java), . Coco/R - , . , RPN (, , , RPN). , .

0

: -)

.

- (WFF)

X WFF, X WFF

X Y WFF, (X โ†’ Y) WFF

X Y WFF, (X Y ) WFF

LEX Flex Java .

YACC Bison .

, , .

0

Python, / , pyparsing, .

0

http://ncalc.codeplex.com?

, (, ) , EvaluateFunction/EvaluateParameter. , :

e = ( "Round (Pow (Pi, 2) + Pow ([Pi2], 2) + X, 2)" );

e.Parameters [ "Pi2" ] = ( "Pi * Pi" ); e.Parameters [ "X" ] = 10;

e.EvaluateParameter + = ( , ParameterArgs)   {     if (name == "Pi" )     args.Result = 3.14;   };

Debug.Assert(117.07 == e.Evaluate()); unicode . antler, . , MEF .

0
source

Source: https://habr.com/ru/post/1707713/


All Articles