如何将头文件放入Bison中的.tab.h?

syx*_*byi 4 c++ compiler-construction bison

我写了野牛代码头:

%{
#include "foo.h"
%}
Run Code Online (Sandbox Code Playgroud)

我在标头中定义了一个名为“ Foo”的结构。我想将其用作Bison中的令牌类型。

%define api.value.type union
%token <Foo*> bar
Run Code Online (Sandbox Code Playgroud)

然后我使用-d选项来生成bison.tab.h文件。

bison -d bison.y
Run Code Online (Sandbox Code Playgroud)

但是#include foo.hin 中没有bison.tab.h,它使用结构Foo定义联合YYSTYPE。

//bison.tab.h
union YYSTPE {
    Foo* bar;
    ...
};
Run Code Online (Sandbox Code Playgroud)

编译该程序时导致错误: error: ‘Foo’ does not name a type

有没有办法在bison.tab.h这种情况下包含头文件或其他解决方案?

sep*_*p2k 6

对于应该同时出现在.c和.h文件中的include(在定义之前%union),您应该使用%code requires { ... }%{ ... }仅在.c文件中插入代码。

有关各种%code选项的更多信息,您可以查看Bison docs“ Prologue Alternatives”一章