在 Bison 中启用变体

Enabling variants in Bison

我无法让变体在 bison 中工作。我输入

%{
  ... 
%}

%define api.value.type variant
...
%%
...

但是我明白了

bison -d -t -o parser.cpp parser.ypp
parser.ypp:16.9-22: error: '"yacc.c"' does not support '%define api.value.type variant'
%define api.value.type variant
        ^^^^^^^^^^^^^^

我的版本应该支持变体。

bison --version
bison (GNU Bison) 3.0.2
Written by Robert Corbett and Richard Stallman.

Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO 
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

当错误消息提到 "yacc.c" 时,它指的是 C 语言的 "yacc" 框架。变体是 C++ 的一个特性。使用 %language "c++" 将语言设置为 C++ 即可。