Evaluate string expression in if statement?

N

No Such Luck

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.
 
R

Richard Heathfield

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.
 
D

David Resnick

No said:
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?

<OT> consider using a scripting language, eg. perl
and the eval command </OT>

-David
 
A

Andrew Poelstra

Richard said:
No Such Luck said:


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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

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

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.

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.
 
I

Ingo Menger

Richard said:
No Such Luck said:


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.
 
K

Keith Thompson

No Such Luck said:
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.
 
M

Malcolm

Keith Thompson said:
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.


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?
 
R

Richard Heathfield

Andrew Poelstra said:
You can't do this natively in C. Also, you can't use the system command
properly and cross-platform because of OS 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. :)
 
R

Richard Heathfield

Keith Thompson said:
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, IMHO. In all probability the OP doesn't actually
need to do this thing that he is asking. It would be interesting to know
the question /behind/ the question.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top