convert the content of a string to an expression to check its correctness

S

spiros

Hi,

suppose you have the class Class1 and the main program:

class Class1
{
public:
Class1();
~Class1();

private:
int a;
int b;
}

void main()
{
Class1 cl = Class1();

cl.a = 4;
cl.b = 6;

char str[] = "a>0 && b<5"
}

I want to check if the expression which is contained to the str[] is
true or false.
With other word i want to check the following:

if (a>0 && b<5)
{
// do something
}
else
{
// do something else
}

the problem is that i have the expression in a string, how can i
convert the content od the string to a expression and check if the
expression is TRUE or FALSE?

Thanks in advance for any help.
 
J

John Harrison

Hi,

suppose you have the class Class1 and the main program:

class Class1
{
public:
Class1();
~Class1();

private:
int a;
int b;
}

void main()
{
Class1 cl = Class1();

cl.a = 4;
cl.b = 6;

char str[] = "a>0 && b<5"
}

I want to check if the expression which is contained to the str[] is
true or false.
With other word i want to check the following:

if (a>0 && b<5)
{
// do something
}
else
{
// do something else
}

the problem is that i have the expression in a string, how can i
convert the content od the string to a expression and check if the
expression is TRUE or FALSE?

Thanks in advance for any help.

It's not easy. You need to write a parser. Whole books have been written
on this subject and its far too big a topic to be explained in a newsgroup.

Your best bet is to get hold of some sample code, study it carefully and
then adapt it to your particular needs. For instance you could get hold of
The C++ Programming Language (3rd edition) by Bjarne Stroustrup which has
a simple arithmetic expression parser in chapter 6.

john
 
T

tom_usenet

Hi,

suppose you have the class Class1 and the main program:

class Class1
{
public:
Class1();
~Class1();

private:
int a;
int b;
}

void main()
{
Class1 cl = Class1();

cl.a = 4;
cl.b = 6;

char str[] = "a>0 && b<5"
}

I want to check if the expression which is contained to the str[] is
true or false.
With other word i want to check the following:

if (a>0 && b<5)
{
// do something
}
else
{
// do something else
}

the problem is that i have the expression in a string, how can i
convert the content od the string to a expression and check if the
expression is TRUE or FALSE?

If you need to evaluate the expressions at runtime, you'll have to
write a parser, which for full C++ functionality will require many
thousands of lines of code (or you could use a library like
boost.Spirit to reduce this substantially). A far better solution is
to use a scripting language that supports this kind of thing - I think
Python is a popular choice. Python and C++ play together quite well,
see http://www.boost.org/libs/python/doc/index.html

Tom
 
J

JKop

spiros posted:


int main()
{
Class1 cl = Class1();

cl.a = 4;
cl.b = 6;

char str[] = "a>0 && b<5"
}

I want to check if the expression which is contained to the str[] is
true or false.
With other word i want to check the following:

if (a>0 && b<5)
{
// do something
}
else
{
// do something else
}

the problem is that i have the expression in a string, how can i
convert the content od the string to a expression and check if the
expression is TRUE or FALSE?


Are you taking input from the user?


-JKop
 
S

spiros

JKop said:
spiros posted:


int main()
{
Class1 cl = Class1();

cl.a = 4;
cl.b = 6;

char str[] = "a>0 && b<5"
}

I want to check if the expression which is contained to the str[] is
true or false.
With other word i want to check the following:

if (a>0 && b<5)
{
// do something
}
else
{
// do something else
}

the problem is that i have the expression in a string, how can i
convert the content od the string to a expression and check if the
expression is TRUE or FALSE?


Are you taking input from the user?


-JKop

No, i don't take any input from the user, the string is available at compile time.

Any idea?

Thanks,
Spiros.
 
J

JKop

spiros posted:
No, i don't take any input from the user, the string is available at
compile time.

Any idea?

Thanks,
Spiros.


Well if the string is available at compile time... then why
is it a string in the first place?! Why not an inline
function?

bool Stuff(int a, int b)
{
return a>0 && b<5;
}


-JKop
 
J

JKop

JKop posted:

Well if the string is available at compile time... then why
is it a string in the first place?! Why not an inline
function?

bool Stuff(int a, int b)
{
return a>0 && b<5;
}


-JKop


And if that ain't elaborate enough for you:

Have loads of these little functions:

bool Suff(int a, int b)
{
return a> 0 && b < 5;
}

bool Choc(int a, int b)
{
return (a - 5) > 2 ? b + 3 : b + 4;
}

bool Monk(int a, int b)
{
return a < -2 || b;
}


But then, in your actual calling code, you can refer to
them by the same name:

int main()
{
bool (*Current)(int,int) = Monk;

Current(5,6);

Current = Stuff;

Current(7,9);

Current = Choc;

Current(5,-2);

}



I just don't see what possible "problem" would make you
have to make strings out of them!


-JKop
 
J

John Harrison

I just don't see what possible "problem" would make you
have to make strings out of them!

OP doesn't know how to use pointers to member functions perhaps?

john
 

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

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top