[antlr-interest] Keeping trees around
Steve Ebersole
steve at hibernate.org
Sun Aug 8 09:34:34 PDT 2010
Most of the uses of Antlr I have seen show temporary use of ASTs. I
mean temporary here in that usually the AST is built, used and discarded
in a single step.
I have a particular usage where I am generating SQL statements and
really I could pre-build large chunks of the resulting SQL AST up-front.
First I am wondering how kosher this is. Considering its not a pattern
I see discussed very often I am a little cautious. The reason I
contemplate this approach even is because it would seem much more
efficient (seem because i think i would end up needing to clone the
pre-built AST fragments to insulate the master fragment from contextual
state changes).
If the general approach itself is sound, I wonder if anyone has taken on
such an approach and had any heads-ups or pointers.
As an example of what I mean, imagine you need to build a SQL SELECT
LIST. The usual approach I have seen is to build the AST on the fly
from the text representations of each select expression:
CommonTree selectList = new CommonTree( SELECT_LIST, "{select list}" );
int count = 0;
for ( String columnName : someListOfColumnNames ) {
CommonTree column = new CommonTree( COLUMN, columnName );
CommonTree alias = new CommonTree( ALIAS, "y" + count++ + "_" );
CommonTree selectExpr = new CommonTree( SELECT_EXPRESSION, "{select
expression}" );
selectExpr.addChild( column );
selectExpr.addChild( alias );
selectList.addChild( selectExpr );
}
More I am thinking a pattern like:
CommonTree selectList = new CommonTree( SELECT_LIST, "{select list}" );
int count = 0;
for ( CommonTree column : someListOfColumns ) {
CommonTree alias = new CommonTree( ALIAS, "y" + count++ + "_" );
CommonTree selectExpr = new CommonTree( SELECT_EXPRESSION, "{select
expression}" );
selectExpr.addChild( column );
selectExpr.addChild( alias );
selectList.addChild( selectExpr );
}
Which is a trivial change here specifically. More I am worried about
handling more complex (possible nested) tree structures in this manner.
Any thoughts/pointers would be appreciated.
--
Steve Ebersole <steve at hibernate.org>
http://hibernate.org
More information about the antlr-interest
mailing list