flex
flex copied to clipboard
Undeclared yylex compiler error for C++ scanner with builtin main
This minimal flex file with C++ and main options causes a compiler error
/* cxxmain.l */
/*
a minimal C++ flex file that gives an undeclared yylex compiler error
run with: flex cxxmain.l && g++ lex.yy.cc
error is:
lex.yy.cc: In function 'int main()':
lex.yy.cc:1572:2: error: 'yylex' was not declared in this scope
*/
%option c++
%option main
%%
$ flex cxxmain.l && g++ lex.yy.cc
lex.yy.cc: In function 'int main()':
lex.yy.cc:1572:2: error: 'yylex' was not declared in this scope
flex and gcc versions
$ flex --version
flex 2.6.4
$ g++ --version
g++ (GCC) 8.2.0
The issue is simple. The generated main in lex.yy.cc calls the free function yylex instead of the yyFlexLexer::yylex method.
// lex.yy.cc
int main ()
{
yylex();
return 0;
}
The reason is flex.skl does not handle the case for a C++ builtin main
%if-not-reentrant
yylex();
%endif
Here's a proposed fix. I'll submit a PR if it's acceptable.
%if-not-reentrant
%if-c-only
yylex();
%endif
%endif
%if-c++-only
yyFlexLexer l;
l.yylex();
%endif