Bison 构建警告:“给出了 -s 选项,但可以匹配默认规则”
我会收到警告,
warning, -s option given but default rule can be matched
如果您搜索“给定选项但可以匹配默认规则”, 您会发现 Flex 手册中有关诊断的章节以及旧 Flex 手册页中的此条目 在诊断部分:
警告,给出了 '-s 选项,但默认 规则可以匹配’意味着它是 可能(也许仅在特定的情况下) 启动条件)表示默认规则 (匹配任何单个字符)是 只有一个与特定的匹配 输入。由于给出了
-s
, 大概这不是有意的。
我的构建文件没有 -s
选项。我写了
bison -y -d calc1.y
flex calc1.l
gcc code...
如何修复此警告?
这是我的 lex 文件的一个小版本。该文件也会触发警告
%{
#include "y.tab.h"
%}
%option noyywrap nodefault yylineno
%%
[0-9]+ {
return INTEGER;
}
[-+()=/*{},;\n] { return *yytext; }
[ \t] /* skip whitespace */
[a-zA-Z0-9_]* { printf("lex Unknown character = '%s'", yytext); yyerror("lex Unknown character"); }
%%
I get the warning
warning, -s option given but default rule can be matched
if you google "option given but default rule can be matched", you'll find the Flex manual's chapter on Diagnostics and this entry in an old Flex manpage in the Diagnostics section:
warning, '-s option given but default
rule can be matched' means that it is
possible (perhaps only in a particular
start condition) that the default rule
(match any single character) is the
only one that will match a particular
input. Since-s
was given,
presumably this is not intended.
My build file doesn't have a -s
option. I wrote
bison -y -d calc1.y
flex calc1.l
gcc code...
How do I fix this warning?
Here is a tiny version of my lex file. This file also triggers the warning
%{
#include "y.tab.h"
%}
%option noyywrap nodefault yylineno
%%
[0-9]+ {
return INTEGER;
}
[-+()=/*{},;\n] { return *yytext; }
[ \t] /* skip whitespace */
[a-zA-Z0-9_]* { printf("lex Unknown character = '%s'", yytext); yyerror("lex Unknown character"); }
%%
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
%选项无默认值
相当于-s
%option nodefault
is equivalent to -s
您系统上的“flex”命令可能已被别名为带有 -s 选项的 flex 命令。这可能发生在多个启动脚本中的任何一个中。
该警告告诉您,有些输入与输入文件中的任何规则都不匹配。该警告旨在让您确保已考虑正则表达式中的所有情况。
如果您确实希望使用默认规则,请将其添加到输入文件中规则的末尾。尝试添加规则
更新:
你提供的输入文件包含%option nodefault,这和在命令行上指定“-s”是一样的,这样谜团就解开了。此外,该文件没有“.”规则。有一些字符未涵盖,例如“@”。
我也对字符类中的“-”符号表示怀疑。它应该是一个范围运算符,但它可以作为您拥有的第一个位置的字符。我不知道。使用“\-”代替可能是个好主意。
您可以更改最后一条规则以
消除警告(并防止生成的扫描仪崩溃)。
另外,您能否将问题的标题更改为有关 Flex 而不是 bison?
The "flex" command on your system could have been aliased to be the flex command with an -s option. That could occur in any of several startup scripts.
The warning is telling you that there are some inputs that aren't matched by any of the rules in your input file. The warning is there to get you to make sure you have considered all cases in your regular expressions.
If you truly want the default rule to be used, then add it to the end of the rules in your input file. Try adding the rules
Update:
The input file you provide includes the %option nodefault, which is the same as specifying "-s" on the command line, so that mystery is solved. Also, the file does not have a rule for ".". There are some characters not covered, '@' for example.
I'm also suspicious of the "-" symbol in the character class. It is supposed to be a range operator, but it may work as a character in the first position as you have it. I don't know. It might be a good idea to use "\-" instead.
You could change the last rule to
to get rid of the warning (and to prevent the resulting scanner from crashing).
Also, could you change the title of the question to be about flex instead of bison?