[antlr-interest] Debugging tree grammars

Jim Idle jimi at temporal-wave.com
Mon Nov 12 14:42:52 PST 2007


You probably have worked out that your grammar does not reflect the tree 
you are producing? 

Looks, from your indenting, like you have

^(METHOD_DEFINITION ^(MODIFERS ...

So, does your modifiers rule reflect that? If so, then keep going 
through each rule until you see that your tree products a sub-tree but 
your tree grammar is not expecting that, or the other way around. 

I think that you can debug tree grammars if you generate it in debug 
mode, then run it external to ANTLRWorks, then use ANTLRWorks remote 
debugger. But you are likely to find this easy enough by hand as it is 
(almost ;-), always just that you are making nodes where your tree 
parser isn't expecting them, or the other way around. You could of 
course be missing a chunk or two in the tree parser :-)

However, all that said, looking at your rules you seem to have lots of 
versions of ^(METHOD_DEFINITION x y z) that look like they are getting 
in your way. If you really want these to be recognized differently in 
the tree parser, then it would probably be best to write them out as 
different nodes and not just different METHOD_DEFINITION nodes, or make 
your ^(METHOD_DEFINITION .. recognize each variant.

Jim

-----Original Message-----
From: John Ridgway [mailto:john at jacelridge.com] 
Sent: Monday, November 12, 2007 2:24 PM
To: antlr-interest
Subject: [antlr-interest] Debugging tree grammars

Friends -
How do I debug a tree grammar?  I started with the Java 1.5 grammar,  
and modified it to produce an AST.  I'm hoping that the AST I'm  
generating is general enough that others can use it if they like.   
I'm then writing a tree grammar to look at the AST and produce  
equivalent Java.  Together they make a not-partiucularly-pretty- 
printer, since all of the comments get dropped, but it gives me some  
belief that the AST is good.

Here's the problem.  I parse the Java and return an AST; I take the  
tokens from the AST and feed them into the tree grammar that I  
created.  I get the message:

JavaPrinterTree.g: node from after line 7:13 no viable alternative at  
input 'METHOD_DEFINITION'

but I cannot see why I'm getting this error -- the printed AST  
includes the following fragment:
   CLASS
     MODIFIERS
       public
     Test
     CLASS_BODY
       METHOD_DEFINITION
         MODIFIERS
           public
           static
         VOID_TYPE
         main
         FORMAL_PARAMETERS
           FORMAL_PARAMETER
             VARIABLE_MODIFIERS
             ARRAY_OF
               String
             args
         throws
           Exception
         BLOCK

and the tree grammar includes the following:

classBody
	:	{System.err.println("classBody");}
         ^(CLASS_BODY classBodyDeclaration*)
	    {System.err.println("leaving classBody");}
     ;
	
interfaceBody
	:	{System.err.println("interfaceBody");}
         ^(INTERFACE_BODY interfaceBodyDeclaration*)
	;

classBodyDeclaration
	:	{System.err.println("classBodyDeclaration");}
     (   {System.err.println("here");}^(STATIC_BLOCK block)
     |   ^(DYNAMIC_BLOCK block)
	|	^(CONSTRUCTOR_DEFINITION modifiers identifier 
formalParameters  
formalParameters throwsPhrase?)
     |   ^(METHOD_DECLARATION modifiers VOID_TYPE identifier  
formalParameters throwsPhrase?)
     |   ^(METHOD_DECLARATION modifiers type '['* identifier  
formalParameters throwsPhrase?)
     |   {System.err.println("here");}
         ^(METHOD_DEFINITION  modifiers VOID_TYPE identifier  
formalParameters (throwsPhrase)? methodBody)
     |   ^(METHOD_DEFINITION modifiers type '['* identifier  
formalParameters (throwsPhrase)? methodBody)




More information about the antlr-interest mailing list