Undefined Reference To yywrap
Asked Answered
S

5

99

I have a simple "language" that I'm using Flex(Lexical Analyzer), it's like this:

/* Just like UNIX wc */
%{
int chars = 0;
int words = 0;
int lines = 0;
%}

%%
[a-zA-Z]+ { words++; chars += strlen(yytext); }
\n        { chars++; lines++; }
.         { chars++; }
%%

int main()
{
    yylex();
    printf("%8d%8d%8d\n", lines, words, chars);
}

The I run a flex count.l, all goes ok without errors or warnings, then when I try to do a cc lex.yy.c I got this errors:

ubuntu@eeepc:~/Desktop$ cc lex.yy.c
/tmp/ccwwkhvq.o: In function yylex': lex.yy.c:(.text+0x402): undefined reference toyywrap'
/tmp/ccwwkhvq.o: In function input': lex.yy.c:(.text+0xe25): undefined reference toyywrap'
collect2: ld returned 1 exit status

What is wrong?

Snorkel answered 28/11, 2009 at 0:30 Comment(0)
R
154

The scanner calls this function on end of file, so you can point it to another file and continue scanning its contents. If you don't need this, use

%option noyywrap

in the scanner specification.

Although disabling yywrap is certainly the best option, it may also be possible to link with -lfl to use the default yywrap() function in the library fl (i.e. libfl.a) provided by flex. Posix requires that library to be available with the linker flag -ll and the default OS X install only provides that name.

Revelry answered 28/11, 2009 at 0:42 Comment(3)
Exactly where is '%option noyywrap' to be added, please? In the definition section or on the command line?Marshall
I added it to the beginning of the .l file and lex accepted it! I am not sure if that is the best place. Maybe it doesn't matter.Succoth
It's supposed to be in the options section, between the first %} and %%.Whitmire
E
15

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.

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;
}
Easement answered 24/7, 2014 at 4:49 Comment(1)
Hello @codenheim I tried adding your suggested yywrap() function but got an error: "compiler undeclared"Durning
C
7
int yywrap(){return(1);}

use this code at the end of the program..Simple

Clathrate answered 18/2, 2019 at 9:53 Comment(0)
I
4

flex doesn't always install with its development libraries (which is odd, as it is a development tool). Install the libraries, and life is better.

On Redhat base systems:

yum -y install flex-devel
./configure && make

On Debian based systems

sudo apt-get install libfl-dev
Isothermal answered 1/8, 2016 at 15:39 Comment(1)
What is the equivalent for arch ?Queridas
K
3

As a note for followers, flex 2.6.3 has a bug where libfl.a "typically would" define yywrap but then doesn't in certain instances, so check if that's your version of flex, might be related to your problem:

https://github.com/westes/flex/issues/154

Karate answered 9/3, 2017 at 17:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.