2010-08-23 80 views
1

在我的解析器,我有问题的typedef与野牛和弯曲

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

现在,SYMTABLE是一个结构typedef的。 struct和typedef位于包含文件中。这没有问题。

QUAD是一个struct的typedef(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错误,但野牛生成的文件编译就好。我在我的扫描仪中包含了QUAD typedef和struct quad的头文件。

看来这是问题发生的唯一地方,所以我可以用struct quad替换QUAD,但这与SYMTABLE不一致。

回答

3

我test.l:

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

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

我test.y:

%{ 
#include "bla.h" 
%} 

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

%% 

input: 
| input; 

%% 

我bla.h:

typedef void *SYMTABLE; 
typedef void *QUAD; 

我的构建:

[email protected]: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 
[email protected]:pts/21:~/temp> flex test.l  
[email protected]:pts/21:~/temp> icc lex.yy.c -c 
[email protected]:pts/21:~/temp> 
+0

我知道这是一个错误在我的结尾。原来在我的扫描仪中,我在#include“quad.h”之前#include“parser.tab.h” 我认为parser.tab.h会包含来自quad.h的include,但它似乎不是。 – Kizaru 2010-08-24 00:28:30