[antlr-interest] Bug using rule action?

Cameron Esfahani dirty at apple.com
Wed Jun 13 16:19:55 PDT 2007


I'm trying to add an action to one of my rules and I'm seeing some  
strange behavior:

grammar Test;

options {
	output = AST;
	ASTLabelType = CommonTree;
}

tokens {
	SIZE_DEFAULT;
	SIZE_8;
}

r
	:	number_size+ EOF
	;

SIZE_8_QUALIFIER
	:	'.' 'U'? 'B'
	;

size_qualifier
	:	SIZE_8_QUALIFIER		-> SIZE_8
	|							-> SIZE_DEFAULT
	;

number_size
	:	num = NUMBER size = size_qualifier {
			// Validate that NUMBER fits within the size qualifier.
			System.out.printf( "!\%s, !\%s\n", $num.toString(), $size.getTree 
().toString() );
		} -> size_qualifier NUMBER
	;

NUMBER
	:	'-'? ( '0' | '1'..'9' '0'..'9'*)
	;

If I try to run this through ANTLR, I get an error:

error(116): Test.g:30:39: unknown attribute for rule size_qualifier:  
getTree

When I examine TestParser.java, I see that "$size" is still there.   
If I change the action, to remove the $ character, then it works:

			System.out.printf( "!\%s, !\%s\n", $num.toString(), size.getTree 
().toString() );

Why do I have to do this?  How come $num correctly causes a local  
variable named num to be created, but not $size?

Cameron Esfahani
dirty at apple.com

"All that is necessary for the triumph of evil is that good men do  
nothing."

Edmund Burke



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070613/67bb1e23/attachment-0001.html 


More information about the antlr-interest mailing list