gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 05:42:07 27 4
gpt4 key购买 nike

有什么方法可以更改 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 开始,您可以更进一步并自己生成错误消息。解析器会为您提供预期的 token 列表,而您将负责伪造传递给 yyerror 的消息。 .见 The Syntax Error Reporting Function yyreport_syntax_error 在文档中了解更多详细信息。

关于bison - 更改 Bison 错误消息的格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11164854/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com