ahoffer
ahoffer

Reputation: 6546

Flex compiler error: request for member in something not a structure or union

I want to use Flex and Bison together. I have declared a union in the bison definition file that I will use in the lexer. Bison produces a .tab.h file that includes the union declaration (see below). I include this .tab.h file in the lexer definition, but the lexer action:

  yylval.stringptr = yytext;

causes a compiler error:

lexer.l: In function ‘yylex’:
lexer.l:190: error: request for member ‘stringptr’ in something not a structure or union

Here is a snippet of the .tab.h file:

#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
typedef union YYSTYPE
{

/* Line 1676 of yacc.c  */
#line 9 "parser.y"

  char * s;
  char * stringptr;
  double d;
  int i;



/* Line 1676 of yacc.c  */
#line 126 "parser.tab.h"
} YYSTYPE;
# define YYSTYPE_IS_TRIVIAL 1
# define yystype YYSTYPE /* obsolescent; will be withdrawn */
# define YYSTYPE_IS_DECLARED 1
#endif

extern YYSTYPE yylval;

Why is yylval not recognized as a structure or union? How should I correct the problem?

PS: I invoked Flex with --bison-bridge

Upvotes: 0

Views: 1375

Answers (1)

Chris Dodd
Chris Dodd

Reputation: 126408

If you use --bison-bridge, then flex creates a scanner that expects yylval as a parameter, rather than a global, AND that parameter is a YYSTYPE * rather than a YYSTYPE. In order to make it work correctly you need to specify %define api.pure in your bison source file (.y), so it will call yylex with the extra argument, rather than declaring (and expecting yylex to use) a global yylval

So you need to either get rid of the --bison-bridge argument (to use the normal, default, non-reentrant calling conventions between yylex and yyparse), OR you need to add %define api.pure to the .y file, and change your .l code to use yylval-> instead of yylval. everywhere.

Upvotes: 4

Related Questions