473,412 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 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 13102

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bagger Vance | last post by:
The code below does not work -- but is there a way to do this? I want to both read a line and string sline; StreamReader m_streamReader = new StreamReader(fi); ...
1
by: David Laub | last post by:
I have no problems running the following dynamic XPath evaluator form MSXSL: <msxsl:script implements-prefix="dyn" language="jscript"> evaluate(context, expression) { return...
3
by: Jason luo | last post by:
Hi all, In c99-standard page 52,there is a sentence about void,as below: If an expression of any other type is evaluated as a void expression, its value or designator is discarded. I don't...
22
by: Paminu | last post by:
As I remember if(1) evaluates to true and all other numbers including 0 evaluate to false. But where do I find out about this for sure?? I have looked through K&R, all the C for dummies books...
3
by: Coco | last post by:
Hi! Does c# has any class method that will be able to evaluate a given string e.g ((True && False) || (True && True)) and return the result in boolean for this case is true Thanks!
13
by: Zeng | last post by:
Hello, Please help!!! I've been stuck on this issue for months. I just wonder if there is a way to programmatically evaluate expression strings such as ( ( 3 + 5 ) / 2 ) > 4 --> this...
0
by: SubasreeG | last post by:
Hi, I have a situation where expression to evaluate comes from the user. I have to execute the expression and based upon the result i have to perform certain operation. The problem is that the...
2
by: parul.prasad | last post by:
How can we evaluate a string expression in if statement Linux C/C++
1
by: aitia | last post by:
this the code. i used ECLIPSE to run this.. it has some codes smells that i can't seem to figure out.. can any one help? import java.io.*; import java.util.*; public class Postfix { private...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.