help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: %union, C++, symbol types


From: Tim Van Holder
Subject: Re: %union, C++, symbol types
Date: Thu, 07 Jul 2005 08:33:18 +0200
User-agent: Mozilla Thunderbird 1.0.2 (Windows/20050317)

Evan Lavelle wrote:
> I'd like to pass a token class from Flex to Bison (ideally, a
> tr1::shared_ptr - a reference counted smart pointer class). However, in
> Bison itself I need to deal with a number of symbol types - tokens, AST
> nodes, and so on.
> 
> This means that I need to use a %union, but this is difficult because
> the classes are too complex to put in a union. Using class pointers in
> the union is difficult, because this messes up the reference counting,
> and I have to do manual resource management.

Hmm = I tend to use a pointer-to-AST-node as main YYSTYPE, occasionally
adding a char or integer field if needed (and it rarely is).  Given that
yylval is purely used to pass a token's value from flex to bison,
reference counting seems fairly pointless (as long as you take care not
to silently discard token values, but cases where flex constructs a
value and bison ignores it completely should typically be rare).

Of course, you could just make a class/struct MyTokenValue that has
various shared_ptr members, and make YYSTYPE a shard_ptr to objects
of that class - then there's no unions to worry about, and when a
MyTokenValue object dies, its shared_ptr members should ensure that
actual values die with it if needed.  It will use more memory than a
union, but will have the added flexibility of allowing multiple value
types at once (e.g. perhaps letting a token have both a string value and
an AST node value).

> One fix might be to split up the Flex and Bison symbol types - is this
> possible? In other words, Flex and Bison communicate using only one
> type, and Bison has its own multiple types for terminals, non-terminals,
> and so on (it seems strange that F/B share a single YYSTYPE - why is this?)

YYSTYPE is the type for values of terminals and nonterminals alike; so
since flex supplies the values of tokens, it makes sense that it returns
a value of type YYSTYPE (note that terminals can have multiple value
types too!).




reply via email to

[Prev in Thread] Current Thread [Next in Thread]