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

Michael Bedward michael.bedward at gmail.com
Wed Apr 6 18:07:33 PDT 2011


Hi Vasan,

I find the most useful debugging methods are often the simplest ones.
Printing the AST with toStringTree() is always the first thing I do
because so often that helps to spot the problem straight away. In your
case the walker is looking for an UP node, ie. the end of a sub-tree,
and that sort of thing is usually obvious in the toStringTree()
output.

Michael

On 7 April 2011 02:27, srinivasan karthikeyan pitchai
<srinivasan.karthikeyan.pitchai at oracle.com> wrote:
> 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
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>


More information about the antlr-interest mailing list