<p>We would like to have user defined formulas in our c++ program.
e.g. The value <em>v = x + ( y - (z - 2)) / 2</em>. Later in the program the user would define x,y and z -> the program should return the result of the calculation. <br />Somewhen later the formula may get changed, so the next time the program should parse the formula and add the  new values. <br/><br/> Any ideas / hints how to do something like this ? <br/><br/> So far I just came to the solution to write a parser to calculate these formulas - maybe any ideas about that ?</p>

[{'answer_id': 28129, 'author': 'OysterD', 'author_id': 2638, 'author_profile': 'https://Stackoverflow.com/users/2638', 'pm_score': 0, 'selected': False, 'text': '<p>Using <a href="http://spirit.sourceforge.net/" rel="nofollow noreferrer">Spirit</a> (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 <a href="http://www.cs.sunysb.edu/~warren/xsbbook/node24.html" rel="nofollow noreferrer">there</a> for example, if needed... (it\'s quite simple to come up with your own).</p>\n\n<p>Note: Spirit is <em>very</em> simple to learn, and quite adapted for such tasks.</p>\n'}, {'answer_id': 28130, 'author': 'Mats Fredriksson', 'author_id': 2973, 'author_profile': 'https://Stackoverflow.com/users/2973', 'pm_score': 3, 'selected': True, 'text': '<p>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. <a href="http://www.lua.org/" rel="nofollow noreferrer">Lua</a> 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.</p>\n'}, {'answer_id': 28134, 'author': 'Lasse V. Karlsen', 'author_id': 267, 'author_profile': 'https://Stackoverflow.com/users/267', 'pm_score': 0, 'selected': False, 'text': "<p>There's generally two ways of doing it, with three possible implementations:</p>\n\n<ol>\n<li>as you've touched on yourself, a library to evaluate formulas</li>\n<li>compiling the formula into code</li>\n</ol>\n\n<p>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.</p>\n\n<p>For C++ I would guess that a library for evaluation would probably exist somewhere so that's where I would start.</p>\n"}, {'answer_id': 28137, 'author': 'Geoff', 'author_id': 1097, 'author_profile': 'https://Stackoverflow.com/users/1097', 'pm_score': 0, 'selected': False, 'text': '<p>If you want to write your own, search for "formal automata" and/or "finite state machine grammar"</p>\n\n<p>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.</p>\n'}, {'answer_id': 28159, 'author': 'levand', 'author_id': 3044, 'author_profile': 'https://Stackoverflow.com/users/3044', 'pm_score': 1, 'selected': False, 'text': '<p>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.</p>\n\n<p>You can then easily enough write a method that recurses through the tree, applying the appropriate operations to whatever values you pass in.</p>\n'}, {'answer_id': 28172, 'author': 'Lehane', 'author_id': 142, 'author_profile': 'https://Stackoverflow.com/users/142', 'pm_score': 0, 'selected': False, 'text': '<p>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. </p>\n\n<p>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.).</p>\n\n<p>Then, you will need to:<br>\nRead in the input char array<br>\nParse it in order to build up a list of variables and actions<br>\nCarry out those actions - in <a href="http://en.wikipedia.org/wiki/BOMDAS" rel="nofollow noreferrer">BOMDAS</a> order</p>\n'}, {'answer_id': 42328, 'author': 'warren', 'author_id': 4418, 'author_profile': 'https://Stackoverflow.com/users/4418', 'pm_score': 1, 'selected': False, 'text': "<p>Building your own parser for this should be a straight-forward operation:</p>\n\n<p>) convert the equation from infix to postfix notation (a typical compsci assignment) (I'd use a stack)\n) wait to get the values you want\n) pop the stack of infix items, dropping the value for the variable in where needed\n) display results</p>\n"}, {'answer_id': 326452, 'author': 'David Robbins', 'author_id': 19799, 'author_profile': 'https://Stackoverflow.com/users/19799', 'pm_score': 0, 'selected': False, 'text': '<p>With <a href="http://www.antlr.org" rel="nofollow noreferrer">ANTLR</a> you can create a parser/compiler that will interpret the user input, then execute the calculations using the Visitor pattern.  <a href="http://www.codeproject.com/KB/recipes/sota_expression_evaluator.aspx" rel="nofollow noreferrer">A good example is here</a>, but it is in C#.  You should be able to adapt it quickly to your needs and remain using C++ as your development platform.  </p>\n'}]
['https://Stackoverflow.com/questions/28124', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3056/']