I'm coding up an algorithm which counts the number of solutions to a system of constraints. The constraints in this case are predicat开发者_如何学Goes such as "a < b" or "a = 3+b or a = 6+b", which I will receive as input strings. I need to be able to ask if any given selection of values for the variables is a valid combination, and I would rather not have to build a parser to do it.
Is there a quick C++ library out there that evaluates strings as mathematical expressions? I think I have a simple way of transforming "a < b" into "5 < 6", I just need a way to evaluate that.
Writing a simple arithmetic expression parser was an exercise at school. It's really not that complicated. Give it a try!
The idea was to convert it to reverse Polish notation and then it becomes dead easy to evaluate it. The above Wikipedia article has all the details you need.
Also the tools "Flex" and "Bison" come to mind, although I haven't used them myself.
There is no easy way to do this in C or C++
You will need to build your own parser.
I am not aware of a library that does that for you, but I would do it myself, given how simple it is (it seems so on the surface at least).
And validating it would be very easy and ideal for a regular expression -- you just omit the spaces.
You could download a relatively small interpreted language, like Lua, and use their existing code. For trivial ops like this it's pretty easy to use.
Here is a tank to kill a fly approach.. :)
boost::spirit.
It's pretty trivial to define parsers for what you are trying to do, and they have some examples which are what you are looking for. May save you digging around bison, lex and yacc..
This C++ implementation might be what you want.
Embed perl http://perldoc.perl.org/perlembed.html it is fast and does what you need.
精彩评论