Search code examples
parsinggrammarbisonglr

Bison C++ GLR parser using variants


I am currently creating a parser using bison, which uses the variant feature heavily, since my grammar is not LALR(1) I want to use the GLR option. When I try to do so I get the following error:

error: '"glr.cc"' does not support '%define api.value.type variant'
 %define api.value.type variant
         ^^^^^^^^^^^^^^

What am I doing wrong?


Solution

  • Note: The answer below was valid when written and for the subsequent four and a half years. However, in Bison v3.8 (released September 7, 2021), a new experimental C++ GLR implementation was included which does support variant semantic types. You can test this skeleton, if you have updated your bison installation to version 3.8, by adding the directive %skeleton "glr2.cc". The Changelog indicates:

    It will eventually replace "glr.cc". However we need user feedback on this skeleton. Please report your results and comments about it.

    To me, this suggests that it should not yet be used in production code, but undoubtedly this warning will become invalid sometime in the next four years. In the meantime, use your own judgement or read the answer below from 2017.


    You are trying to build a GLR parser using the C++ API with a semantic type which is not POD, and that is not supported by the current C++ Bison GLR implementation.

    In particular, the variant type used by Bison's C++ API is not POD, and so it cannot be used in a GLR parser, as the error message states.

    The only workaround I know of is to use C-style discriminated unions with a tag field and a union.