0

I'm looking for a way to parse a string for any valid C++ code, and then run it somehow. In particular, math functions like sin() or cos().

The basic method of parsing every single character doesn't extend to expressions with multiple math functions.

Is there a way to parse and evaluate code from within a std::string?

  • 6
    Not in the language standard, you'd need an interpreter – UnholySheep Feb 01 '19 at 21:47
  • Alright. Thank you for your comment –  Feb 01 '19 at 21:53
  • 1
    [Boost.Spirit](http://boost-spirit.com/home/) might be what you are looking for. – R Sahu Feb 01 '19 at 22:01
  • 1
    Maybe http://chaiscript.com/ could be a good fit aswell. – super Feb 01 '19 at 22:06
  • 1
    to execute arbitrary code there are a lot of duplicate questions [Convert string to code at run time c++](https://stackoverflow.com/q/18766048/995714), [is it possible to make a function execute code from a string on the stack?](https://stackoverflow.com/q/3225124/995714), [execute C++ from String variable](https://stackoverflow.com/q/15841511/995714), [How to run c++ code from c++?](https://stackoverflow.com/q/1541429/995714) – phuclv Feb 02 '19 at 05:28
  • 1
    However just for doing math then it's a different problem [parsing math expression in c++](https://stackoverflow.com/q/11703082/995714), [Evaluate Mathematical Function from String](https://stackoverflow.com/q/9977045/995714), [How to parse mathematical formulae from strings in c++](https://stackoverflow.com/q/20969995/995714)... – phuclv Feb 02 '19 at 05:28
  • @phuclv Thank you, I'll read up on the Shunting yard algorithm. –  Feb 02 '19 at 09:12

0 Answers0