将错误产生式添加到语法中的策略是什么?

发布于 2024-11-05 02:39:01 字数 122 浏览 2 评论 0原文

通常如何添加错误产生式?我遇到的问题是我的错误产生式太浅:当解析器开始弹出语句中错误的状态时,它会弹出,直到它遇到它所在部分的错误产生式,并打印出无效错误信息。

向每个非终结符添加一些描述性错误产生是一个好主意吗?

How are error productions typically added? I'm encountering the issue that my error productions are too shallow: when the parser starts popping states on an error in a statement, it pops until it hits the error production for the section in which it is located, and prints out an invalid error message.

Is it a good idea to just add some descriptive error production to every nonterminal?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

池木 2024-11-12 02:39:01

错误产生式是从错误中恢复以尝试继续处理输入,而不是打印合理或有用的错误消息。因此,它们应该用在语法中您可能可以正确识别和重新同步输入流的位置。例如,如果您的语言由一系列以 ; 字符结尾的构造组成,则良好的错误生成类似于 construct: error ';',它将从错误中恢复在构造(无论是什么)中,通过在输入中向前跳到;并尝试从那里继续。

放置许多错误恢复规则通常是一个坏主意,因为解析器只会恢复到最接近的一个,并且通常是顶层最全局的规则最有可能有用,并且尝试使用更细的粒度只会导致错误由于错误恢复规则无法与输入正确重新同步,因此导致错误级联。

Error productions are about recovering from an error in order to attempt to continue processing the input, not about printing reasonable or useful error messages. Therefor they should be used at points in the grammar where its likely that you can recognize and resynchronize the input stream properly. For example, if your language consists of a sequence of constructs ending with ; characters, a good error production is something like construct: error ';', which will recover from errors in a construct (whatever that is) by skipping forward in the input to a ; and attempting to go on from there.

Putting many error recovery rules is generaly a bad idea, since the parser will only recover to the closest one, and its often the most global ones at the top level that are most likely to be useful and trying to use a finer granularity will just lead to error cascades as the error recovery rules can't resync with the input properly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文