我写了 Bison 代码头:
%{
#include "foo.h"
%}
然后我在 header 中定义了一个名为“Foo”的结构。我想将它用作 Bison 中的 token 类型。
%define api.value.type union
%token <Foo*> bar
然后我使用-d
选项生成bison.tab.h
文件。
bison -d bison.y
但是bison.tab.h
中没有#include foo.h
,它使用struct Foo来定义 union 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 .
关于c++ - 如何将头文件放入 Bison 中的 .tab.h?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52905235/