Search code examples
ubuntuflex-lexeryacclex

why ubuntu showing error while running lex program


I am trying to run a lexical program in UBUNTU with .l file extension, I already installed flex and bison, and I was able to get lex.yy.c file, but when I give command cc lex.yy.c -lfd or cc lex.yy.c the terminal showed an error:

first.l:2:10: fatal error: iostream: No such file or directory
 #include <iostream>
          ^~~~~~~~~~

my code is:

%{
#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();
}

I reinstall flex and bison ans also uninstall and install gcc, but no change! Any help will be greatly appreciated,


Solution

  • Here is the correct code to run it:

    %%
    [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();
    }