Tolerating malformed statements with ANTLR (e.g., for code-completion)

╄→гoц情女王★ 提交于 2019-12-08 04:05:35

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/1723275/tolerating-malformed-statements-with-antlr-e-g-for-code-completion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!