[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Bison C++ Parser with Flex C++ Scanner

From: Viktor Engelmann
Subject: Bison C++ Parser with Flex C++ Scanner
Date: Tue, 20 Nov 2012 10:22:10 +0100
User-agent: Mozilla/5.0 (X11; Linux i686; rv:16.0) Gecko/20121028 Thunderbird/16.0.2

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

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

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


   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


The solution is to MANUALLY derive a class from FlexLexer, give it a
private yylval ATTRIBUTE and add a method
    int yylex(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++

Kind regards

Viktor Engelmann
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with undefined - http://www.enigmail.net/


reply via email to

[Prev in Thread] Current Thread [Next in Thread]