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

标签 c++ compiler-construction bison

我写了 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/

相关文章:

c++ - 将用户的 N 个元素添加到 vector 端线

c++ - Qt Creator 中 Qt Designer 中的 float IDE 窗口

c++ - 在没有 IDE 的情况下学习 C++

parsing - Bison语法错误处理

在语句结尾解析可选分号

c++ - 赋值是否等同于 std::atomic<bool> 的加载/存储

c++ - 在 VC++2010 环境之外运行时程序立即崩溃

PHP 执行权限被拒绝

optimization - 为什么这段代码是由 avr-gcc 生成的,它是如何工作的?

gcc - yylval 未定义,带有 flex 和 bison