[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: generic expression evaluation
From: |
Hans Aberg |
Subject: |
Re: generic expression evaluation |
Date: |
Sat, 28 Sep 2002 10:45:20 +0200 |
Reply-To: address@hidden
At 08:21 -0400 2002/09/27, Karthikeyan Ramnath wrote:
>I am pretty new to Lex and Yacc, two days to be precise.
So then you do not know this is the Bison list. Latest Bison & Flex are
available at:
Flex Beta (2.5.21+): ftp://ftp.uncg.edu/people/wlestes/
Bison beta: ftp://alpha.gnu.org/gnu/bison/ (bison-1.49x.tar.gz)
Also there is the
Help-flex mailing list
address@hidden
http://mail.gnu.org/mailman/listinfo/help-flex
and
http://mail.gnu.org/mailman/listinfo/help-bison
>What do i read up? Where can i
>find more useful information to get the job done quickly?
Both Flex and Bison have excellent manuals.
>Earlier I was faced with the problem of evaluating a logical
>expression, like
>((A < 10) & (A > 2)) & (NOT(B==3))
>I had implemented a rather simple parser and used a tree to
>evaluate this expression. But now the thing is that the fields,
>A,B,C etc.. are configurable, as is the entire expression. Each
>field is an object in C++ with specific datatype binding.
>Faced with this problem, I was told to look into Lex and Yacc
>for implementing a generic solution. But my lack of expertise
>is posing a serious problem.
>Could you kindly help me out? What do i read up? Where can i
>find more useful information to get the job done quickly?
You need to build a "closure", snippets of code that can be executed after
the parsing has been finished, if the values of the variables (A, B in you
example) are unknown at the time of the parsing.
Under C++, I build a polymorphic class hierarchy using a reference count --
see my recent post "Re: bison %union and C++" to this list. Then one might
classes like
class less : public virtual object_root {
data x, y;
public:
...
data evaluate() { return x < y; }
};
The Bison rule might look like:
%%
expression:
expression "<" expression { $$ = new less($1, $3); }
| ...
...
%%
This produces an object that later one can apply evaluate() to get the answer.
The above is only a rough outline of the technique. -- You will have to
adapt it to your conditions.
Hans Aberg