Search code examples
apache-flexeof

Premature EOF error in flex


When I run the following code in flex , i get premature EOF at the last line as an error. If the definition part is removed no error is generated.Why is this so??

%{
     #include <stdio.h>
       int x = 0;
}%

%%
"a"  {x=x+1; printf("id %d",x); }
%%

int yywrap(void)
{
    return 0;
}

int main(void)
{
int x = 0;
    yylex();
    return 0;
}

Solution

  • The third line of your code has the problem . It should be %} instead of }%

    Try this :

    %{
    x = 0;
    %}
    
    %%
    [a]  {x++; printf("id %d",x); }
    %%
    
    int main(void)
    {
        yylex();
        return 0;
    }
    

    I don't know what you intend to do but a working example that will print an id for every a encountered in input file will somewhat look like this :

    %{
    x = 0;
    %}
    
    %%
    [a]  {x++; printf("id %d ",x); }
    %%
    
    int main(int argc,char * argv[])
    {
      yyin = fopen(argv[1],"r");
      yylex();
      fclose(yyin);
      return 0;
    }