我正在尝试使用.l
文件扩展名在UBUNTU中运行词法程序,我已经安装了flex和bison,并且我能够获得lex.yy.c文件,但是当我发出命令cc lex.yy.c -lfd
或cc lex.yy.c
时,终端显示错误:
first.l:2:10: fatal error: iostream: No such file or directory
#include <iostream>
^~~~~~~~~~
我的代码是:
%{
#include <iostream>
using namespace std;
#define YY_DECL extern "C" int yylex()
%}
%%
[ \t\n] ;
[0-9]+\.[0-9]+ { cout << "Found a floating-point number:" << yytext << endl; }
[0-9]+ { cout << "Found an integer:" << yytext << endl; }
[a-zA-Z0-9]+ { cout << "Found a string: " << yytext << endl; }
%%
int main(int, char**) {
// lex through the input:
yylex();
}
我重新安装flex和bison ans也卸载并安装gcc,但没有变化!任何帮助将不胜感激,
您正在尝试使用C编译器编译C ++代码。请改用g++
。
以下是运行它的正确代码:
%%
[0-9]+\.[0-9]* { printf("Found a floating-point number:"); }
[0-9]* { printf("Found an integer:"); }
[a-zA-Z0-9]* { printf("Found a string: "); }
%%
main(int argc, char** argv)
{
yylex();
}