Search code examples
bisonincompatibility

How to handle bison version differences in one .y-file?


I have to support systems which come with bison version 3.0.4 and 3.4.2.

I used the variable parser_class_name in my .y-file. This one is not supported anymore with 3.4.2.

error: syntax error, unexpected string, expecting identifier

It's replacement is to use api.parser.class, this however doesn't work with 3.0.4: (see the changelog: History: Introduced in Bison 3.3 to replace parser_class_name.)

How do I fix this problem within the .y-file? If there something like %if version < 3.3 in bison's syntax?


Solution

  • There's no solution which can be implemented within the grammar file. Sorry.

    Bison does not provide any facility for conditional inclusion.

    It does provide the %require declaration, which declares a minimum bison version. That's probably not much use on this case. (Also, it doesn't allow specifying a range of versions, so you cannot restrict a grammar to an obsolete bison version.)

    As far as I know, the usual strategy is to also include the generated parser in the distribution, which allows your project to be compiled on a system with an incompatible bison version or even without bison at all. If, for whatever reason, the final user wishes to regenerate the parser, it is their responsibility to acquire the correct version, which of course should be documented in the build instructions.

    You could preprocess the grammar file based on bison version. Autotools can help you with that, if you're using them. But that's probably overkill.