0

I have a string

string expression=((true AND true)OR false); 

How do I convert this string into a conditional statement so that I can use this in an if statement, i.e.

if((true AND true)OR false))

Like eval in JavaScript - is there any API in C++ or C?

nikolas
  • 8,707
  • 9
  • 50
  • 70
user2656866
  • 105
  • 1
  • 2
  • 3
  • This is a great question and BLUEPIXY (see below) has a surprising answer. This question should NOT be on hold, it is ON TOPIC. – JackCColeman Aug 06 '13 at 23:36

4 Answers4

5

You could use a third party scripting library such as LUA (http://www.lua.org/) to do this, or you could write your own expression parser (which is non-trivial).

Jonatan Hedborg
  • 4,382
  • 21
  • 30
3

Fundamentally, C / C++ code has to be compiled before it will run, which restricts you from running code dynamically the way you can in some other languages.

The closest behaviour you can get easily is to parse the string into some internal representation, and having your code respond to that representation. e.g. using boost spirit :

http://boost-spirit.com/home/

e.g. this answer would do what you ask, and is built with boost spirit. But as you can see, there is a quite a lot of effort involved :

Boolean expression (grammar) parser in c++

Community
  • 1
  • 1
Graham Griffiths
  • 2,196
  • 1
  • 12
  • 15
3
#include <stdio.h>
#include <stdlib.h>

int eval(const char *boolean_expression){
    FILE *fp = fopen("temp.c", "w");
    int ret;
    fprintf(fp, 
        "#include <stdbool.h>\n"
        "#include <iso646.h>\n"
        "int main(void){\n"
        "return (int)%s;}", boolean_expression);
    fflush(fp);
    fclose(fp);
    system("gcc temp.c -o temp");
    remove("temp.c");
    ret=system("temp.exe");
    remove("temp.exe");
    return ret;
}

int main(void){
    const char *expression="((true and true) or false)";
    int condition = eval(expression);

    if(condition)
        printf("YES\n");
    else
        printf("NO!\n");
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • This will work, but with two major caveats: (1) it requires the user to have gcc installed on his computer (and most users don't have that), and (2) it allows the user to run arbitrary C code on the machine running the program, which (depending on the program's usage) could be a big security hole. E.g. the string could contain a command to reformat the hard drive or install a keylogger or etc – Jeremy Friesner Aug 06 '13 at 15:08
  • @JeremyFriesner Course it is. So do not taken too seriously. – BLUEPIXY Aug 06 '13 at 15:17
  • +1 for removing temp.c / temp.exe – Graham Griffiths Aug 07 '13 at 09:25
0

C and C++ does not have an eval type functionality - that is because the language is compiled, and it would require an entire compiler to be part of the runtime if you wanted to be able to "compile some more code" - never mind the difficulties with referencing for example existing variables.

For this type of expression, it's not terribly hard to write some code that parses and evaluates the expression - it requires a stack to deal with the parenthesis, and some simple operators, but that's not terribly hard to do.

Obviously, integrating an existing scripting language would also work.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227