help-bison
[Top][All Lists]
Advanced

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

Re: C++ / multiple instances / iostreams


From: Hans Aberg
Subject: Re: C++ / multiple instances / iostreams
Date: Tue, 20 Jul 2004 21:05:45 +0200

At 20:49 +0200 2004/07/19, Laurence Finston wrote:
>On Mon, 19 Jul 2004, Hans Aberg wrote:
>
>>
>> But you need only this skeleton file if you use a semantic type with
>> non-POD data members; otherwise the compiling the C output as C++ will
>> suffice. In particular, if you use the Bison %union feature, then you can
>> compile C as C++.
>>
>
>What's a POD?

>From the C++ standard (ISO/IEC 14882:1998):

[intro.object] 1.8:5 Unless it is a bit-field (9.6), a most derived object
shall have a non-zero size and shall occupy one or more
bytes of storage. Base class sub-objects may have zero size. An object of
POD 4) type (3.9) shall occupy
contiguous bytes of storage.

4) The acronym POD stands for "plain old data."

[basic.types] 3.9:10 Arithmetic types (3.9.1), enumeration types, pointer
types, and pointer to member types (3.9.2), and cv-qualified versions of
these types (3.9.3) are collectively called scalar types. Scalar types,
POD-struct types,
POD-union types (clause 9), arrays of such types and cv-qualified versions
of these types (3.9.3) are collec-tively called POD types.

>I hesitate to suggest anything without trying it out first, but generally
>speaking, I would define `YYSTYPE' to be a pointer if I was using
>a class type as my semantic value.  Even if using a type with non-trivial copy
>constructors was possible, calling a copy constructor is potentially
>expensive,
>whereas copying a pointer is not.

The idea of C++ is to take those running time expenses in favor of
programming convenience. If a pointer is replaced with say a class
maintaining a reference count, then one gets automatic cleanup, also in the
case of errors. Under C, the cleanup will have to be done by hand, or by
using the still experimental %destructor option.

>  My union looks like this:
>
>%union
>{
>  char string_value[64];
>  double real_value;
>  signed int int_value;
>  void* pointer_value;
>};
>
>I may revise my parser so that I just use the `void*' and don't
>bother with the other types.  I thought it would be more convenient this way,
>but it's not, really.

I use:

class semantic_type {
public:
  long number_;
  std::string text_;
  my::object object_;

  semantic_type() : number_(0) {}
};

#define YYSTYPE semantic_type

Here, my::object is the polymorphic type, which maintains a
reference-counted pointer object_root*. I loose some space, as this is not
a union.

>Hans' method of deriving all possible rule values from a common base class (if
>I've understood him correctly) will probably work well for a lot of
>applications.  In my application it wouldn't, but I've found that casting the
>objects to the appropriate types as needed isn't any trouble.

It will probably work in your case as well, if you were willing to take the
overhead it generates relative your method.

  Hans Aberg






reply via email to

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