Bison / Flex declare mess ... how should I include / declare where?

I use Bison and Flex to create a pair of reenter / analyzer, but I can’t wrap my head around where everything should be included and declared.

First of all, I use reentrant Flex, so I need to first pass the yyscan_t scanner yyscan_t to Bison by declaring %parse-param {yyscan_t scanner} , and then make Bison pass this Flex by declaring %lex-param {yyscan_t scanner} . But yyscan_t not declared by Bison, so I have to include the Flex scanner header file (which I named scanner.flex.h) in the Bison file. But since my Flex file includes my Bison header, and my Bison header now includes the Flex header, I get a circular set that randomly messes up the stuff!

And let me say that I want to add location tracking using %locations to my Bison file and %bison-locations to my Flex file. Now I need to change the declaration of my yyerror and my yylex (it seems I need to define yylex AGAIN, although it is defined in the Flex header, but I can’t include it, remember?) In my Bison file, to include the YYLTYPE pointer, But now what? It seems that the default YYLTYPE declaration is AFTER the prolog has been entered, and therefore I cannot use this default YYLTYPE in the declaration of my yyerror and my yylex.

I understand that there are many workarounds for these problems ... but how should you do it right? It completely eludes me, and it just leaves my head a mess ...

+7
source share
2 answers

At the top of your bison file, you need to send a declaration of these values. You can do this in a common header or in a code file before including flex and bison files. yyscan_t is just a typedef from void *, so I declared the parameters as void pointers to make it 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); 

Yyerror example

 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); } 
+4
source

Came to this, looking for something else.

The answer is that Bison issues an internal declaration for YYLTYPE after the% union specification. Therefore, putting function prototypes and other things in the prolog section after% union avoids the YYLTYPE declaration problem; this is normal, Bison allows more than one prolog section:

 %{ /* Prologue 1 */ %} %this %that %union {} %{ /* Prologue 2, YYLTYPE declared */ %} %% blah: talk | blah talk %% 
+1
source

All Articles