在Ubuntu 14.04的lex中无法识别的规则 [英] Unrecognized rule in lex on Ubuntu 14.04

查看:1405
本文介绍了在Ubuntu 14.04的lex中无法识别的规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让lex使用echo在程序中吐出保留字,但我继续得到以下错误:

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

下面是我的lex扫描程序代码:

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();
}

这是我为lex创建的用于扫描仪的令牌文件: / p>

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

最后,我有一个case.c文件结合所有:

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.

推荐答案

错误消息说:错误在第30行,你定义你的令牌。

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

 begin       { ECHO; }   

如果你用一些有意义的代码定义 - 也许是begin来替换它,

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是一个特殊语句,只能在操作部分使用。

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

请参阅 http://dinosaur.compilertools.net/flex/manpage.html

这篇关于在Ubuntu 14.04的lex中无法识别的规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆