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
If you use
--bison-bridge, then flex creates a scanner that expectsyylvalas a parameter, rather than a global, AND that parameter is aYYSTYPE *rather than aYYSTYPE. In order to make it work correctly you need to specify%define api.purein your bison source file (.y), so it will call yylex with the extra argument, rather than declaring (and expecting yylex to use) a globalyylvalSo you need to either get rid of the
--bison-bridgeargument (to use the normal, default, non-reentrant calling conventions between yylex and yyparse), OR you need to add%define api.pureto the .y file, and change your .l code to useyylval->instead ofyylval.everywhere.