bison - 更改 Bison 错误消息的格式?

标签 bison

有什么方法可以更改 Bison 错误消息的格式吗?例如,不要说 syntax error, unexpected T_ID, expected ';' or T_IMPORTunexpected 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/

相关文章:

c++ - Bison malloc 内存损坏

windows - 无法识别的 %option bison-bridge with flex 2.5.4

c - 是否可以从 Bison 语法操作更改弹性状态?

c - RegEx 仅匹配字符常量和特定转义序列

parsing - 为什么我在Jison中的语法错误不是 “propagated”?

c - Bison解析全局变量和函数

c++ - 清除 Bison 生成的解析器的解析器状态

c - 错误 : expected specifier-qualifier-list before ‘TR’

c - Flex/Bison EOF 从标准输入与文件传播

c++ - 使用 g++、bison 和 flex 进行编译时,yyparse() 中未定义对 `yylex' 的引用