我是编程新手,但是我有一个yacc / lex项目,并且在详细编写错误消息(错误的行和类型)方面有些困难。请提供一个小例子来帮助您。
问不好的问题会得到正确但无用的答案
void yyerror(const char *s);
extern int line_num;
void yyerror(const char *s)
{
std::cerr << "PARSING ERROR: " << line_num << " " << s << std::endl;
exit(1);
}
在Flex扫描仪定义中添加以下内容,以使扫描仪跟踪行号:
%option yylineno
(请参阅the flex manual。]
然后将以下声明添加到您的野牛语法中:
%define parse.error verbose
%define parse.lac full
(请参阅error reporting和LAC (lookahead correction)上的野牛手册章节。
最后,使用yyerror
的定义,该定义使用行号信息。至少,类似:
void yyerror(const char* msg) {
fprintf(stderr, "At line %d: %s\n", yylineno, msg);
}