对 GCC 和 Flex/Bison 没有效果警告的声明
当使用 gcc 和 -Wall 选项编译我的项目时,我收到一条关于在我的 Flex 文件不存在的最后一行中没有任何效果的语句的警告:
警告:
gcc -Wall -O0 -ggdb3 -DNOSUDO -DJOBC -DDEBUG -c lex.yy.c
tokenizer.l: In function ‘yylex’:
tokenizer.l:179: warning: statement with no effect
Shell命令:
$ wc -l tokenizer.l
178 tokenizer.l
lex 文件的最后部分:
; {
return SEMI;
}
有人知道为什么我可能会收到此警告吗?
如果我抑制所有#line指令,错误是:
lex.yy.c: In function ‘yylex’:
lex.yy.c:1021: warning: statement with no effect
它引用了以下中的ECHO行:
case 30:
YY_RULE_SETUP
ECHO;
YY_BREAK
case YY_STATE_EOF(INITIAL):
case YY_STATE_EOF(inQuote):
case YY_STATE_EOF(inWord):
yyterminate();
When compiling my project with gcc and the -Wall option, I get a warning about a statement with no effect in the non-existant last line of my flex file:
Warning:
gcc -Wall -O0 -ggdb3 -DNOSUDO -DJOBC -DDEBUG -c lex.yy.c
tokenizer.l: In function ‘yylex’:
tokenizer.l:179: warning: statement with no effect
Shell Command:
$ wc -l tokenizer.l
178 tokenizer.l
Last part of lex file:
; {
return SEMI;
}
Anyone know why I might be getting this warning?
If I suppress all #line Directives, the error is:
lex.yy.c: In function ‘yylex’:
lex.yy.c:1021: warning: statement with no effect
Which refers to the ECHO line in:
case 30:
YY_RULE_SETUP
ECHO;
YY_BREAK
case YY_STATE_EOF(INITIAL):
case YY_STATE_EOF(inQuote):
case YY_STATE_EOF(inWord):
yyterminate();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一般来说,您会从任何具有无条件“返回”的 lex 规则中得到该错误,因为 flex 最终会生成如下所示的代码(在宏扩展之后):
问题在于“break”无法访问。它需要存在,以防您的规则不返回(因此代码不会落入下一个标记的代码,而是会继续识别下一个标记。)Flex 不够智能,无法实际解析您的规则代码并进行流分析以确定何时不需要中断,因此它总是将其放在那里。一般来说,在 flex (或 bison)的输出上使用 -Wall 会给你很多像这样的警告......
In general, you'll get that error from any lex rule that has an unconditional 'return' in it, because flex ends up generating code that looks like (after macro expansion):
The problem being that the 'break' is unreachable. It needs to be there in case your rule does not return (so the code won't fall through to the code for the next token, but will instead go on to recognize the next token.) Flex isn't smart enough to actually parse your rule code and do flow analysis to determine when the break is unneeded, so it just always puts it there. In general, using -Wall on the output of flex (or bison) will give you lots of warnings like this...
我猜感觉
;
是一条没有效果的语句。尝试引用它:I guess it feels that
;
is a statement with no effect. Try quoting it:我遇到了同样的问题并进行了一些研究。事实证明,flex 源文件(.l 文件)中的“#includes”之一以某种方式间接包含了标准库 termios.h 文件,该文件定义了一个名为“ECHO”的宏(如“0000010”)。
另一方面,flex生成的源文件也想创建一个名为“ECHO”的宏,前面有一个“#ifndef ECHO”。但是,当然,在包含 termios.h 的情况下,“ifndef”的计算结果为 false,并且 ECHO 未按预期定义。这导致了错误。
长解释,短解决方案:我将
所有包含内容放在 .l 文件中,这样就成功了。
我知道这个讨论线程已经有点旧了,但当我用谷歌搜索警告时它首先出现,所以我的贡献可能对人们仍然有用。
I ran into the same problem and looked a bit into it. It turned out that one of my "#includes" in the flex source file (the .l file) somehow indirectly included the standard library termios.h file, which defines a macro named "ECHO" (as "0000010").
The source file generated by flex on the other hand also wants to create a macro named "ECHO", there is an "#ifndef ECHO" before that. But of course, with termios.h included, that "ifndef" evaluates to false and ECHO wasn't defined as intended. That led to the error.
Long explanation, short solution: I put
below all my includes in my .l file, and that did the trick.
I know this discussion thread is already a bit old, but it turned up first when I googled the warning, so my contribution might still be useful for folks.
正如其他答案所说,问题在于“返回”后无法达到“中断”。
过去可以让编译器相信有一种方法可以通过伪造条件返回来达到“中断”:
我怀疑某些现代编译器足够聪明,能够看穿这一点 - 这实际上会适得其反他们。 “是的,你是对的,编译器先生,但我无法避免它,所以 STFU”。
As the other answers said, the trouble is that a 'break' after a 'return' can't be reached.
It used to be possible to satisfy a compiler that there was a way to get to the 'break' by faking a conditional return:
I have a suspicion that some modern compilers are clever enough to see through this - which is actually counter-productive of them. "Yes, you're right, Mr Compiler, but I can't avoid it so STFU".