3

I need a simple boolean expression parser for a little C++ templating engine I'm working on, and I was looking for some library that could do it for me. Unfortunately, I didn't really find anything easy to integrate. I found this spirit based solution, which seems like a good starting point, but it doesn't quite have all the features I need. I am aware of the spirit mini-c example, but it seems way too complex and it would probably take me forever to somehow strip what I need. Basically, what I need is the following:

  • Variables with variable types: integer, double, string, array
  • Operators "and", "or", "xor", "not" and the correct/expected precedence
  • Comparison operators "==", "!=", "<", "<=", ">", ">="
  • Arithmetic operators such as "+", "-", "/", "*" and their expected precedence
  • Optional: C-style function calls that return a value, e.g. somefunc(12, "abc") that I can somehow map to a c++ function during evaluation

I need to be able to supply the parser with all variables and their values, including arrays. The idea is to be able to evaluate expressions such as:

var == 2 + 3
var != 2.32
var == "foo"
somearr[var][2] == "bar"
(var == 2) or (var > 100)

Optionally for functions I'd be nice to be able to do something like this:

var.length() == 3
"bla".length() == 3

or alternatively

length(var) == 3
length("bla") == 3

I am somewhat flexible on the syntax details, so does anyone know of an open source library that can do this and is easy to integrate? Or how I can maybe extend some spirit based example?

For example, in this boolean expression example I don't quite understand how I would set the variables to some values before parsing them. But even if i did, it wouldn't quite support enough of what I need.

Any help/pointers are appreciated! I spent quite some time trying to understand spirit, but I guess I still don't understand it well enough to accomplish anything beyond very simple parsing.

Community
  • 1
  • 1
user2129246
  • 109
  • 1
  • 5
  • 2
    you have two options here: _reinvent the wheel_ or _learn boost::spirit_ :) at first option you've supposed to write your own library or take smth else/simple than boost... – zaufi Aug 03 '13 at 18:27
  • 1
    For simple expressions, you don't need fancy machinery like boost::spirit. You also don't have to reinvent the wheel. See how to build your own expression parser at http://stackoverflow.com/a/2336769/120163 – Ira Baxter Aug 04 '13 at 01:29
  • 1
    Boost spirit will generate pretty fast code if you care about performance. The code will probably be not too verbose as well. The one downside to spirit is that with complex grammar compilation times of your parser go very high. – Artem Tokmakov Aug 04 '13 at 03:38
  • 2
    [Many](http://www.codeproject.com/Articles/7335/An-extensible-math-expression-parser-with-plug-ins) [alternatives](http://www.codeproject.com/Articles/27654/General-Expression-Parser-and-Evaluator) [are](https://bitbucket.org/tartakynov/mathpresso/wiki/Home) [available](http://www.codeproject.com/Articles/7773/Fast-Mathematical-Expressions-Parser). – Jerry Coffin Aug 04 '13 at 06:15

1 Answers1

0

Probably the best way to learn about that is by reading chapter 6 of Stroustrup's "The C++ Programming Language", which discusses at some length source code for a desk calculator (available here: http://www.stroustrup.com/3rd_code.html).

It's basic (since it's a teaching tool) but easy to modify to suit your needs.

Răzvan Cojocaru
  • 883
  • 9
  • 12