未定义参考yywrap

Nat*_*pos 77 flex-lexer

我有一个简单的"语言",我正在使用Flex(词法分析器),它是这样的:

/* 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);
}
Run Code Online (Sandbox Code Playgroud)

我跑了一个flex count.l,一切顺利没有错误或警告,然后当我尝试做一个cc lex.yy.c我得到这个错误:

ubuntu @ eeepc:〜/ Desktop $ cc lex.yy.c
/tmp/ccwwkhvq.o:在函数yylex': lex.yy.c:(.text+0x402): undefined reference toyywrap'/
tmp/ccwwkhvq.o中:在函数input': lex.yy.c:(.text+0xe25): undefined reference toyywrap中'
collect2:ld返回1退出状态

怎么了?

hjh*_*ill 121

扫描程序在文件末尾调用此函数,因此您可以将其指向另一个文件并继续扫描其内容.如果您不需要,请使用

%option noyywrap
Run Code Online (Sandbox Code Playgroud)

尽管禁用yywrap 当然是最佳选择,但也可以链接-lfl以使用flex提供yywrap()的库fl(即libfl.a)中的默认函数.Posix要求库具有链接器标志-ll,默认OS X安装仅提供该名称.

  • 请问'%option noyywrap'到底要添加到哪里?在定义部分还是在命令行上? (2认同)

cod*_*eim 9

我更喜欢定义自己的yywrap().我正在用C++编译,但重点应该是显而易见的.如果有人用多个源文件调用编译器,我将它们存储在列表或数组中,然后在每个文件的末尾调用yywrap(),以便您有机会继续使用新文件.

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;
}
Run Code Online (Sandbox Code Playgroud)


小智 6

int yywrap(){return(1);}
Run Code Online (Sandbox Code Playgroud)

在程序末尾使用此代码..简单