Flex文件中的"premature eof error"错误

15

我有以下代码,当我运行 flex hello.l 命令时会出现错误 "hello.l",line 31: premature EOF。

%{

  #include <stdlib.h>
  #include "y.tab.h"

  %}

%%

("hi"|"oi")"\n"      {return HI; }
("tchau"|"bye")"\n"  {return BYE;}
.                    {yyerror(); }

%%

int main(void)
{
    yyparse();
    return 0;
}

int yywrap(void)
{
    return 0;
}

int yyerror(void)
{
    printf("Error\n");
    exit(1);
}
2个回答

30

问题出在你的%}上 - flex 对于空格非常敏感。删除它前面的空格,一切都应该没问题。

此外,如果你不想要一个yywrap函数,可以在你的flex文件中添加%option noyywrap


你能告诉我如何在Dev-C++中编译运行lex.yy.c和y.tab.c文件吗? - Waseem

7

将此更改为:

%{

  #include <stdlib.h>
  #include "y.tab.h"

  %}

到这个:
%{

  #include <stdlib.h>
  #include "y.tab.h"

%}

它可以与Flex 2.5.35 (mingw)配合使用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接