Connecting Tech Pros Worldwide Help | Site Map

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

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 04:37 PM
spiros
Guest
 
Posts: n/a
Default convert the content of a string to an expression to check its correctness

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.

  #2  
Old July 22nd, 2005, 04:37 PM
John Harrison
Guest
 
Posts: n/a
Default 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
  #3  
Old July 22nd, 2005, 04:37 PM
tom_usenet
Guest
 
Posts: n/a
Default 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
  #4  
Old July 22nd, 2005, 04:38 PM
JKop
Guest
 
Posts: n/a
Default 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
  #5  
Old July 22nd, 2005, 04:41 PM
spiros
Guest
 
Posts: n/a
Default 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.
  #6  
Old July 22nd, 2005, 04:41 PM
JKop
Guest
 
Posts: n/a
Default 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
  #7  
Old July 22nd, 2005, 04:41 PM
JKop
Guest
 
Posts: n/a
Default 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
  #8  
Old July 22nd, 2005, 04:42 PM
John Harrison
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.