Antlr3 C 运行时相当于抛出错误报告异常的是什么
我非常熟悉 Antlr 2.X 和 Antlr 3.1.X CSharp 和 python 目标。
然而,我现在被迫在项目中使用 Antlr 3 C 目标。
我的问题是如何报告语法或树语法中的错误。
考虑一个与标记匹配的规则,我们将其放入映射中。我们希望确保令牌是唯一的。通常,如果令牌已经在映射中,我会抛出异常,并在解析器之外捕获异常以报告错误。
Antlr C 运行时相当于以下规则是什么?
token_match: ID
{
if(mp.find($ID.Text))
throw std::exception("Non unique token found");
}
I'm pretty familiar with Antlr 2.X and the Antlr 3.1.X CSharp and python targets.
However I'm now forced to use the Antlr 3 C target for a project.
My issue is how do I report errors in my grammar, or tree grammar.
Consider a rule that matches a token and we put it into a map. We want to ensure the token is unique. Normally I'd throw an exception if the token was already in the map and catch the exception outside of hte parser to report the error.
What is Antlr C runtime equivalent of the following rule?
token_match: ID
{
if(mp.find($ID.Text))
throw std::exception("Non unique token found");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议使用您自己的方法来显示语义错误。但是,如果您为此需要留在 ANTLR 中:
如果您想创建自定义异常类型,首先您必须创建自己的错误处理程序。查看 antlr3baserecognizer.c:1000 的原始版本。
然后编写一些函数或规则代码来处理异常结构。查看antlr3baserecognizer.c:325来设置您的异常。
接下来,当您检测到错误时,您需要实际抛出异常。我认为执行此操作的唯一方法是覆盖添加到代码中并从规则中调用它的mismatch()。
最后告诉 ANTLR 使用你的新函数。
I'd recommend using your own methods to display semantic errors. But if you need to stay in ANTLR for this:
First you have to create your own error handler if you want to create custom exception types. Look at antlr3baserecognizer.c:1000 for the original.
Then code some function or rule code to process an exception structure. Look at antlr3baserecognizer.c:325 to setup your exception.
Next you need to actually throw your exception when you detect the error. I think the only way to do this is to override mismatch() adding in your code and calling it from your rule.
Finally tell ANTLR to use your new functions.