3

I want to develop a logical expression evaluator to compute applicability of certain logical expression against a particular expression. For example,

An expression could be of the form

(A AND B) NOT C

this expression should then be evaluated with another expression like

(B AND C) OR D

The result of the evaluation in the above case is FALSE as the second expression doesn't full fill the first.

The expression can be more complex also, like it can have numerical ranges R(1-100) which means the applicability of the expression is valid through the ranges, like [A-Za-z0-9] in regular expression.

So the expression can be complex like

(A AND B) OR C AND R(1-100) NOT R(80-100)

and this has to be then evaluated by an expression like

(C OR D) AND B NOT R(1-7) AND R(25-100)

There are clear rules on when an expression satisfies another expression. So, if one has to write an expression evaluator, what is the best way to go. Since, I haven't done something before, I would like to have an head start. Any relevant pointers, or similar implementations could be vast help.

Geek Num 88
  • 5,264
  • 2
  • 22
  • 35
  • I may be misunderstanding you, but your first two statements don't seem to be inconsistent (they are satisfiable - as long as `D=TRUE AND C=FALSE`). – Jeff Nov 16 '12 at 01:46
  • A, B, C & D should be considered as tokens. They do not have any logical values, like TRUE or FALSE. An analogy, is you may treat there are several buckets, and each holding an expression like this.In the above case, there are two buckets, each having an expression (A AND B) NOT C – user1828355 Nov 16 '12 at 01:56
  • I think you are looking for something like [this](https://github.com/m-peko/booleval) – NutCracker Sep 02 '19 at 05:41

1 Answers1

0

You can evaluate boolean expressions fairly easily using a stack.

basically as you see "values" you push them on the stack, as you see operators you apply them. Google "boolean expression stack" will give you plenty of hits.

John3136
  • 28,809
  • 4
  • 51
  • 69