Unrecognized rule in lex on Ubuntu 14.04

2019-09-03 16:07发布

问题:

I am attempting to have lex use echo to spit back out reserved words in a program but I continue to get the following errors:

    scanner.l:30: unrecognized rule
scanner.l:30: unrecognized rule
scanner.l:70: unrecognized rule
scanner.l:70: unrecognized rule
scanner.l:70: unrecognized rule
scanner.l:71: unrecognized rule
scanner.l:71: unrecognized rule

Below is my scanner code for lex:

%{
#include <stdio.h> 
#include <ctype.h>  
#include "tokens.h"
%}


ws      [ \t\r\n]+
quoted      \".*\"
letter      [A-Za-z]
digit       [0-9]
word        {letter}+(\-{letter}+)?
number      {digit}+
punc        [,:;()] 
begin       { ECHO; }        
boolean     { ECHO; }    
else        { ECHO; }       
end     { ECHO; }       
endif       { ECHO; }         
function    { ECHO; }       
if      { ECHO; }       
is      { ECHO; }       
integer     { ECHO; }       
real        { ECHO; }       
returns     { ECHO; }          
then        { ECHO; }         

%%

{begin}             { return(begin); } 
{boolean}           { return(BOOLEAN); } 
{else}              { return(ELSE); }
{end}               { return(END); }
{endif}             { return(ENDIF); }   
{function}          { return(FUNCTION); }
{if}                { return(IF); }
{is}                { return(IS); }
{integer}           { return(INTEGER); }
{real}              { return(REAL); }
{returns}           { return(RETURNS); }   
{then}              { return(THEN); }   
"&&"                    { return(LOGOPAND); }                   
"||"                    { return(LOGOPOR); }                           
"!="                    { return(LOGOPNOT); }   
[ \t\n]                 ;
{ws}                    { ECHO; }                          
"<"                     { ECHO; return(RELOP); } 
"="                     { ECHO; return(RELOP); } 
"/="                    { ECHO; return(RELOP); } 
">"                     { ECHO; return(RELOP); } 
">="                    { ECHO; return(RELOP); } 
"<="                    { ECHO; return(RELOP); }
"*"                     { ECHO; return(MULTOP); } 
"/"                     { ECHO; return(MULTOP); }  
"+"                     { ECHO; return(ADDOP); }      
"-"                     { ECHO; return(ADDOP); }
"true"                  { ECHO; return(BOOLLITERAL); }          
"false"                 { ECHO; return(BOOLLITERAL); }  
{digit}                 { ECHO; return(I_LITERAL); } 
{digit}+"."{digit}*             { ECHO; return(R_LITERAL); } 
begins                  { ECHO; return(BEGINS); }  
{punc}                  { ECHO; return yytext[0]; }   
{quoted}            { ECHO; }
{word}              {toTitle(yytext, yyleng); }
{number}            { ECHO; }

%%

int main() {
yylex();
}

This is a token file I created for lex to use with the scanner:

#ifndef TOKENS_H
#define TOKENS_H

typedef enum Tokens {RELOP = 256, ADDOP = 257, MULTOP = 258, LOGOPNOT = 259, BOOLLITERAL = 260, I_LITERAL = 261, R_LITERAL = 262, IDENTIFIER = 263, PUNCTUATION = 264, BEGINS = 265, BOOLEAN = 266, ELSE = 267, END = 268, ENDIF = 269, FUNCTION = 270, IF = 271, IS = 272, INTEGER = 273, REAL = 274, RETURNS = 275, THEN = 276, LOGOPAND = 277, LOGOPOR = 278, begin = 279} Tokens;

#endif

Finally I have a case.c file to be used in conjunction with it all:

#include <stdio.h>
#include <ctype.h>
#include "tokens.h"

extern int yylex();
extern int yyleng();
extern int yylineno;
extern char* yytext;


void toTitle(char* yytext, int yyleng)
{
    putchar(toupper(yytext[0]));
    int i;
    for (i = 1; i < yyleng; i++)
        putchar(toupper(yytext[i]));
}

int main()
{
    while (yylex());
    return 0;
}

Why would I be getting these errors? I'm sure it is simple. Thanks.

回答1:

As the error message says: the error is in line 30, in the definitions section, where you define your tokens.

 begin       { ECHO; }   

If you replace this by some meaningful token definition - maybe "begin" - , you will get the same error message for the equivalent definitions that follow.

ECHO is a special statement, that can only be used in the actions section.

see http://dinosaur.compilertools.net/flex/manpage.html