有什么方法可以更改 Bison 错误消息的格式吗?例如,不要说 syntax error, unexpected T_ID, expected ';' or T_IMPORT
说unexpected id, expected a semicolon or import
——像$end
这样的东西更是如此。 .
我知道最坏的情况下我可以进行字符串操作来修复字符串,但这似乎有点荒谬。
最佳答案
为了得到更好的东西
syntax error, unexpected T_ID
而不是
%token T_ID
做
%token T_ID "identifier"
消息将是
syntax error, unexpected identifier
在 $end
的特定情况下,执行以下操作:
%token T_EOF 0 "end of file"
如果您需要国际化支持,请使用_(...)
:
%token T_ID _("identifier")
%token T_EOF 0 _("end of file")
自 Bison 3.6 起,您可以进一步自行生成错误消息。解析器将为您提供预期标记和意外标记的列表,您将负责伪造传递给 yyerror 的消息。请参阅The Syntax Error Reporting Function yyreport_syntax_error
在文档中了解更多详细信息。
关于bison - 更改 Bison 错误消息的格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11164854/