g++ flex 和 bison ‘yylex’ 未在此范围内声明

g++ flex and bison ‘yylex’ was not declared in this scope

我是 flex 和 bison 的新手。
请在我编译我的代码时提供帮助‘yylex’ was not declared in this scope
这是我的 sample.ll 文件:

%{
#include <iostream>
using namespace std;
%}
%%
[ \t\n]         /*do nothing*/
("//")(.)*      { cout << "comments" << endl; }
"("         { cout << "start (" << endl; }
")"         { cout << "end )" << endl; }
"+"         { cout << "+ detected" << endl; }
"-"         { cout << "- detected" << endl; }
"/"         { cout << "/ detected" << endl; }
"*"         { cout << "* detected" << endl; }
"="         { cout << "= detected" << endl; }
"=="            { cout << "==" << endl; }
"<"         { cout << "<" << endl; }
"<="            { cout << "<=" << endl; }
">"         { cout << ">" << endl; }
">="            { cout << ">=" << endl; }
"!="            { cout << "!=" << endl; }
("\"")([a-zA-Z0-0\ ]*)("\"")    { cout << "string : " << yytext << endl; }
[0-9]+          { cout << "int = " << yytext << endl; }
([0-9]+)(".")([0-9]+)   { cout << "double = " << yytext << endl; }
[a-zA-Z][a-zA-Z0-9]*    { cout << "a varible or error : " << yytext << endl; }
"."         { cout << ". detected" << endl; }
.           { cout << "unknown : " << yytext <<  endl; }
%%
int main(int argc, char** argv) {
    // lex through the input:
    yylex();
}  

我的 sample.y 文件是:

%start init
%token NUMBER
%{
#include <iostream>
using namespace std;
%}
%%
init : exp
;
exp : term
| exp '+' term { cout << "+"; }
| exp '-' term { cout << "-"; }
;
term : factor
| term '*' factor { cout << "*"; }
| term '/' factor { cout << "/"; }
;
factor : NUMBER { cout << ; }
| '('exp')'
;
%%
#include "lex.yy.cc"

我用 flex -+ sample.l 编译 .l 文件,用 bison -L c++ sample.y 编译 .y 文件,当我 运行 g++ sample.tab.cc 我得到那个错误
我尝试了 int yylex();void yyerror(char const*); 来修复,但我遇到了另一个错误 error: too many arguments to function ‘int yylex()
请帮助我

如果要使用 C++ 模板,则需要使用完全不同的接口。这些在 Flex and Bison 手册中有描述,您的第一步将是通读该手册并相应地调整您的代码。

如果你只是想使用 C++(因为,显然,你写 cout << s << endl; 比写 printf("%s\n", s); 更舒服),你可以删除要求使用 C++ 模板的选项(-+-L C++)。然后你会得到熟悉的界面,包括yylex。您需要明确指定输出文件的文件名(例如 -o calc.lex.cc-o calc.tab.cc)。 C 模板可以使用 C++ 编译器进行编译,但您只能使用 POD 作为语义类型。