I’ve been spending a bunch of time writing some YACC grammars using Flex and Bison. Flex and Bison are GNU ports of Lex and Yacc respectively. These tools were originally designed to generate C code. However, I wanted to be able to use Bison to generate C++ code. Writing context-free grammars is easy; getting C and C++ code to interop seamlessly is not ;( Man, did I feel lots of pain. The purpose of this entry is to hopefully prevent other folks from feeling the same pain that I felt today.
The generally accepted practice is to let Flex generate C code, and to trick Bison into generating C++ code.[1] This means, however, that you now are mixing C and C++ code within the same application. To fix the inevitable compiler errors that will result from this unholy combination, you’ll need to tell your C++ compiler to turn off name mangling for certain symbols. This, of course, requires that you understand how Flex and Bison talk to each other.
Bison calls the Flex-generated yylex() function to obtain the next token from Flex. Since yylex() will be compiled in a “C” source file, you need to disable name mangling of this function. This is conveniently done using the extern “C” directive and declaring the yylex() function prototype in the header of your Bison file:
extern "C" {
int yylex( void );
}
In C++ code, you can only define a method once. The yyerror() method is called by Bison-generated code to report errors. Therefore, you must add a forward-reference to your yyerror() implementation that you’ll write at the bottom of your Bison source code file by adding:
int yyerror( const char * );
to your Bison file’s header.
To complicate matters, I wasn’t reading input from stdin and stdout; I was reading input from strings that are passed to my parser. This required me to call the yy_scan_string() and yy_delete_buffer() methods from my C++ code. Therefore, I needed to turn off name mangling for these symbols as well. Here’s all of the code that you’ll need to add to your Bison file’s header to turn off name mangling.
`typedef struct yy_buffer_state *YY_BUFFER_STATE;
extern “C” { int yylex( void ); YY_BUFFER_STATE yy_scan_string( const char * ); void yy_delete_buffer( YY_BUFFER_STATE ); }`
But wait, there’s more pain! In my Bison file, I need to use unions, since I would pass both token values and text values back to my Bison grammar. Here’s the union definition from my Bison file:
%union
{
int token;
char *string;
}
This union is manifested through a global variable called yylval. Once Bison is finished generating code, you’ll wind up with a declaration that looks like:
`typedef union { int token; char *string; } YYSTYPE;
YYSTYPE yylval;`
However, yylval is defined by Bison, not Flex. And the Bison-generated file is a C++ file, whereas the Flex-generated code is a C file. This means that you need to disable name mangling of yylval.
When Bison generates code, it does so by using a template file called bison.simple. Unfortunately, the version of bison.simple that I got from Wilbur Streett’s otherwise excellent Win32 port of Flex and Bison did not disable name mangling of yylval. Therefore, I needed to replace 117 of his file:
YYSTYPE yylval;
with some conditionally compiled code:
`#ifdef __cplusplus extern “C” YYSTYPE yylval;
else
YYSTYPE yylval;
endif`
With these fixes in place, I’m happy to report that I can finally use Bison to generate C++ code, and Flex to generate C code. More importantly, figuring out how to get the generated C++ and C code to talk to each other greatly deepened my understanding of how Flex and Bison work together to generate your parser.
[1] This is an excellent tutorial for Lex and Yacc. In particular, Section 5 is invaluable in telling you why you need to leave Lex as it is and to NOT use the -+ option.
1 archived reader comment
Sebastian Wain (2004-07-05)
Hi John!, it’s easier to work with C++ & Flex, look at the “Generating C++ scanners” section in the man page.
Regards Sebastian Wain – http://swain.webframe.org