[antlr-interest] Tree gramar, StringTemplate, Java target conditionals problem

Bartosz Dobrzelecki bartosz at epcc.ed.ac.uk
Fri Jul 6 02:25:56 PDT 2007


Hello!

I have the following rule in my tree grammar:

selExpr	:	^(COL expr ID?)
		-> template(expression={$expr.st},alias={$ID.text})
		"<expression><if(alias)> AS <alias><endif>" ;

Antlr will generate the following Java code:

{
	retval.st = new StringTemplate(templateLib,
		"<expression><if(alias)> AS <alias><endif>",
		new STAttrMap()
		.put("expression", expr2.st)
		.put("alias", ID3.getText()));
}

This of course will not work if (alias) is not set - a null pointer 
exception will be thrown.

What I would expect to see in the code is sth like:

{
	STAttrMap map = new STAttrMap().put("expression", expr2.st)
	if(ID3 != null) map.put("alias", ID3.getText());

         retval.st = new StringTemplate(templateLib,
		"<expression><if(alias)> AS <alias><endif>", map);
}



Am I doing something wrong ?

Cheers,
Bartek


More information about the antlr-interest mailing list