ANTLR:构建失败时如何提取错误消息

发布于 2024-10-31 02:32:36 字数 1225 浏览 0 评论 0原文

我在 ANTLR 中构建了我的词法分析器和解析器,它们工作得非常好,当用户代码无法解析时,它会向 STDERR 输出有用的错误消息,显示确切的行号。 。

问题是,我需要提取此信息,以便在 Eclipse 编辑器中的正确位置显示错误消息,但除了 STDERR 之外,它似乎在任何地方都不可用 我基本上是在寻找某种 myParser.getErrorMessages()。

有人遇到过这个问题的解决方案吗?

我找到了下面的链接,但是只有当用户代码部分解析时才有效(即我们仍然得到 AST)。当它完全失败时,你就无法恢复一棵树。 http:// tech.puredanger.com/2007/02/01/recovering-line-and-column-numbers-in-your-antlr-ast/

我还在官方 ANTLR FAQ 中找到了这个确切的问题......但我真的不明白他的解决方案。有人可以帮我翻译一下吗?我没有使用他提到的任何类,他正在谈论 v4(尚未发布)。 http://www.antlr.org/wiki/display/ANTLR3/Pattern+for+returning+errors+from+ANTLR+in+data+structs%2C+not+STDERR

我的代码如下所示:

FileInputStream fis = new FileInputStream("UserCode.txt");
ANTLRInputStream input = new ANTLRInputStream(fis);
MyLexer lexer = new MyLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
MyParser parser = new MyParser(tokens);
CommonTree tree = (CommonTree)parser.flow().getTree();
MyAST ast = new MyAST(tree);

I've built my lexer and parser in ANTLR and they work really well in the sense that when user code fails to parse, it outputs useful error messages to STDERR, showing the exact line no. etc.

The problem is, I need to extract this information in order to display the error messages in my Eclipse editor at the correct positions, but it doesn't seem to be available anywhere except on STDERR. I'm basically looking for some kind of myParser.getErrorMessages().

Has anybody come across a solution to this?

I found the below link, however this only works if the user code partially parses (i.e. so we still get an AST). When it fails completely, you don't get a tree back.
http://tech.puredanger.com/2007/02/01/recovering-line-and-column-numbers-in-your-antlr-ast/

I also found this exact question in the official ANTLR FAQ... but I really don't understand his solution. Can anybody translate it for me? I'm not using any of the classes he refers to, and he's talking about v4 (which isn't released yet).
http://www.antlr.org/wiki/display/ANTLR3/Pattern+for+returning+errors+from+ANTLR+in+data+structures%2C+not+STDERR

My code looks as follows:

FileInputStream fis = new FileInputStream("UserCode.txt");
ANTLRInputStream input = new ANTLRInputStream(fis);
MyLexer lexer = new MyLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
MyParser parser = new MyParser(tokens);
CommonTree tree = (CommonTree)parser.flow().getTree();
MyAST ast = new MyAST(tree);

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

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

发布评论

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

评论(1

森林很绿却致人迷途 2024-11-07 02:32:36

请参阅:http://www.antlr.org/wiki/display /ANTLR3/Error+reporting+and+recovery (不确定这些示例是否与 ANTLR v3.2/v3.3 完全兼容,但如果不是,则不应进行太多更改即可使其正常工作)

See: http://www.antlr.org/wiki/display/ANTLR3/Error+reporting+and+recovery (not sure if the examples are fully compatible with ANTLR v3.2/v3.3, but if not, there shouldn't be too many changes to get it working)

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