[antlr-interest] Can @init code throw Java exceptions?

Vaclav Barta vbar at comp.cz
Thu Jul 19 11:49:36 PDT 2007


Hi,

I'm trying to construct an XML document in a tree grammar, and getting an 
interesting error. I put the XML doc initialization into the @init block of 
the topmost rule:

sourceFile
@init {
	DocumentBuilderFactory factory =
		DocumentBuilderFactory.newInstance();
	DocumentBuilder builder = factory.newDocumentBuilder();
	document = builder.newDocument();
}
	: assignments rules
	;

But that generates illegal Java, because the newDocumentBuilder is declared as 
throwing javax.xml.parsers.ParserConfigurationException while the enclosing 
sourceFile method isn't. I suppose I can put my own try...catch inside and 
wrap everything in RuntimeException, but that seems rather inelegant - is 
there a better way?

	Bye
		Vasek


More information about the antlr-interest mailing list