help-bison
[Top][All Lists]
Advanced

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

Using Standard C++ library with Bison .y files (or defining tokens with


From: Michael Suodenjoki
Subject: Using Standard C++ library with Bison .y files (or defining tokens with #define)...
Date: Tue, 26 Mar 2002 20:30:50 +0100

Hi,

I'm one of those C++ developers that's trying to use Bison to integrate
a parser into a C++ project. I'm using
MS Visual C++ 7.0 with the Dinkum Standard C++ Library 3.10.

I've found that the way Bison is defining its token's using #defines is
not clever - it may interfere with the use of Standard C++ libraries (or
other libraries for that matter).

The following scenario illustrates the problem. Given the following .y
file:

    %{

    #include <sstream>    /* May also be any other include file using a
'eq' symbol for something */
    ...

    %}

    %token eq
    ...

    %%

This will when Bison have been used produce a file with something like
the following:

    ...
    #define eq 257
    ...

    #include <sstream> 
    ...

This will in my version of the Standard C++ library FAIL because the
'eq' is used inside the library (in my case in <iosfwd>, which have been
included through <sstream>). The define simply overwrites any other eq
definitions thus typically resulting in a compiler error.

As I can see it there are two solutions:

1. Don't define token symbols that Standard C++ or any other include
file may use. This may be a huge amount,
and it is nearly impossible to know on beforehand. I know that the
normal convention is to use uppercase
tokens like EQ, however in principle these may be used elsewhere too.

2. Change Bison so that it doesn't produces #defines but uses constants,
e.g. like "const int eq = 257;"

I like of course solution 2 best because it is the most clean one - at
least when dealing with C++ in which
case #defines should generally be avoided - see e.g. the very first item
on pp.13 in Scott Meyers book 
"Effective C++, Second Edition".

Optionally or for the sake of backward compatibility Bison could be
extended with an extra option
that tells it to use constants in stead of defines.

Best regards,
/Michael




reply via email to

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