将错误产生式添加到语法中的策略是什么?
通常如何添加错误产生式?我遇到的问题是我的错误产生式太浅:当解析器开始弹出语句中错误的状态时,它会弹出,直到它遇到它所在部分的错误产生式,并打印出无效错误信息。
向每个非终结符添加一些描述性错误产生是一个好主意吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误产生式是从错误中恢复以尝试继续处理输入,而不是打印合理或有用的错误消息。因此,它们应该用在语法中您可能可以正确识别和重新同步输入流的位置。例如,如果您的语言由一系列以
;
字符结尾的构造组成,则良好的错误生成类似于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 likeconstruct: error ';'
, which will recover from errors in aconstruct
(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.