2

I am running a simple program:

#include<iostream>
#include<math.h>
  using namespace std;
  double fu (double x) {
      double func = pow(x,0.5);
      return func;
  }

  int main (int argc, char* argv[]) {
      double x = 2;
      double func = fu(x);

      cout<<"f(x) = "<<func<<endl;
      return 0;
  }

Here func is a function of which value is calculated at x.
Suppose, I need to use this program from another program or if I want to give a function, such as pow(x,0.5) or sqrt(1+sin(x)) during command line running of this program.

How I can do that? If I do it using argv, then can you suggest how can I convert a string into an expression func (that can be evaluated by C++ compiler)?

Any suggestions?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
c202933
  • 31
  • 1
  • 3
  • 1
    You need to parse the command line options (which are in `argv`). – Cameron Nov 27 '14 at 23:04
  • @Cameron, modified the question. How to convert a string into an expression that can be evaluated in C++? – c202933 Nov 27 '14 at 23:09
  • 1
    Well, you'd need a C++ front-end :P If your expressions are relatively simple, you could get away with a simple hand-rolled parser instead. – Cameron Nov 27 '14 at 23:10
  • 2
    @c202933 parsing and evaluating expressions is not difficult, but it's not trivial at all. What do you need exactly? – Stefano Sanfilippo Nov 27 '14 at 23:12
  • @Cameron I'm net to C++ and did not completely got your point. I want something running like like `program.exe sin(1+x^2) 2` – c202933 Nov 27 '14 at 23:16
  • My suggestion is to simplify the syntax as much as you can. It might be enough for you to provide a preset list of functions which you can invoke, rather than arbitrary adhoc functions. Then you can just give them each a name. – paddy Nov 27 '14 at 23:16
  • Here, `sin(1+x^2)` will be evaluated at `2`. I want that. – c202933 Nov 27 '14 at 23:18
  • 2
    possible duplicate, http://stackoverflow.com/questions/9439295/convert-string-to-mathematical-evaluation – M.M Nov 27 '14 at 23:19
  • 8
    @OP : your problem is not to give command line input, it is to do the actual using of said input. To understand the magnitude of what your want to achieve look at the code of this project (and obviously use their library don't try to roll your own). http://www.partow.net/programming/exprtk/index.html – Félix Cantournet Nov 27 '14 at 23:24
  • @FélixCantournet, why not try to roll your own?! As an exercise, it is already good to attempt to do your own compiler, if that's something you are interesting in it can be very fulfilling. – Alexis Wilke Nov 28 '14 at 00:04
  • 3
    I'm sorry i didn't mean to sound like an ass. What i meant is, what the OP seems to want to do represents a daunting task, that extends far beyond his current skills as represented by his question. No doubt that this is a very interesting challenge, for later, but i fear that if he tries to do this from scratch it will only be discouraging. I do strongly recommend the OP to check out the code of the library i mentionned, because it does what he wants to do, and he can get a feel of what is needed in terms of complexity and scale. – Félix Cantournet Nov 28 '14 at 09:42

2 Answers2

0

There's no easy way to automatically evaluate that kind of math expressions from a string in c++ (like you may have experienced with kind of eval() expressions in scripting languages).

C++ is a compiled language, and the compiler can't actually resolve anything to concrete function calls and parameter values at compile time.

You have to parse these strings at runtime, break down the input tokens and parameters, and map the parsing results to concrete function calls of the appropriate <cmath> functions applying the parsed parameter values, from within your program.

That's not trivial, and there are a number of solution approaches available. But these completely depend on the kind of math language you want to parse from the command line arguments.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • I was looking something similar to `eval` in matlab. But you said no automatic evaluation. – c202933 Nov 27 '14 at 23:26
  • @c202933 There's nothing like `eval` in c++, there are libraries to define certain languages though, e.g. [boost::spirit](http://boost-spirit.com/home/). – πάντα ῥεῖ Nov 27 '14 at 23:30
  • When I tried to use boost::spirit, it had a lot of problem even just #including the damn thing... I would rather suggest flex and bison, both of which can be used with C++. – Alexis Wilke Nov 27 '14 at 23:38
  • @AlexisWilke There's a number of solutions, I've seen working `boost::spirit` based solutions for DSL's, [`boost::wave`](http://www.boost.org/doc/libs/1_57_0/libs/wave/) also looks helpful. Bison & Flex look a bit outdated, and overly complicated (additional build steps outside c++) nowadays. – πάντα ῥεῖ Nov 27 '14 at 23:46
  • Yes, my [snap::snap_parser](https://sourceforge.net/p/snapcpp/code/ci/master/tree/snapwebsites/lib/snap_parser.h) classes are also an attempt in creating a "spirit" like parser in C++. It's pretty complete too. All part of the same library. – Alexis Wilke Nov 27 '14 at 23:55
0

There is not such thing as 'execute an expression in a string' in standard C++. The reason is because C++ is a compiled language (i.e. the output is binary code that the processor can directly execute without any intermediate interpreter). So it cannot just interpret an expression (or code in general) from a string.

I have an implementation in Snap Websites, there is C++ code:

https://sourceforge.net/p/snapcpp/code/ci/master/tree/snapwebsites/lib/snap_expr.cpp

Go here to find the other files:

https://sourceforge.net/p/snapcpp/code/ci/master/tree/snapwebsites/lib

However, instead of implementing an expression parser and execution environment, you may want to make use of an existing library (mine is an example, obviously, actually I have a tool named snapexpr under snapwebsites/src which will do exactly what you are talking about!) In that case you could also choose a different language. For example, Qt offers QScript:

http://qt-project.org/doc/qt-5/qtscript-index.html

In that case, you have to write JavaScript code, but since JavaScript is relatively close to C++, it may work in your environment.

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156