[antlr-interest] line number problem. Please help!

tdknghi <nk.truong at student.qut.edu.au> nk.truong at student.qut.edu.au
Mon Feb 17 15:00:20 PST 2003


  Hi all,

I would like to keep track the line number and display it in Java AST 
tree. I wrote my own custom AST node. Everything work fine except 
one problem. the line number always reset at the first token of any 
variable declaration, all other tokens in the same line have correct line 
number. For example:
   int i = 5; (the line number of token "int" always reset to 0).

I have done debug and look through the grammar. I have found the 
follow section has changed the line number of the counter in the java.g 
grammar.
/-----------------------------------------------------------------
   variableDefinitions[AST mods, AST t]
{System.out.println("b " + ((LineAST)t).getText() + " " + ((LineAST)
t).getLine());}
	:	variableDeclarator[getASTFactory().dupTree
(mods),
						   
getASTFactory().dupTree(t)]
{System.out.println("before 1 " + ((LineAST)t).getText() + " " + 
((LineAST)t).getLine());}				
		   
		(	COMMA!
			variableDeclarator[getASTFactory
().dupTree(mods),
						
	   getASTFactory().dupTree(t)]
		{System.out.println("before 2 " + ((LineAST)
t).getText() + " " + ((LineAST)t).getLine());})*
	;

/** Declaration of a variable.  This can be a class/instance variable,
 *   or a local variable in a 
 * It can also include possible initialization.
 */
variableDeclarator![AST mods, AST t]
{System.out.println("after " + ((LineAST)t).getText() + " " + ((LineAST)
t).getLine());	}
	:	id:IDENT d:declaratorBrackets[t] v:varInitializer
		{
			#variableDeclarator = #(#
[VARIABLE_DEF,"VARIABLE_DEF"], mods, #(#[TYPE,"TYPE"],d), id, 
v);
System.out.println("$" + id.getText());	
//System.out.println("after " + ((LineAST)t).getText() + " " + ((LineAST)
t).getLine());			
		} 
	;

/------------------------------------------------
In my understanding, the variableDefinitions rule calls the 
variableDeclarator rule and past all neccessary information to construct 
the tree. I have found the line number of the t ast is changed in the 
variableDeclarator rule. Do you have any idea why it happens and how 
to fix it? 

Thanks for all your help.

Regards
MiMi


 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 



More information about the antlr-interest mailing list