Search code examples
flex-lexer

How to use yyextra in C++?


I would like to access an external struct, within the scanner rules. When using C language, yyextra is provided as solution for this. Apparently yyextra is not available when generating a C++ scanner. What's the proper solution to achieve the same?


Solution

  • You can derive your own lexer class from yyFlexLexer and add whatever you want to it.

    If you do this, you will probably want to tell Flex the name of your class:

    %option yyclass="myLexer"
    

    See the description of this option:

    ‘--yyclass=NAME, %option yyclass="NAME"’
    …informs flex that you have derived NAME as a subclass of yyFlexLexer, so flex will place your actions in the member function NAME::yylex() instead of yyFlexLexer::yylex().…

    and the note about the yylex() member function:

    virtual int yylex()
    performs the same role is yylex() does for ordinary flex scanners: it scans the input stream, consuming tokens, until a rule’s action returns a value. If you derive a subclass S from yyFlexLexer and want to access the member functions and variables of S inside yylex(), then you need to use %option yyclass="S" to inform flex that you will be using that subclass instead of yyFlexLexer. In this case, rather than generating yyFlexLexer::yylex(), flex generates S::yylex() (and also generates a dummy yyFlexLexer::yylex() that calls yyFlexLexer::LexerError() if called).