help-bison
[Top][All Lists]
Advanced

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

Re: %union, C++, symbol types


From: Hans Aberg
Subject: Re: %union, C++, symbol types
Date: Thu, 7 Jul 2005 12:59:32 +0200


On 6 Jul 2005, at 11:40, 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.

You do not need to use %union. I use a C++ polymorphic (virtual) hierarchy together with dynamic_cast; any eventual type errors will then be revealed at runtime. (In addition, I found it useful to add a Std::string and a integral type to my YYSTYPE, used only to forward token names and numbers to the Bison parser.)

You can also use static_cast, as dynamic_cast is slow (just as any dynamic allocation), if your class hierarchy aren't marked "virtual" in the derivation. Then, one would probably need Bison's static type checking.

This can easily be done, as soon Bison gets suitable constructs (like %typed, not requiring a union for implementation, and %code which admits proper code placement). But it cannot handle it right now.

Also try out latest Bison alpha <ftp://alpha.gnu.org/gnu/bison/ bison-2.0a.tar.gz>; it should be more reliable than the version you are using.

  Hans Aberg






reply via email to

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