-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi there,
I have studied your bison manual because I wanted to write a flex+bison parser in C++, using the "Complete C++ Example" there as a guide. http://www.gnu.org/software/bison/manual/html_node/index.html#Top I want to have this as part of an (open source) library that will contain multiple parsers which might run in parallel and multiple instances of the same parser might run in parallel. I was not able to achieve this, although I'm a graduate computer scientist. The issue is that in your example, the scanner is a C-Style scanner and it is very hard to find anything about connecting a bison C++ parser to a flex C++ scanner (which is much harder than it sounds). You see, a C-Style scanner can have arguments to the yylex function (by using #define YY_DECL in the .l file or by using %option bison-bridge) so you can pass a pointer to an object where yylex can store the yylval. But when you create a C++ scanner with flex, you get something like #define yyFlexLexer MyClassName #include<FlexLexer.h> so this creates a class definition for MyClassName derived from FlexLexer, because FlexLexer.h basically looks like this #ifndef __FLEX_LEXER_H #define __FLEX_LEXER_H class FlexLexer ... #endif class yyFlexLexer : public FlexLexer ... The problem now is that in FlexLexer.h the yylex method has signature int yylex(), so IT IS NOT POSSIBLE to pass a pointer to some yylval object and if you do something like #define YY_DECL int yylex(arguments) then the C++ compiler will not compile the code and report that the method yylex does not match any definition in MyClassName. %option bison-bridge does not work together with %option c++. Using a global yylval is out of the question because I want to be able to run multiple instances of the same parser in parallel. Solving this problem is much harder than it sounds. I have been searching for an answer for 3 days now and I have finally found this example: http://www.thebinaryidiot.com/archives/2010/03/06/flex-bison-cpp-example/ The solution is to MANUALLY derive a class from FlexLexer, give it a private yylval ATTRIBUTE and add a method int yylex(argument) { yylval=argument; return yylex(); } Now you let flex generate the code for THIS yylval() by specifying %option yyclass="MyDerivedClass" in the .l file. If the official bison manual claims to provide a C++ example, then IMHO it should provide a real C++ example and not a C++ parser connected to a C Scanner, unless it can easily be replaced with a C++ Scanner. Kind regards Viktor Engelmann -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) Comment: Using GnuPG with undefined - http://www.enigmail.net/ iQEcBAEBAgAGBQJQq0u8AAoJEFNPHqh3rhUT11sH/0yuNh52bfSt1obF8OQRLKGM UeD67nigLu4eGxjqfOzYpy0OAS+sTnzJ2bQWPVr6bhu3I0QH8I4e6OD+0+lSKnWb PllgQKoMo+KvLQ0jjvGscBvDRZiDL+7DylClORgi78HwkxmIO1s6axpSp8x6aUug qgpbiUsL/nnqwI8NvfAZq9sH4AjzbkfNCQp9AgXjt2h8T8ah2jgnh8p7/HISIaU5 P/wHiOf5A3Uz7Hq0Ky7B8v7Np1PKBNTLsPhKeSk8lQGKFpytcldEAv2z6+TI1/W7 2gAaJB59Wd8xsnJympWs6DJCnTEYQ0afDbfDs9vhqAIbxbeo5uAuRAg3b6sUJVY= =/WnU -----END PGP SIGNATURE-----
