2

For example, I have string ((data1 + data2) ^ data3) / data4 and I want my little program to get this string and do something like this:

int main(int argc, char **argv) {

    double data1 = 1.0;
    double data2 = 2.0;
    double data3 = 3.0;
    double data4 = 4.0;

    double result = parse_formula("((data1 + data2) ^ data3) / data4");

    printf("Result is %d\n", result);
    return 0;
}

Is there such a parser in the standard library? If not, how would I make such a parser myself?

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
J. Leeroy
  • 450
  • 1
  • 5
  • 17

3 Answers3

3

There is nothing ready-made in the standard library for parsing expressions, no. However, it's a nice exercise to roll a parser/evaluator yourself. I don't want to spoil the fun, but here are some thoughts:

The idea is to first parse the input string into some sort of data structure which represents the expression (usually some sort of tree structure) and then 'evaluate' that data structure with some given variable bindings.

The data structure might be a tagged union, something like this:

enum ValueType {
 ConstantValue, VariableValue, Addition, Division
};

struct Value {
  enum ValueType type;

  /* The 'representation' of the value. */
  union {
     int constantValue;
     const char *variableValue;
     struct {
       struct Value *summand1;
       struct Value *summand2;
     } additionValue;
     struct {
       struct Value *dividend;
       struct Value *divisor;
     } divisionValue;
  } repr;
};

For the parsing part, I suggest to read up on 'recursive descent' parsers, which area quite easy to understand and write by hand. The goal is to define a function

Value *parse( const char *s );

which returns a representation for the given string.

The evaluation part is quite straightforward and lends itself to recursion. The goal is to define a function

int eval( const Value *v, ??? bindings );

...where ??? would be some type appropriate for holding variable bindings (e.g. a string to int mapping). Depending on the 'type' of the given value, it will perform the arithmetic operation, e.g.:

int eval( const Value *v, ??? bindings ) {
  switch ( v->type ) {
    case ConstantValue:
      return v->repr.constantValue;
    case Addition:
      return eval( v->repr.additionValue.summand1 ) + eval( v->repr.additionValue.summand2 );
    ...
Community
  • 1
  • 1
Frerich Raabe
  • 90,689
  • 19
  • 115
  • 207
  • I don't consider this to be the right answer. The question specifically asked if there was available a C library that could parse and evaluate an expression. The questioner didn't ask if there was something in the standard library, just whether such a library exists anywhere. The answer is obviously yes. Yes one can learn a lot by implementing your own parser and evaluator but this is not what the questioner asked for. – rhody Aug 27 '17 at 18:47
  • @rhody Note that the OP also asked "Or what is the best way to code it by myself?". However, if you would like to share libraries which may be useful, feel free to add an answer. – Frerich Raabe Aug 27 '17 at 19:22
2

There is no such function in the standard library, no.

There are any number of libraries, I'm not going to recommend one here.

Note that no library will allow "automatic" access by name to your program's variables; they are not available at runtime. You're going to have to find an expression evaluator with variable support, and initialize the variables in the evaluator before trying to evaluate the formula itself.

unwind
  • 391,730
  • 64
  • 469
  • 606
0

There are quite a number of parsers available, both in C++ and plain C. For C++ there is muParser:

http://beltoforion.de/article.php?a=muparser&hl=en&s=idPageTop#idPageTop

For C I found this little one which looks promising:

https://github.com/codeplea/tinyexpr

rhody
  • 2,274
  • 2
  • 22
  • 40