views:

128

answers:

1

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.

A: 

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.

Scott Stanchfield
I have a catch clause, as described in the wiki, but the exception, understandably, fires on the overarching function rule, not the particular hosed statement. My grammar needs (I assume) to support partial statements, but I haven't managed to hack that together... Xtext is an interesting alternative, though. Thanks for the link.
CapnNefarious