471,599 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,599 software developers and data experts.

Evaluate string expression in if statement?

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.

Dec 7 '05 #1
8 12992

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?

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

-David

Dec 7 '05 #2
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.

--
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)
Dec 7 '05 #3

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.

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

Dec 7 '05 #4

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.

Dec 7 '05 #5
"No Such Luck" <no*********@hotmail.com> writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 7 '05 #6

"Keith Thompson" <ks***@mib.org> wrote
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?
Dec 7 '05 #7
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 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. :-)
--
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)
Dec 8 '05 #8
Keith Thompson said:
"No Such Luck" <no*********@hotmail.com> writes:
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, 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.

--
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)
Dec 8 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Bagger Vance | last post: by
22 posts views Thread by Paminu | last post: by
3 posts views Thread by Coco | last post: by
reply views Thread by SubasreeG | last post: by
2 posts views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.