Search code examples
compiler-errorslexcc

unrecognized rule error in lex {lineo++;}


I've got some errors in my lex code, maybe someone know what i made wrong ...

16    %%
17    {ws}  {/*no action and no return */}
18    {}    
19    {newline} {lineo++;}
20    {number}  {tokenval=atoi(yytext); return(NUM);}
21    {id}      {
22          int p = 0;
23          p = lookup(yytext);
24          if (p == 0) 
25          p = insert (yytext, ID);
26      tokenval = p;
27      return symtable[p].token;
28    
29    }
30    
31    <<EOF>>   {return DONE;}
32      {tokenval = NONE; return yytext[0];}
33    
34    
35    %%

I'm newbie in this kind of technology, and I spend on it many of time but without any positive result. Below errors from the console:

new.l:21: unrecognized rule
new.l:28: unrecognized rule
new.l:28: unrecognized rule
new.l:36: EOF encountered inside an action
new.l:36: unrecognized rule
new.l:36: fatal parse error
make: *** [lex.yy.c] Error 1

When I'm deleted that lines of code whoes bad (lines: 21-32) I've got that error:

new.l:19:2: error: ‘lineo’ undeclared (first use in this function) {newline} {lineo++;}


Solution

    1. Line 18 ({}) is just wrong; flex will treat that as a pattern, not an action.

    2. You probably meant to write lineno (that is, a way of typing "Line Nº" which is an abbreviation for "line number".) But did you declare it somewhere? Or did you intend to use yylineno?

    3. Did you define {id}? The error message suggests you didn't, but you didn't paste your definitions.

    4. In line 32, the pattern for the default rule is .:

      . {tokenval = NONE; return yytext[0];}

    5. Have you read the handy flex manual?

    In addition to the above, as a replacement to point 3, the definition:

    id      {letter}({letter} | {digit})*
    

    is incorrect because regular expressions cannot include unquoted whitespace. You need to write that as

    id      {letter}({letter}|{digit})*