C/C++

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • Evaluate string expression in if statement?

    8 answers - 568 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    Is there anyway to literally evaluate the contents of a string in an if
    statement?
    For example:
    int i = 0;
    char * str = "i == 0";
    if(str) /* I know this doesn't do what I want */
    {
    }
    would be the same as:
    int i = 0;
    if(i == 0)
    {
    }
    The general application is that I'm in interested in reading if
    conditionals from a file, not only the conditional variables but the
    conditional operators as well (the whole phrase).
    Any ideas appreciated.
    Thanks.
  • No.1 | | 979 bytes | |


    No Such Luck wrote:
    Is there anyway to literally evaluate the contents of a string in an if
    statement?

    For example:

    int i = 0;
    char * str = "i == 0";

    if(str) /* I know this doesn't do what I want */
    {

    }

    would be the same as:

    int i = 0;

    if(i == 0)
    {

    }

    The general application is that I'm in interested in reading if
    conditionals from a file, not only the conditional variables but the
    conditional operators as well (the whole phrase).

    Any ideas appreciated.

    Is "don't use C" an idea? I suppose you could generate C
    code, compile it from within your program, and go execute
    it somehow. I don't think you can do what you want by
    interpretering arbitrary strings -- e.g. how do you
    associate the string "i" with the variable i?

    <Tconsider using a scripting language, eg. perl
    and the eval command </T>
    -David

  • No.2 | | 720 bytes | |

    No Such Luck said:

    Is there anyway to literally evaluate the contents of a string in an if
    statement?

    Yes and no. If you're looking for an eval() function, prepare to be
    disappointed. But you can write a C interpreter if you like, and pass it
    the expression you wish to parse. Alternatively, you can get your program
    (A) to write a program (B) containing the string and any necessary program
    furniture, together with code that will exercise the condition and write
    the result to a file. Your A program can then invoke a compiler (using the
    system() function) to compile B; it can then invoke B; and finally it can
    read B's output file to find the result. Very, very messy.
  • No.3 | | 753 bytes | |


    Richard Heathfield wrote:
    No Such Luck said:

    Is there anyway to literally evaluate the contents of a string in an if
    statement?

    Yes and no. If you're looking for an eval() function, prepare to be
    disappointed. But you can write a C interpreter if you like, and pass it
    the expression you wish to parse. Alternatively, you can get your program
    (A) to write a program (B) containing the string and any necessary program
    furniture, together with code that will exercise the condition and write
    the result to a file. Your A program can then invoke a compiler (using the
    system() function) to compile B; it can then invoke B; and finally it can
    read B's output file to find the result. Very, very messy.
  • No.4 | | 844 bytes | |


    Richard Heathfield schrieb:

    No Such Luck said:

    Is there anyway to literally evaluate the contents of a string in an if
    statement?

    Yes and no. If you're looking for an eval() function, prepare to be
    disappointed. But you can write a C interpreter if you like, and pass it
    the expression you wish to parse.

    Depending on the complexity of the expressions he has to evaluate, much
    less may be required than a C interpreter.
    Almost every introduction to yacc contains an example desk calculator
    program, i.e. a programm that parses and evaluates expressions. Simple
    conditional expressions should not be too hard to derive from those
    examples.
    The more severe difficulty is the linkage between variables in the C
    program and identifiers in the expression strings.

  • No.5 | | 1110 bytes | |

    "No Such Luck" <no_suchluck@hotmail.comwrites:
    Is there anyway to literally evaluate the contents of a string in an if
    statement?

    For example:

    int i = 0;
    char * str = "i == 0";

    if(str) /* I know this doesn't do what I want */
    {

    }

    would be the same as:

    int i = 0;

    if(i == 0)
    {

    }

    Some people have suggested elaborate solutions like using an external
    interpreter. Aside from being way too much work, they're not going to
    work if you want the expression encoded in the string to refer to
    variables in your own program.

    The general application is that I'm in interested in reading if
    conditionals from a file, not only the conditional variables but the
    conditional operators as well (the whole phrase).

    You might be able to implement an expression evaluator that takes a
    string and evaluates it as an expression, but you'll need to figure
    out how to evaluate variable names within the expression. Mapping
    them to variables in your own program probably doesn't make sense.
  • No.6 | | 1222 bytes | |


    "Keith Thompson" <kst-u@mib.orgwrote
    Some people have suggested elaborate solutions like using an external
    interpreter. Aside from being way too much work, they're not going to
    work if you want the expression encoded in the string to refer to
    variables in your own program.
    >
    >The general application is that I'm in interested in reading if
    >conditionals from a file, not only the conditional variables but the
    >conditional operators as well (the whole phrase).
    >

    You might be able to implement an expression evaluator that takes a
    string and evaluates it as an expression, but you'll need to figure
    out how to evaluate variable names within the expression. Mapping
    them to variables in your own program probably doesn't make sense.

    I think that if you don't understand why a string with the value "myvar"
    cannot easily be used to access variable "myvar" in the program, you
    probably don't understand enough to mess with expression parsers.
    Why do you need these conditional expressions in a file? Is there not some
    way of achieving what you want by hard-coding them?

  • No.7 | | 2172 bytes | |

    Andrew Poelstra said:

    Richard Heathfield wrote:
    >No Such Luck said:
    >>

    >Is there anyway to literally evaluate the contents of a string in an if
    >statement?
    >>

    >Yes and no. If you're looking for an eval() function, prepare to be
    >disappointed. But you can write a C interpreter if you like, and pass it
    >the expression you wish to parse. Alternatively, you can get your program
    >(A) to write a program (B) containing the string and any necessary
    >program furniture, together with code that will exercise the condition
    >and write the result to a file. Your A program can then invoke a compiler
    >(using the system() function) to compile B; it can then invoke B; and
    >finally it can read B's output file to find the result. Very, very messy.
    >>

    You can't do this natively in C. Also, you can't use the system command
    properly and cross-platform because of S differences (I hope I didn't
    need to state that).

    You can, if you are prepared to capture the appropriate local command from
    the user. (This gets messier and messier.)

    You will have to write your own parsing program. It's actually not as
    difficult as it seems as long as you know what you need to parse.

    Handling left-to-right associativity correctly can be a nuisance. It's the
    kind of problem where you spend hours or even days scratching your head and
    trying to think your way through a recursive corkscrew, and then finally
    you find a way to do it that seems to work okay - and whenever anyone ever
    mentions that problem again in the future, you say "oh, that's trivial".

    An interesting option similar to Richard's idea, but a tiny bit less
    messy would be to make a CGI app that outputs the code into a
    Javascript, which evaluates the expression and returns the code via an
    XHTTPRequest object. This would require the assistance of a web
    browser, however.

    Compiler, browser, whatever. :-)
  • No.8 | | 1407 bytes | |

    Keith Thompson said:

    "No Such Luck" <no_suchluck@hotmail.comwrites:
    >Is there anyway to literally evaluate the contents of a string in an if
    >statement?
    >>

    >For example:
    >>

    >int i = 0;
    >char * str = "i == 0";
    >>

    >if(str) /* I know this doesn't do what I want */
    >{
    >
    >}
    >>

    >would be the same as:
    >>

    >int i = 0;
    >>

    >if(i == 0)
    >{
    >
    >}
    >

    Some people have suggested elaborate solutions like using an external
    interpreter. Aside from being way too much work,

    True.

    they're not going to
    work if you want the expression encoded in the string to refer to
    variables in your own program.

    False. Think about it. If you *had* to do it that way, couldn't you find a
    way to make it work? I know I could.

    You might be able to implement an expression evaluator that takes a
    string and evaluates it as an expression,

    Still way too much work, IMH In all probability the P doesn't actually
    need to do this thing that he is asking. It would be interesting to know
    the question /behind/ the question.

Re: Evaluate string expression in if statement?


max 4000 letters.
Your nickname that display:
In order to stop the spam: 4 + 3 =
QUESTION ON "C/C++"

EMSDN.COM