I'm using Bison and Flex to make a reentrant scanner/parser pair but can't wrap my head around where everything is to be included and declared.
First of all, I'm using reentrant Flex so I need to pass the yyscan_t
scanner type first to Bison by declaring %parse-param {yyscan_t scanner}
and then make Bison pass this to Flex by declaring %lex-param {yyscan_t scanner}
. But yyscan_t
is not declared by Bison so I must include the Flex generated scanner header file (which I named scanner.flex.h) in my Bison file. But since my Flex file includes my Bison header, and my Bison header now includes the Flex header, I get circular includes which messes up stuff in unpredictable ways!
And let's say I want to add in location tracking with %locations
in my Bison file and %bison-locations
in my Flex file. Now I need to change the declaration of my yyerror and my yylex (it seems I have to define yylex AGAIN even though it's defined in the Flex generated header but I cannot include that, remember?) functions in my Bison file to include the YYLTYPE pointer. But what now? It seems that the default YYLTYPE declaration is placed AFTER the prologue is inserted and thus I cannot use this default YYLTYPE in the declaration of my yyerror and my yylex.
I realize that there are a lot of workarounds to these problems... but how are you supposed to do it properly? It totally escapes me and this just leaves my head a mess...
In the top of your bison file you need to forward declare these values. You can do this in a common header or in the code file before you include the flex and bison files. yyscan_t
is just a typedef of a void * so I declared the parameters as void pointers to get it to work.
CommonHeader.h
int yylex (union YYSTYPE * yyval_param, struct YYLTYPE * yylloc_param, void * yyscanner);
int yyerror(struct YYLTYPE * yylloc_param, void *scanner, const char *s);
Example of yyerror
int yyerror(struct YYLTYPE * yylloc_param, void *scanner, const char *s)
{
printf("*** Lexical Error %s %d.%d-%d.%d\n", s,
yylloc_param->first_line, yylloc_param->first_column,
yylloc_param->last_line, yylloc_param->last_column);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With