   Evaluation of string math expressions
  Given a string such as "3+(4 | 0x1)", for example, is there a simpleway to collapse this into "8" or 8?  Something like atoi("3+(4 | 0x1)")would be nice!My game reads a rule file.  The rules are dependent on various mathematical combinations of #define's. I would like to run a preprocessor on the rule file before reading it(either runtime or compile time) to be able to set rule attributes properly based on #define settings.thankssteve coward   
  Why don't you just run the C preprocessor on the file?   ;  I do.In my rules file -TOKEN_CONTROL_ID:GAME_STATUS_BARwith following defines -#define TOKEN_CONTROL_ID "control id"#define GAME_STATUS_BAR   BASE_CONTROL_ID + NUM_SETUP_CONTROLS * MAX_PLAYERS + 0get this in my string when parsing file{""control id":100 + 5 * 4 + 0"}is there a way to simply evaluate "100 + 5 * 4 + 0"?   ;Quote:Original post by steve cowardis there a way to simply evaluate "100 + 5 * 4 + 0"?You could embed an interpreter or rely on external tools. There are no C or C++ function that take a string and mathematically evaluate it.On recent versions of Windows, the shell command SET /A seems promising. You could set the value of an arbitrary environment variable and then retrieve it, but that's a pure hack.   ;thanks you for your help.I think I will start with code available on this forum at:My math expression compiler/evaluator.. ;  I've just finished working through a book called Writing Compilers and Interpreters, and I now have a fully working compiler which I plan to use in many projects. The book is based on a Pascal syntax but you can easily adapt that to any syntax you like.It uses recursive decomposition to decompose the original code into expressions and such. I won't try to explain it all here, but I definitely recommend that book - it helped me a lot!   ;  Since we're recommending books on the subject, I should just point out that the ultimate C++ book, Stroustup's The C++ Programming Language, has the most elegant and concise example of an expression evaluator I have ever seen which totally revolutionised my approach to writing compilers and interpreters.   ;  This sounds like an ideal task for Python.I will assume a file of entries, one per line, of the form"quoted identifier" : mathematical + expressionThe trick I will be using is that Python dicts are written as comma-separated key/value pairs, where the pair just happens to be separated by a colon. And also making use of the Python eval() statement, which parses a string as if it were a Python source code expression.dictstring = '{'data = file("rules.txt")for line in data: dictstring += line + ','# Not sure about that. You might need 'line[-1]' instead, to take off the# end-of-line character.dictstring += '}'# evaluate the string as a dict, which will implicitly evaluate the mathematical# expression for each value, and then grab the text representation of the result# - which will be basically the same as the input, except the whitespace will be# normalized, and equations solved.# Then, take off the opening and closing brackets, and split up the pairs onto# separate lines.parsed = repr(eval(dictstring))[1:-1].split(',')data.close()data = file("rules.txt", "w")for line in parsed: data.write(line)You could go further, of course, Instead of using #define-syntax, you could make things in such a way that the Python script does the interpolation of variables for you. Alternatively, you could have the Python script invoke the C preprocessor for you, rather than doing it manually before running the script.   