【发布时间】:2021-05-30 19:08:08
【问题描述】:
我在 Windows 上使用来自 this repository 的 flex 和 bison 以及 Visual Studio 2019 来生成一个小型计算器应用程序。
之前我通过 VS 中的构建属性启用了 bison 调试消息。我现在已经关闭它,清理并重建了项目,但调试消息仍然存在于计算器中。我还尝试了许多其他方法来显式禁用它,例如将 YYDEBUG 设置为 0 和 %define parse.trace false。
弹性
%option noyywrap
%{
#include <stdlib.h>
#include "calculator.tab.h"
#define YY_DECL extern "C" int yylex()
void yyerror(const char*);
%}
digit [0-9]
%%
{digit}+ {
yylval = atoi(yytext);
return INTEGER;
}
[-+*/\n()] {
return *yytext;
}
[ \t] ;
. {
yyerror("Unrecognised character.");
}
%%
start /B /WAIT /D "%(RootDir)%(Directory)" win_flex.exe --outfile="%(Filename).flex.cpp" --wincompat "%(Filename)%(Extension)"
exit /b %errorlevel%
野牛
%{
#include <stdlib.h>
#include <stdio.h>
extern "C" int yylex();
void yyerror(const char*);
%}
%token INTEGER
%left '-' '+'
%left '*' '/'
%%
program:
program expr '\n' { printf("Result: %d\n", $2); }
|
;
expr:
| INTEGER { $$ = $1; }
| expr '-' expr { $$ = $1 - $3; }
| expr '+' expr { $$ = $1 + $3; }
| expr '*' expr { $$ = $1 * $3; }
| expr '/' expr { $$ = $1 / $3; }
| '(' expr ')' { $$ = $2; }
;
%%
void yyerror(const char* msg){
fprintf(stderr, "ERROR: %s\n", msg);
}
int main(void){
yyparse();
return 0;
}
start /B /WAIT /D "%(RootDir)%(Directory)" win_bison.exe --output="%(Filename).tab.cpp" --defines="%(Filename).tab.h" "%(Filename)%(Extension)"
exit /b %errorlevel%
运行时输出
--(end of buffer or a NUL)
1+5
--accepting rule at line 14 ("1")
--accepting rule at line 18 ("+")
--accepting rule at line 14 ("5")
--accepting rule at line 18 ("
")
Result: 6
--(end of buffer or a NUL)
如何从我的应用程序中正确删除调试消息/跟踪?
【问题讨论】:
-
这些看起来像 flex 调试消息,而不是野牛的。您需要从 flex 中删除
--debug选项。 -
@ChrisDodd 嗨,我在 VS 中也禁用了 Flex 调试,你可以看到它在上面生成的命令行输入,没有 --debug 但仍然包括跟踪。
-
如果 flex 调试已打开(使用
--debug),您可以在程序中调用yyset_debug(0);将其关闭,至少在相当新的 flex 版本中是这样。 -
两个建议:(1)
[ \t]+ ;效率更高; (2). return yytext[0];将非法字符返回给解析器,解析器会将其视为语法错误,同时也让解析器有机会进行错误恢复,而不是直接将字符扔掉。
标签: debugging compiler-construction bison yacc lex