如何将头文件放入 .tab.h 在 Bison 中?
How to put header file to .tab.h in Bison?
我写了野牛代码头:
%{
#include "foo.h"
%}
并且我在头文件中定义了一个名为 'Foo' 的结构。我想在 Bison 中将其用作令牌类型。
%define api.value.type union
%token <Foo*> bar
然后我使用 -d
选项生成 bison.tab.h
文件。
bison -d bison.y
但是bison.tab.h
中没有#include foo.h
,它使用struct Foo来定义联合YYSTYPE。
//bison.tab.h
union YYSTPE {
Foo* bar;
...
};
编译此程序时出错:error: ‘Foo’ does not name a type
有没有办法在 bison.tab.h
或这种情况下的其他解决方案中包含头文件?
对于应该同时出现在 .c 和 .h 文件中的包含(在 %union
的定义之前),您应该使用 %code requires { ... }
。 %{ ... }
仅在 .c 文件中插入代码。
有关各种 %code
选项的更多信息,您可以查看 "Prologue Alternatives" chapter of the Bison docs。
我需要使用 2.3 bison 版本,它没有 %code 指令,所以我在编译程序时添加了一个命令,将我的包含插入到 bison 输出的顶部header[=11] =]
echo #include \"my_include.hpp\" | cat - ${BISON_HEADER_OUTPUT} > tmp && mv tmp ${BISON_HEADER_OUTPUT}
我写了野牛代码头:
%{
#include "foo.h"
%}
并且我在头文件中定义了一个名为 'Foo' 的结构。我想在 Bison 中将其用作令牌类型。
%define api.value.type union
%token <Foo*> bar
然后我使用 -d
选项生成 bison.tab.h
文件。
bison -d bison.y
但是bison.tab.h
中没有#include foo.h
,它使用struct Foo来定义联合YYSTYPE。
//bison.tab.h
union YYSTPE {
Foo* bar;
...
};
编译此程序时出错:error: ‘Foo’ does not name a type
有没有办法在 bison.tab.h
或这种情况下的其他解决方案中包含头文件?
对于应该同时出现在 .c 和 .h 文件中的包含(在 %union
的定义之前),您应该使用 %code requires { ... }
。 %{ ... }
仅在 .c 文件中插入代码。
有关各种 %code
选项的更多信息,您可以查看 "Prologue Alternatives" chapter of the Bison docs。
我需要使用 2.3 bison 版本,它没有 %code 指令,所以我在编译程序时添加了一个命令,将我的包含插入到 bison 输出的顶部header[=11] =]
echo #include \"my_include.hpp\" | cat - ${BISON_HEADER_OUTPUT} > tmp && mv tmp ${BISON_HEADER_OUTPUT}