lex 中无法识别的规则

发布于 2024-08-30 12:23:23 字数 2221 浏览 3 评论 0原文

我正在 lex 中编写一个程序,它给了我以下错误:

scanner.l:49: 无法识别的规则

第 49 行是: {number} {return(NUM);}

编辑: 但是,该错误似乎与之前的行 {id} {return(ID);} 相关。 它将直接将该规则之后的行列为错误源,即使它是空白的。

这是我的代码:

#include <stdio.h>

%token BOOL, ELSE, IF, TRUE, WHILE, DO, FALSE, INT, VOID
%token LPAREN, RPAREN, LBRACK, RBRACK, LBRACE, RBRACE, SEMI, COMMA, PLUS, MINUS, TIMES
%token DIV, MOD, AND, OR, NOT, IS, ADDR, EQ, NE, LT, GT, LE, GE
%token NUM, ID, PUNCT, OP

int line = 1, numAttr;
char *strAttr;
%}

/* regular definitions */

delim   [ \t]
ws      {delim}+
letter  [A-Za-z]
digit   [0-9]
id      ({letter} | _)({letter} | {digit} | _)*
number  {digit}+

%%

{ws}        {/* no action and no return */}
[\n]        {line++;}
bool        {return(BOOL);}
else        {return(ELSE);}
if          {return(IF);}
true        {return(TRUE);}
while       {return(WHILE);}
do          {return(DO);}
false       {return(FALSE);}
int         {return(INT);}
void        {return(VOID);}

{id}        {return(ID);}  // error is on these two lines
{number}    {return(NUM);} //

"("         {yylval = LPAREN; return(PUNCT);}
")"         {yylval = RPAREN; return(PUNCT);}
"["         {yylval = LBRACK; return(PUNCT);}
"]"         {yylval = RBRACK; return(PUNCT);}
"{"         {yylval = LBRACE; return(PUNCT);}
"}"         {yylval = RBRACE; return(PUNCT);}
";"         {yylval = SEMI;   return(PUNCT);}
","         {yylval = COMMA;  return(PUNCT);}

"+"         {yylval = PLUS;   return(OP);}
"-"         {yylval = MINUS;  return(OP);}
"*"         {yylval = TIMES;  return(OP);}
"/"         {yylval = DIV;    return(OP);}
"%"         {yylval = MOD;    return(OP);}
"&"         {yylval = ADDR;   return(OP);}
"&&"        {yylval = AND;    return(OP);}
"||"        {yylval = OR;     return(OP);}
"!"         {yylval = NOT;    return(OP);}
"!="        {yylval = NE;     return(OP);}
"="         {yylval = IS;     return(OP);}
"=="        {yylval = EQ;     return(OP);}
"<"         {yylval = LT;     return(OP);}
"<="        {yylval = LE;     return(OP);}
">"         {yylval = GT;     return(OP);}
">="        {yylval = GE;     return(OP);}

%%

该规则有什么问题?谢谢。

I'm writing a program in lex, and it gives me the following error:

scanner.l:49: unrecognized rule

Line 49 is: {number} {return(NUM);}

EDIT:
However, the error seems related to the line directly before that, {id} {return(ID);}.
It will list the line directly after that rule as the source of the error, even if it's blank.

Here's my code:

#include <stdio.h>

%token BOOL, ELSE, IF, TRUE, WHILE, DO, FALSE, INT, VOID
%token LPAREN, RPAREN, LBRACK, RBRACK, LBRACE, RBRACE, SEMI, COMMA, PLUS, MINUS, TIMES
%token DIV, MOD, AND, OR, NOT, IS, ADDR, EQ, NE, LT, GT, LE, GE
%token NUM, ID, PUNCT, OP

int line = 1, numAttr;
char *strAttr;
%}

/* regular definitions */

delim   [ \t]
ws      {delim}+
letter  [A-Za-z]
digit   [0-9]
id      ({letter} | _)({letter} | {digit} | _)*
number  {digit}+

%%

{ws}        {/* no action and no return */}
[\n]        {line++;}
bool        {return(BOOL);}
else        {return(ELSE);}
if          {return(IF);}
true        {return(TRUE);}
while       {return(WHILE);}
do          {return(DO);}
false       {return(FALSE);}
int         {return(INT);}
void        {return(VOID);}

{id}        {return(ID);}  // error is on these two lines
{number}    {return(NUM);} //

"("         {yylval = LPAREN; return(PUNCT);}
")"         {yylval = RPAREN; return(PUNCT);}
"["         {yylval = LBRACK; return(PUNCT);}
"]"         {yylval = RBRACK; return(PUNCT);}
"{"         {yylval = LBRACE; return(PUNCT);}
"}"         {yylval = RBRACE; return(PUNCT);}
";"         {yylval = SEMI;   return(PUNCT);}
","         {yylval = COMMA;  return(PUNCT);}

"+"         {yylval = PLUS;   return(OP);}
"-"         {yylval = MINUS;  return(OP);}
"*"         {yylval = TIMES;  return(OP);}
"/"         {yylval = DIV;    return(OP);}
"%"         {yylval = MOD;    return(OP);}
"&"         {yylval = ADDR;   return(OP);}
"&&"        {yylval = AND;    return(OP);}
"||"        {yylval = OR;     return(OP);}
"!"         {yylval = NOT;    return(OP);}
"!="        {yylval = NE;     return(OP);}
"="         {yylval = IS;     return(OP);}
"=="        {yylval = EQ;     return(OP);}
"<"         {yylval = LT;     return(OP);}
"<="        {yylval = LE;     return(OP);}
">"         {yylval = GT;     return(OP);}
">="        {yylval = GE;     return(OP);}

%%

What is wrong with that rule? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

多彩岁月 2024-09-06 12:23:24

我也遇到了这个问题。我在 id 的定义行后面添加了注释。也许这也是评论问题。

I ran into this problem too. I put a comment after the definition line of id. Maybe it's a comment issue here as well.

宫墨修音 2024-09-06 12:23:23

上一行导致了问题。如果您在 {id} 规则和 {number} 规则之间添加空格,您会注意到错误的行号不会更改。

模式中不允许有空格。定义 {id} 如下:

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

The previous line is causing the problem. If you add a space between the {id} rule and the {number} rule, you'll notice that the line number of the error doesn't change.

whitespace isn't allowed in patterns. Define {id} thus:

id      ({letter}|_)({letter}|{digit}|_)*
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文