Kizaru
Kizaru

Reputation: 2513

Typedef issue with bison and flex

In my parser, I have

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

Now, SYMTABLE is the typedef for a struct. The struct and typedef are in an included file. There are no issues with this.

QUAD is the typedef for a struct (typedef struct quad QUAD). The struct and typedef are in an included file.

There is no problem doing:

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

My lexer needs yylval, so in the declarations part I have

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

When I do

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

GCC complains

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

If I go back to my parser.y file and replace QUAD with struct quad in ONLY the yylval %union, the problem goes away. I want to say this is a silly typedef mistake, but the bison generated file compiles just fine. I have included the header file for my QUAD typedef and struct quad in my scanner.

It seems this is the only place where the issues occurs, so I could just replace QUAD with struct quad, but this is inconsistent with the SYMTABLE.

Upvotes: 1

Views: 2977

Answers (1)

hroptatyr
hroptatyr

Reputation: 4829

my test.l:

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

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

my test.y:

%{
#include "bla.h"
%}

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

%%

input:
| input;

%%

my bla.h:

typedef void *SYMTABLE;
typedef void *QUAD;

my build:

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> 

Upvotes: 4

Related Questions