[antlr-interest] Debugging tree grammars

John Ridgway john at jacelridge.com
Mon Nov 12 14:23:56 PST 2007


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)
     |   ^(SCALAR_DEFINITION modifiers type identifier  
variableInitializer)
     |   ^(ARRAY_DEFINITION modifiers type '['+ identifier  
variableInitializer?)
     |   ^(GENERIC_CONSTRUCTOR_DEFINITION modifiers typeParameters  
identifier formalParameters (throwsPhrase)?)
     |   ^(GENERIC_METHOD_DECLARATION modifiers typeParameters type  
'['* identifier formalParameters (throwsPhrase)?)
     |   ^(GENERIC_METHOD_DEFINITION modifiers typeParameters type  
'['* identifier formalParameters (throwsPhrase)? methodBody)
	|	interfaceDeclaration
	|	classDeclaration
     )
         {System.err.println("leaving classBodyDeclaration");}
	;

Running it yields:

classDeclaration
modifiers
leaving modifiers
classBody
classBodyDeclaration
JavaPrinterTree.g: node from after line 7:13 no viable alternative at  
input 'METHOD_DEFINITION'
classBodyDeclaration
JavaPrinterTree.g: node from after line 7:13 no viable alternative at  
input 'METHOD_DEFINITION'
leaving classBody
BR.recoverFromMismatchedToken
JavaPrinterTree.g: node from after line 8:9 mismatched tree node:  
VOID_TYPE expecting <UP>

I don't understand the error, and I don't know how to debug this.  Help?

Peace
- John



More information about the antlr-interest mailing list