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

Discussion in 'C++' started by spiros, Jul 18, 2004.

  1. spiros

    spiros Guest

    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.
    spiros, Jul 18, 2004
    #1
    1. Advertising

  2. On 18 Jul 2004 14:40:42 -0700, spiros <> wrote:

    > 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
    John Harrison, Jul 18, 2004
    #2
    1. Advertising

  3. spiros

    tom_usenet Guest

    On 18 Jul 2004 14:40:42 -0700, (spiros) wrote:

    >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
    tom_usenet, Jul 19, 2004
    #3
  4. spiros

    JKop Guest

    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
    JKop, Jul 19, 2004
    #4
  5. spiros

    spiros Guest

    JKop <> wrote in message news:<ZiPKc.4939$>...
    > 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.
    spiros, Jul 20, 2004
    #5
  6. spiros

    JKop Guest

    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
    JKop, Jul 20, 2004
    #6
  7. spiros

    JKop Guest

    JKop 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



    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
    JKop, Jul 20, 2004
    #7
  8. On Tue, 20 Jul 2004 19:52:52 GMT, JKop <> wrote:

    >
    >
    > 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
    John Harrison, Jul 20, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthias Kaeppler

    const-correctness and lambda expression

    Matthias Kaeppler, Apr 16, 2005, in forum: C++
    Replies:
    1
    Views:
    596
    Kanenas
    Apr 20, 2005
  2. Apple
    Replies:
    3
    Views:
    292
    Apple
    Aug 1, 2005
  3. thunk
    Replies:
    1
    Views:
    292
    thunk
    Mar 30, 2010
  4. thunk
    Replies:
    0
    Views:
    453
    thunk
    Apr 1, 2010
  5. thunk
    Replies:
    14
    Views:
    599
    thunk
    Apr 3, 2010
Loading...

Share This Page