c - bison 和 flex 的 Typedef 问题

标签 c data-structures typedef bison flex-lexer

在我的解析器中,我有

%union {
  SYMTABLE *p_entry ;
  QUAD *p_quad ; 
} ;

现在,SYMTABLE 是结构的类型定义。 struct 和 typedef 在包含的文件中。这没有问题。

QUAD 是结构的类型定义 (typedef struct quad QUAD)。 struct 和 typedef 在包含的文件中。

做没有问题:

bison -d parser.y
gcc parser.tab.c -c

我的词法分析器需要 yylval,所以在声明部分我有

#include "parser.tab.h" /* bison generated header file */
extern YYSTYPE yylval ;

当我这样做

flex scanner.lex
gcc lex.yy.c -c

GCC 提示

In file included from scanner.lex:16:
parser.y:30: error: syntax error before "QUAD"
parser.y:30: warning: no semicolon at end of struct or union
parser.y:32: error: syntax error before '}' token
parser.y:32: warning: data definition has no type or storage class
parser.tab.h:121: error: syntax error before "yylval"
parser.tab.h:121: warning: data definition has no type or storage class

如果我返回我的 parser.y 文件并仅在 yylval %union 中将 QUAD 替换为 struct quad,问题就会消失。我想说这是一个愚蠢的 typedef 错误,但是 bison 生成的文件编译得很好。我在我的扫描仪中包含了我的 QUAD typedef 和 struct quad 的头文件。

似乎这是唯一出现问题的地方,所以我可以用 struct quad 替换 QUAD,但这与 SYMTABLE 不一致。

最佳答案

我的测试.l:

%{
#include "bla.h"
#include "test.tab.h" /* bison generated header file */
extern YYSTYPE yylval ;
%}

%%
\n      printf("nl");
.       printf("c");
%%

我的测试.y:

%{
#include "bla.h"
%}

%union {
        SYMTABLE *p_entry ;
        QUAD *p_quad ; 
};

%%

input:
| input;

%%

我的 bla.h:

typedef void *SYMTABLE;
typedef void *QUAD;

我的构建:

freundt@segen:pts/21:~/temp> bison -d test.y
test.y: conflicts: 1 shift/reduce
test.y:13.3-7: warning: rule useless in parser due to conflicts: input: input
freundt@segen:pts/21:~/temp> flex test.l    
freundt@segen:pts/21:~/temp> icc lex.yy.c -c
freundt@segen:pts/21:~/temp> 

关于c - bison 和 flex 的 Typedef 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3552346/

相关文章:

java - 贝塞尔低通算法

algorithm - 如何从二进制堆中删除元素?

c++ - 具有节点的树可以是 3 种不同的类型(字符串、整数或 float )

c - 如何使用包含结构的 union ?

c++ - C/C++ 下划线 t/type (_t/_type) 和类名?

c - 如何在线性时间内对 int 数组进行排序?

c - 如何禁用 gcc 警告 "cc1: warning: command line option ‘-std=c++11’ 对 C++/ObjC++ 有效但对 C 无效 [默认启用]”

c - C 中的链表操作(段错误核心转储!)

c++ - typedef std::runtime_error MyError 与类 MyError:public std::runtime_error

c - 带元组的 typedef 如何工作?