[antlr-interest] Choosing AST rewrite based on test

Bill Andersen andersen at ontologyworks.com
Tue Oct 13 08:39:02 PDT 2009


Hi All

I have the following rule in a tree grammar (BTW, rewrite = true  
here).  Both AS_TEXT and AS_NAME are imaginary tokens.

text_block
	: ^(AS_TEXT text_option* name? phrase+ )	
	  -> ?????
	;

what I want for the ????? part is the rewrite

	^(AS_TEXT name phrase+)

if the optional name was present in the input tree, and

	^(AS_TEXT { <generated-name> } phrase+)

if no name was present in the input tree.  One variant I tried was

text_block
	: ^(AS_TEXT text_option* name? phrase+ )	
	  -> ^(AS_TEXT { ($name.tree == null) ? pm.generateName() :  
$name.tree } phrase+)
	;

where pm is a helper class instance and generateName looks like

CommonTree generateName() {
   String name = ....
   return new CommonTree( new CommonToken(AS_NAME, name) );
}

First question is, is this a good approach to "filling in a blank" in  
a rewritten tree?  If not, please advise.  If so, I'll post another  
message showing what I did and the null pointer exception that results.

Thanks in advance!

	.bill

Bill Andersen
Ontology Works, Inc. (www.ontologyworks.com)
3600 O'Donnell Street, Suite 600
Baltimore, MD 21224
Office: +1.410.675.1201
Cell: +1.443.858.6444
Fax: +1.410.675.1204






More information about the antlr-interest mailing list