From: Gregor Kova? on
Hi!

Is there a way to convert a plain string to an expression ?
Let's say that I have a
int a = 1;
int b = 2;
std::string str = "a + b";

How would I evaluate the expression in str ?

Best regards,
Kovi

--
----------- Gregor Kovac -----------------------------------
In A World Without Fences Who Needs Gates? Experience Linux.
From: Richard Heathfield on
Gregor Kova? said:

> Hi!
>
> Is there a way to convert a plain string to an expression ?
> Let's say that I have a
> int a = 1;
> int b = 2;
> std::string str = "a + b";
>
> How would I evaluate the expression in str ?

"Compilers: Principles, Techniques, and Tools", by Aho, Sethi and Ullman
("The Dragon Book").

--
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)
From: James Dennett on
Richard Heathfield wrote:
> Gregor Kova? said:
>
>> Hi!
>>
>> Is there a way to convert a plain string to an expression ?
>> Let's say that I have a
>> int a = 1;
>> int b = 2;
>> std::string str = "a + b";
>>
>> How would I evaluate the expression in str ?
>
> "Compilers: Principles, Techniques, and Tools", by Aho, Sethi and Ullman
> ("The Dragon Book").

To elaborate a little: in C++ (as in C), variable names do
not (in principle) play any part after code is compiled
("translated"). As far as a compiler is concerned, your
code above is the same as

int var1 = 1;
int var2 = 2;
std::string var3 = "a + b";

From this it should be clear that there's no way to
evaluate (at runtime) an expression stored in a string
which refers to (compile-time) variables.

What you need to do is to define your own run-time place
to store variables, and to write a parser for the set of
expressions you want to be able to evaluate.

You can either do this yourself, or find an embed a
ready-made solution; Tcl is particularly easy to embed,
but using Python could be reasonable, or even a more
specialized expression language such as XPath.

Or you could just use a language designed as a scripting
language directly, rather than trying to reinvent the
wheel in C++. (Most of them are written in C or C++
anyway.)

-- James
From: Mike Wahler on

"Gregor Kovac" <gregor.kovac(a)mikropis.si> wrote in message
news:jS8Rg.5018$oj5.1955944(a)news.siol.net...
> Hi!
>
> Is there a way to convert a plain string to an expression ?

If you write code that does so.

> Let's say that I have a
> int a = 1;
> int b = 2;
> std::string str = "a + b";
>
> How would I evaluate the expression in str ?

First note that the 'a' and 'b' in the string have
no relation at all to the object names 'a' and 'b'
which you've defined. Once your program is compiled,
those names go away.

However you can create code that gives particular meanings
to 'a' and 'b' (and the '+' character) in your string.
E.g.:

#include <iostream>
#include <sstream>
#include <string>

int main()
{
int a = 1;
int b = 2;
std::string s = "a + b";
std::istringstream iss(s);
std::string left;
std::string op;
std::string right;
iss >> left >> op >> right;

if(left == "a" && op == "+" && right == "b")
{
std::cout << a << ' ' << op << ' ' << b
<< " = " << a + b << '\n';
}
else
{
std::cout << "I don't know what that means.\n";
}
return 0;
}

Of course to make this useful, you'll need a more generic
approach (e.g. make a table which relates your object names
to names stored in a string).

-Mike


From: Bart on
Richard Heathfield wrote:
> Gregor Kova? said:
>
> > Hi!
> >
> > Is there a way to convert a plain string to an expression ?
> > Let's say that I have a
> > int a = 1;
> > int b = 2;
> > std::string str = "a + b";
> >
> > How would I evaluate the expression in str ?
>
> "Compilers: Principles, Techniques, and Tools", by Aho, Sethi and Ullman
> ("The Dragon Book").

Or google for "yacc" or "bison".

Regards,
Bart.

 |  Next  |  Last
Pages: 1 2
Prev: stdio.lib
Next: structures and pointer