Connecting Tech Pros Worldwide Forums | Help | Site Map

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

spiros
Guest
 
Posts: n/a
#1: Jul 22 '05
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.

John Harrison
Guest
 
Posts: n/a
#2: Jul 22 '05

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


On 18 Jul 2004 14:40:42 -0700, spiros <kantas@ceid.upatras.gr> wrote:
[color=blue]
> 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.[/color]

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
tom_usenet
Guest
 
Posts: n/a
#3: Jul 22 '05

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


On 18 Jul 2004 14:40:42 -0700, kantas@ceid.upatras.gr (spiros) wrote:
[color=blue]
>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?[/color]

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
JKop
Guest
 
Posts: n/a
#4: Jul 22 '05

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


spiros posted:


int main()[color=blue]
> {
> 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[/color]
the str[] is[color=blue]
> 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,[/color]
how can i[color=blue]
> convert the content od the string to a expression and[/color]
check if the[color=blue]
> expression is TRUE or FALSE?[/color]


Are you taking input from the user?


-JKop
spiros
Guest
 
Posts: n/a
#5: Jul 22 '05

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


JKop <NULL@NULL.NULL> wrote in message news:<ZiPKc.4939$Z14.6221@news.indigo.ie>...[color=blue]
> spiros posted:
>
>
> int main()[color=green]
> > {
> > 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[/color]
> the str[] is[color=green]
> > 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,[/color]
> how can i[color=green]
> > convert the content od the string to a expression and[/color]
> check if the[color=green]
> > expression is TRUE or FALSE?[/color]
>
>
> Are you taking input from the user?
>
>
> -JKop[/color]

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

Any idea?

Thanks,
Spiros.
JKop
Guest
 
Posts: n/a
#6: Jul 22 '05

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


spiros posted:
[color=blue]
> No, i don't take any input from the user, the string is[/color]
available at[color=blue]
> compile time.
>
> Any idea?
>
> Thanks,
> Spiros.[/color]


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
JKop
Guest
 
Posts: n/a
#7: Jul 22 '05

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


JKop posted:

[color=blue][color=green]
>> No, i don't take any input from the user, the string is[/color][/color]
available at[color=blue][color=green]
>> compile time.
>>
>> Any idea?
>>
>> Thanks,
>> Spiros.[/color]
>
>
> Well if the string is available at compile time... then[/color]
why[color=blue]
> 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[/color]


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
John Harrison
Guest
 
Posts: n/a
#8: Jul 22 '05

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


On Tue, 20 Jul 2004 19:52:52 GMT, JKop <NULL@NULL.NULL> wrote:
[color=blue]
>
>
> I just don't see what possible "problem" would make you
> have to make strings out of them!
>
>[/color]

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

john
Closed Thread