使用 ANTLR 容忍格式错误的语句(例如,用于代码完成)

发布于 2024-08-11 07:49:18 字数 943 浏览 3 评论 0原文

我有一个简单 DSL 的 ANTLR 语法,如果没有语法错误,一切都会顺利进行。然而,现在我需要支持自动完成机制,我需要从对属性、函数等执行基本类型检查的树语法中获得可能的完成。

问题是,ANTLR 没有报告语法错误本地语句级别,但在解析树的更上方,例如程序函数级别。因此,我并不是

             program
                |
             function   
            /   |    \
           /    |     \
       stat   hosed   stat

在树的顶部得到垃圾节点,而是因为与 statement 规则匹配失败而“冒泡”并阻止了 function匹配规则。

有没有办法编写一个具有“包罗万象”子句的规则来吃掉意外的令牌?

我正在考虑这样的事情:

statement
    : var_declaration
    | if_statement
    | for_loop
    | garbage
    ;

garbage
    : /* Match unexpected tokens, etc. (not actual statements, or closing
         parens, braces, etc.).  Maybe just consume one input token and let
         the parser try again? */
    ;

AST中可能有任意数量的垃圾节点,但是垃圾之前(最好是之后)的一切都应该是理智的。

我将不胜感激任何提示/建议/指示/等。我正在使用 ANTLR v3,Java 目标。

I have an ANTLR grammar for a simple DSL, and everything works swimmingly when there are no syntax errors. Now, however, I need to support an auto-completion mechanism, where I need to get possible completions from my tree grammars that perform basic type-checking on attributes, functions, etc.

The problem is, ANTLR isn't reporting syntax errors at the local statement level, but farther up the parse tree, e.g., at the program or function level. Hence, instead of an AST that looks like

             program
                |
             function   
            /   |    \
           /    |     \
       stat   hosed   stat

I get garbage nodes across the top of the tree, as a failure to match the statement rule "bubbles up" and prevents the function rule from matching.

Is there a way to write a rule that has a "catch-all" clause to eat unexpected tokens?

I'm thinking of something like:

statement
    : var_declaration
    | if_statement
    | for_loop
    | garbage
    ;

garbage
    : /* Match unexpected tokens, etc. (not actual statements, or closing
         parens, braces, etc.).  Maybe just consume one input token and let
         the parser try again? */
    ;

There may be any number of garbage nodes in the AST, but everything before (and preferably after) the garbage should be sane.

I'd appreciate any hints/suggestions/pointers/etc. I'm using ANTLR v3, Java target.

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

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

发布评论

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

评论(1

梦里人 2024-08-18 07:49:18

看看 http://www.antlr.org/ wiki/display/ANTLR3/Error+reporting+and+recovery

顺便说一句:如果你的目标是 eclipse,你应该看看 xtext (http://www.eclipse.org/Xtext/) - 它基于 ANTLR 3,并生成一个带有语法高亮和代码辅助的漂亮编辑器。

Take a look at http://www.antlr.org/wiki/display/ANTLR3/Error+reporting+and+recovery

BTW: If you're targeting eclipse, you should look at xtext (http://www.eclipse.org/Xtext/) - it's based on ANTLR 3 and generates a nice editor with syntax hilighting and code assist.

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