Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undeclared yylex compiler error for C++ scanner with builtin main #421

Open
zmajeed opened this issue Apr 6, 2019 · 0 comments
Open

Undeclared yylex compiler error for C++ scanner with builtin main #421

zmajeed opened this issue Apr 6, 2019 · 0 comments

Comments

@zmajeed
Copy link
Contributor

zmajeed commented Apr 6, 2019

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant