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?
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 derivedNAME
as a subclass ofyyFlexLexer
, so flex will place your actions in the member functionNAME::yylex()
instead ofyyFlexLexer::yylex()
.…
and the note about the yylex()
member function:
virtual int yylex()
performs the same role isyylex()
does for ordinary flex scanners: it scans the input stream, consuming tokens, until a rule’s action returns a value. If you derive a subclassS
fromyyFlexLexer
and want to access the member functions and variables ofS
insideyylex()
, then you need to use%option yyclass="S"
to inform flex that you will be using that subclass instead ofyyFlexLexer
. In this case, rather than generatingyyFlexLexer::yylex()
, flex generatesS::yylex()
(and also generates a dummyyyFlexLexer::yylex()
that callsyyFlexLexer::LexerError()
if called).