473,574 Members | 2,355 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 13121

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*********@ho tmail.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_Keit h) 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.or g> 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*********@ho tmail.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
1580
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); while(sline=m_streamReader.ReadLine()) Debug.WriteLine(sline);
1
3089
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 context.nextNode().selectNodes(expression);
3
5676
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 know how to understand it, How to evaluate the expression as a void expression? explicit conversion the expression? but, befor the sentence...
22
29982
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 and various other C programming books but nowhere there is a mention on what a number in an if statement evaluates to. Is this some kind of big...
3
8015
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
33812
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 should return 0 or false( ( 3 + 6 ) / 3 ) > ( ( 5 + 3 ) / 4 ) --> this should return 1 or trueThanks for any comment or
0
2609
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 expression given by the user (in the form of a variable) does not evaluate when i give it under the if element in xslt. I have the sample code...
2
2153
by: parul.prasad | last post by:
How can we evaluate a string expression in if statement Linux C/C++
1
6534
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 static Stack operators = new Stack(); private static Stack operands = new Stack();
0
7826
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7741
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
6491
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5637
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5328
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3763
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3781
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2257
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1362
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.