flex文件中过早出现eof错误

时间:2022-01-22 09:43:14

I have the following code and it gives an error" "hello.l",line 31: premature EOF" when I run the following command flex hello.l

我有下面的代码,它会给出一个错误“hello”。当我运行以下命令flex hello.l时,第31行:早熟的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 个解决方案

#1


20  

The problem is with your %} - flex is very sensitive about spacing. Remove the space before it, and all should be well.

问题是您的%}- flex对间隔非常敏感。去掉前面的空格,一切都会好起来的。

Also, if you don't want a yywrap function, you can stick %option noyywrap in your flex file.

另外,如果您不想要ywrap函数,可以在flex文件中插入%选项noywrap。

#2


5  

Change this:

改变:

%{

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

  %}

To this:

:

%{

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

%}

It works with flex 2.5.35 (mingw)

它与flex 2.5.35兼容(mingw)

#1


20  

The problem is with your %} - flex is very sensitive about spacing. Remove the space before it, and all should be well.

问题是您的%}- flex对间隔非常敏感。去掉前面的空格,一切都会好起来的。

Also, if you don't want a yywrap function, you can stick %option noyywrap in your flex file.

另外,如果您不想要ywrap函数,可以在flex文件中插入%选项noywrap。

#2


5  

Change this:

改变:

%{

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

  %}

To this:

:

%{

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

%}

It works with flex 2.5.35 (mingw)

它与flex 2.5.35兼容(mingw)