Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Business rule expression parser/evaluation [closed]

I'm looking for suggestions of portable lightweight libraries written in C++, that support mathematical and business rule expression and evaluation. I understand C++ doesn't provide such functionality in the STL.

The basic requirement is as follows:

The expressions to be evaluated will be comprised of numbers and strings and variables either representing numbers or strings.

Some of the expressions are expected to be evaluated many times per second (1000-2000 times), hence there is a requirement for high performance evaluations of the expressions.

Originally the project at my company, we encode all the business rules as classes that derived from a base expression class. The problem is that this approach does not scale well as the number of expressions increases.

I've googled around, but most "libraries" I could find are pretty much simple examples of the shunting yard algorithm, most of the expression parsers, perform parsing and evaluation in the same step making them unsuitable for continuous reevaluations, and most only support numbers.

What I'm looking for:

  1. Library written in C++ (C++03 or C++11)
  2. Stable/production worthy
  3. Fast evaluations
  4. Portable (win32/linux)
  5. Any suggestions for building high performance business rules engine.

Example business rule:

'rule_result = (remaining_items < min_items) and (item == "beach ball")'

like image 516
Zamfir Kerlukson Avatar asked Oct 06 '13 22:10

Zamfir Kerlukson


2 Answers

See the C++ Mathematical Expression Library outlined in this answer.

But, if you really want speed, consider compiling the expressions as C/C++ directly, then load them dynamically (shared objects/DLLs).

like image 101
Macke Avatar answered Nov 08 '22 05:11

Macke


Have you considered generating your own parser with Bison + Flex? It uses a FSM-based LALR parser implementation that is fast and is easy to write, and supports evaluation of expressions while you're parsing them, as well as AST generation for repeated evaluation.

like image 28
Renan Gemignani Avatar answered Nov 08 '22 04:11

Renan Gemignani