获取:警告,规则无法匹配。

时间:2022-08-23 09:39:50

I am working on building a lexical and syntax analyzer. I am getting the following warning when I try to use flex with my .l file.

我正在努力构建一个词汇和语法分析器。当我尝试使用flex与我的.l文件时,我得到了以下警告。

littleDuck.l:26: warning, rule cannot be matched

Rule 26 is the one that starts with {cteI}, my rules section is the following:

规则26是从{cteI}开始的,我的规则部分如下:

[ \t\n]     ;
{RW}        {return RESERVED;}
{id}        {return ID;}
{ops}       {return OPERATOR;}
{seps}      {return SEPARATOR;}
{cteI}      {yylval.ival = atoi(yytext); return INT;}
{cteF}      {yylval.fval = atof(yytext); return FLOAT;}
{ctestring} {yylval.sval = strdup(yytext); return STRING;}
.       ;

Also, my definitions section is this:

另外,我的定义部分是:

RW      program|var|int|float|print|else|if
id      ([a-z]|[A-Z)([a-z]|[A-Z]|[0-9])*
ops     "="|"<"|">"|"<>"|"+"|"-"|"/"|"*"
seps    ":"|","|";"|"{"|"}"|"("|")"
cteI    [0-9]+
cteF    {cteI}(\.{cteI}((e|E)("+"|"-")?{cteI})?)?
ctestring   (\".*\")

Why is this warning appearing, and how can I modify my file so it will not appear?

为什么会出现这个警告,我如何修改我的文件,这样它就不会出现?

1 个解决方案

#1


6  

The warning tells you that anything that might be matched by {cteI} will always be matched by some earlier rule. In your case, it indicates that a rule doesn't match what you expect it does, probably due to a typo. In your case, its the {id} rule, which you define as:

警告告诉您,任何可能匹配的{cteI}将始终与一些早期规则匹配。在您的示例中,它表示一条规则与您预期的不匹配,可能是由于输入错误。在您的案例中,它的{id}规则,您定义为:

([a-z]|[A-Z)([a-z]|[A-Z]|[0-9])*

Notice the nesting of the parentheses and square brackets. Adding spaces for clarity, this is

注意括号和方括号的嵌套。为清晰度增加空间,这是。

( [a-z] | [A-Z)([a-z] | [A-Z] | [0-9] )*

This will match any sequence of letters, digits, or the characters ( ) or [. You probably meant:

这将匹配任何序列的字母、数字或字符()或[。你可能意味着:

([a-z]|[A-Z])([a-z]|[A-Z]|[0-9])*

which can be more clearly written as

哪一个可以写得更清楚?

[a-zA-Z][a-zA-Z0-9]*

#1


6  

The warning tells you that anything that might be matched by {cteI} will always be matched by some earlier rule. In your case, it indicates that a rule doesn't match what you expect it does, probably due to a typo. In your case, its the {id} rule, which you define as:

警告告诉您,任何可能匹配的{cteI}将始终与一些早期规则匹配。在您的示例中,它表示一条规则与您预期的不匹配,可能是由于输入错误。在您的案例中,它的{id}规则,您定义为:

([a-z]|[A-Z)([a-z]|[A-Z]|[0-9])*

Notice the nesting of the parentheses and square brackets. Adding spaces for clarity, this is

注意括号和方括号的嵌套。为清晰度增加空间,这是。

( [a-z] | [A-Z)([a-z] | [A-Z] | [0-9] )*

This will match any sequence of letters, digits, or the characters ( ) or [. You probably meant:

这将匹配任何序列的字母、数字或字符()或[。你可能意味着:

([a-z]|[A-Z])([a-z]|[A-Z]|[0-9])*

which can be more clearly written as

哪一个可以写得更清楚?

[a-zA-Z][a-zA-Z0-9]*