2

I have my lex file here.

%{
#include <stdio.h>
%}

%%
stop    printf("Stop command received\n");
start   printf("Start command received\n");
%%

I compiled it using flex. But there's an error when I compile the lex.yy.c using gcc. It says..

: undefined reference to yywrap

I used gcc lex.yy.c -lfl but there's still an error. It says..

ld.exe: cannot find -lfl

Please help me to compile my lex.yy.c file. Thank you so much.

Brian
  • 3,850
  • 3
  • 21
  • 37
Aron
  • 129
  • 1
  • 3
  • 13

1 Answers1

0

There are several approaches. The ones I recommend are.

Provide your own yywrap() function.

Since my compilers usually allow multiple source files, I prefer to define my own yywrap(). I'm compiling with C++, but the point should be obvious. If someone calls the compiler with multiple source files, I store them in a list or array, and then yywrap() is called at the end of each file to give you a chance to continue with a new file. NOTE: getNextFile() or compiler->getNextFile() are my own functions, not flex functions.

int yywrap() {
   // open next reference or source file and start scanning
   if((yyin = getNextFile()) != NULL) {
      line = 0; // reset line counter for next source file
      return 0;
   }
   return 1;
}

If building with C++ use:

extern "C" int yywrap() {
   // open next reference or source file and start scanning
   if((yyin = compiler->getNextFile()) != NULL) {
      line = 0; // reset line counter for next source file
      return 0;
   }
   return 1;
}

With older lex/flex, yywrap() was a macro, so to redefine it we had to do:

%{
#undef yywrap
%}

Potentially you could redefine a simple macro which would end scanning after a single file:

%{
#undef yywrap
#define yywrap() 1
%}

With newer Flex / POSIX lex, you can disable yywrap altogether at the command line:

flex --noyywrap

This works for other functions besides yywrap (--noyymore etc.)

Or put it in the options section.

%option noyywrap
/* end of rules */
%%

Where the general syntax of a lex file is:

Options and definitions
%%
Rules
%%
C code
codenheim
  • 20,467
  • 1
  • 59
  • 80
  • thanks, I appreciate the answer. But the error's still there. `%option noyywrap` is already on the generated `lex.yy.c` – Aron Jul 24 '14 at 04:59
  • Is it in the options section (ie above the first %%) ? Did you try one of the other options I suggested? – codenheim Jul 24 '14 at 05:16