Unrecognized rule in lex on Ubuntu 14.04

风格不统一 提交于 2019-12-12 02:05:34

问题


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



来源:https://stackoverflow.com/questions/28143517/unrecognized-rule-in-lex-on-ubuntu-14-04

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!