[antlr-interest] Debugging ANTLR tree grammar...

srinivasan karthikeyan pitchai srinivasan.karthikeyan.pitchai at oracle.com
Wed Apr 6 09:27:01 PDT 2011


Hi All,

I am a newbie to antlr.  I am in the process of building a tree parser
using the AST rewrite rules that I've coded in the parser grammar file.
However I am running into RecognitionException  when I use the tree
grammar to parse the AST generated using the parser grammar.  I've used
the following code snippet to get more details about the recognition
exception.

==========

@members
{
public String getErrorMessage(RecognitionException e,
                                   String[] tokenNames)
     {
         List stack = getRuleInvocationStack(e, this.getClass().getName());
         String msg = null;
         String inputContext =
             input.LT(-3) == null ? "" : ((Tree)input.LT(-3)).getText()+" "+
             input.LT(-2) == null ? "" : ((Tree)input.LT(-2)).getText()+" "+
             input.LT(-1) == null ? "" :
((Tree)input.LT(-1)).getText()+">>>"+
             ((Tree)input.LT(1)).getText()+"<<<  "+
             ((Tree)input.LT(2)).getText()+" "+
             ((Tree)input.LT(3)).getText();
         if ( e instanceof NoViableAltException ) {
            NoViableAltException nvae = (NoViableAltException)e;
            msg = " no viable alt; token="+e.token+
               " (decision="+nvae.decisionNumber+
               " state "+nvae.stateNumber+")"+
               " decision=<<"+nvae.grammarDecisionDescription+">>";
         }
         else {
            msg = super.getErrorMessage(e, tokenNames);
         }
         return stack+" "+msg+" context=..."+inputContext+"...";
     }
     public String getTokenErrorDisplay(Token t) {
         return t.toString();
     }
}


===========
The above code produces error message like the following:-

==

Walker.g: node from line 3:0 [start, startStatement, dml, query_term,
query_expression, query, from_clause, from_list, from_term,
table_list_item] mismatched tree node: join expecting<UP>  context=...UP
>>>join<<<  DOWN TABLE_OR_VIEW_NAME...

==

However I am unable to understand this error message.

It will be great if you can guide me in understanding the basic approach
to tree grammar debugging.  Any tips, pointers in debugging the tree
grammar would be great.  FYI.  I am using antlr3.1.3 and I've
antlrworks1.3.1 installed.

Thanks,
Vasan



More information about the antlr-interest mailing list