OK, so I suppose my question is quite self-explanatory.
好的,所以我想我的问题是不言自明的。
I'm currently building a parser in Bison, and I want to make error reporting somewhat better.
我目前正在Bison中构建一个解析器,我想让错误报告更好一些。
Currently, I've set %define parse.error verbose
(which actually gives messages like syntax error, unexpected ***********************, expecting ********************
.
目前,我已经设置了%define parse.error verbose(实际上它提供了语法错误,意外的消息***********************,期待*** *****************。
All I want is to add some more information in the error messages, e.g. line number (in input/file/etc)
我想要的只是在错误消息中添加更多信息,例如行号(输入/文件/等)
My current yyerror
(well nothing... unusual... lol) :
我当前的yyerror(没什么......不寻常......哈哈):
void yyerror(const char *str)
{
fprintf(stderr,"\x1B[35mInterpreter : \x1B[37m%s\n",str);
}
P.S.
- I've gone through the latest Bison documentation, but I seem quite lost...
- I've also had a look into the
%locations
directive, which most likely is very close to what I need - however, I still found no complete working example and I'm not sure how this is to be used.
我已经阅读了最新的Bison文档,但我似乎很丢失......
我还看了一下%locations指令,它很可能非常接近我的需要 - 但是,我仍然没有找到完整的工作示例,我不确定如何使用它。
1 个解决方案
#1
4
So, here I'm a with a step-by-step solution :
所以,在这里,我是一个循序渐进的解决方案:
- We add the
%locations
directive in our grammar file (between%}
and the first%%
) - We make sure that our lexer file contains an include for our parser (e.g.
#include "mygrammar.tab.h"
), at the top - We add the
%option yylineno
option in our lexer file (between%}
and the first%%
)
我们在语法文件中添加%locations指令(在%}和第一个%%之间)
我们确保我们的lexer文件包含我们的解析器的包含(例如#include“mygrammar.tab.h”),在顶部
我们在lexer文件中添加%选项yylineno选项(在%}和第一个%%之间)
And now, in our yyerror
function (which will supposedly be in our lexer file), we may freely use this... yylineno
(= current line in file being processed) :
现在,在我们的yyerror函数中(可能会在我们的lexer文件中),我们可以*地使用它... yylineno(=正在处理的文件中的当前行):
void yyerror(const char *str)
{
fprintf(stderr,"Error | Line: %d\n%s\n",yylineno,str);
}
Yep. Simple as that! :-)
是的。就那么简单! :-)
#1
4
So, here I'm a with a step-by-step solution :
所以,在这里,我是一个循序渐进的解决方案:
- We add the
%locations
directive in our grammar file (between%}
and the first%%
) - We make sure that our lexer file contains an include for our parser (e.g.
#include "mygrammar.tab.h"
), at the top - We add the
%option yylineno
option in our lexer file (between%}
and the first%%
)
我们在语法文件中添加%locations指令(在%}和第一个%%之间)
我们确保我们的lexer文件包含我们的解析器的包含(例如#include“mygrammar.tab.h”),在顶部
我们在lexer文件中添加%选项yylineno选项(在%}和第一个%%之间)
And now, in our yyerror
function (which will supposedly be in our lexer file), we may freely use this... yylineno
(= current line in file being processed) :
现在,在我们的yyerror函数中(可能会在我们的lexer文件中),我们可以*地使用它... yylineno(=正在处理的文件中的当前行):
void yyerror(const char *str)
{
fprintf(stderr,"Error | Line: %d\n%s\n",yylineno,str);
}
Yep. Simple as that! :-)
是的。就那么简单! :-)