Question

In reference to the accepted solution in: SO:expression_evaluator

Can anyone provide a version that works with negation as well? things like

((!(0 or !1) and !((0 or 1 or 1) and !1))

need to work as well.

I got it working so that negating the 0's or 1's is fine but I can't get it to work with the negation of whole groups(!'s at beginning of parenthesis)

I tried negating tmp after returning from eval in the *expr == '(' block.. if ! had been seen before calling it, but that didn't work.

Was it helpful?

Solution

The code in that answer is a bit ad-hoc. Parsing is a well-understood field, there's no need for invention. One of the other answers there recommends looking into recursive descent parsing. I second that recommendation.

OTHER TIPS

The algorithm described here can deal with unary operators (operators having one operand), as well as functions.

I once used it as the basis for a complete C expression evaluator with support for symbolic variables for a command-line console on an embedded system. I did not use the accompanying example code, but rather wrote it from scratch to suit my purposes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top