Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined Reference To yywrap

Tags:

flex-lexer

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?

like image 742
Nathan Campos Avatar asked Nov 28 '09 00:11

Nathan Campos


People also ask

What does Yywrap () do?

A lex library routine that you can redefine is yywrap() , which is called whenever the scanner reaches the end of file. If yywrap() returns 1, the scanner continues with normal wrapup on the end of input.

What is Yywrap in yacc?

yywrap() Returns the value 1 when the end of input occurs. yymore() Appends the next matched string to the current value of the yytext array rather than replacing the contents of the yytext array.

What is option Noyywrap?

This option is implied by `%option lex-compat' . `yywrap' if unset (i.e., `%option noyywrap' ), makes the scanner not call `yywrap()' upon an end-of-file, but simply assume that there are no more files to scan (until the user points yyin at a new file and calls `yylex()' again).


2 Answers

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.

like image 115
hjhill Avatar answered Sep 23 '22 20:09

hjhill


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; } 
like image 32
codenheim Avatar answered Sep 24 '22 20:09

codenheim