Tolerating malformed statements with ANTLR (e.g., for code-completion)
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
开发者_如何学Go |
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.
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.
精彩评论