tags:

views:

732

answers:

8

We would like to have user defined formulas in our c++ program. e.g. The value v = x + ( y - (z - 2)) / 2. Later in the program the user would define x,y and z -> the program should return the result of the calculation.
Somewhen later the formula may get changed, so the next time the program should parse the formula and add the new values.

Any ideas / hints how to do something like this ?

So far I just came to the solution to write a parser to calculate these formulas - maybe any ideas about that ?

A: 

Using Spirit (for example) to parse (and the 'semantic actions' it provides to construct an expression tree that you can then manipulate, e.g., evaluate) seems like quite a simple solution. You can find a grammar for arithmetic expressions there for example, if needed... (it's quite simple to come up with your own).

Note: Spirit is very simple to learn, and quite adapted for such tasks.

OysterD
+2  A: 

If it will be used frequently and if it will be extended in the future, I would almost recommend adding either python or LUA into your code. LUA is a very lightweight scripting language which you can hook into and provide new functions, operators etc. If you want to do more robust and complicated things, use Python instead.

Mats Fredriksson
A: 

There's generally two ways of doing it, with three possible implementations:

  1. as you've touched on yourself, a library to evaluate formulas
  2. compiling the formula into code

The second option here is usually done either by compiling something that can be loaded in as a kind of plugin, or it can be compiled into a separate program that is then invoked and produces the necessary output.

For C++ I would guess that a library for evaluation would probably exist somewhere so that's where I would start.

Lasse V. Karlsen
A: 

If you want to write your own, search for "formal automata" and/or "finite state machine grammar"

In general what you will do is parse the string, pushing characters on a stack as you go. Then start popping the characters off and perform tasks based on what is popped. It's easier to code if you force equations to reverse-polish notation.

Geoff
+1  A: 

You can represent your formula as a tree of operations and sub-expressions. You may want to define types or constants for Operation types and Variables.

You can then easily enough write a method that recurses through the tree, applying the appropriate operations to whatever values you pass in.

levand
A: 

To make your life easier, I think getting this kind of input is best done through a GUI where users are restricted in what they can type in.

If you plan on doing it from the command line (that is the impression I get from your post), then you should probably define a strict set of allowable inputs (e.g. only single letter variables, no whitespace, and only certain mathematical symbols: ()+-*/ etc.).

Then, you will need to:
Read in the input char array
Parse it in order to build up a list of variables and actions
Carry out those actions - in BOMDAS order

Lehane
+1  A: 

Building your own parser for this should be a straight-forward operation:

) convert the equation from infix to postfix notation (a typical compsci assignment) (I'd use a stack) ) wait to get the values you want ) pop the stack of infix items, dropping the value for the variable in where needed ) display results

warren
A: 

With ANTLR you can create a parser/compiler that will interpret the user input, then execute the calculations using the Visitor pattern. A good example is here, but it is in C#. You should be able to adapt it quickly to your needs and remain using C++ as your development platform.

David Robbins