[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Newbie Dynamic Data Typing Help
From: |
Hans Aberg |
Subject: |
Re: Newbie Dynamic Data Typing Help |
Date: |
Wed, 5 Oct 2005 19:59:36 +0200 |
On 5 Oct 2005, at 18:54, Gill,Michael J wrote:
I'm a complete newbie to bison and language semantics in general. I'm
trying to extend the mfcalc.y example from the manual to allow
dynamically typed variables but am at a loss as to how to implement
them. I'm currently saving var names and values in the symbol.c
linked
list but with double vals only, I want to add char and char* types as
dynamic types. Any direction/help would be apprectiated.
If you write in C++, you can make a polymorphic class hierarchy,
i.e., a root class object, and classes derived from a that. As a
parser semantic type, one would use another class, maintaining a
pointer object*, which may be combined with a reference count, in
order to avoid unnecessary copying.
Now, if you program in C, you just translate this picture; vice
versa, the C++ constructs were developed in order to automate the
corresponding C constructs. C requires more programming by hand, but
is easier to make optimized work in. So in C, you might have a class
(in pseudocode)
enum type { DOUBLE, STRING, OTHER, ... };
struct data {
type type_;
union {
double double_;
char* string_;
void* other_;
...
}:
};
#define YYSTYPE data
Then use the type_ value to extract the right kind of data, making
sure that (de-)allocations takes place correctly, etc.
If you only need statically typed variables that can hold different
values, then use the Bison feature %union, and instead of the type
enum, use Bison statc type system to select the right union field.
Use %destructor to cleanup during error recovery.
Hans Aberg