[antlr-interest] insert after tree?
Vaclav Barta
vbar at comp.cz
Thu Jan 17 05:07:14 PST 2008
Hi,
sorry for the late answer, but I had to publish the software first; :-)
for programmatic transformations of SQL, I heartily recommend MacroScope
(http://macroscope.sourceforge.net/). It's in C#, so probably not
directly usable in your case, but IMHO still useful to see how your
problem can be solved (in an ANTLR-nonspecific way - it was my first
ANTLR project, so the architecture is quite conservative). ANTLR is used
strictly for parsing. Its actions build an SQL syntax tree (not ANTLR
AST, but a tree of custom typed objects - what MS calls an "object
model"), and tree transformations (like adding an ESCAPE clause) are
implemented separately, normally with visitors. I don't doubt ANTLR
gurus could do tree transformations with much less code, but I don't
regret starting simple - had enough ANTLR on my plate as it was...
Bye
Vasek
Geoff hendrey wrote:
> It's the first case you mention below.
>
> More specifically, I need to recognize SQL "LIKE" expression trees from
> within a SQL statement and add the text "ESCAPE '~'" after the LIKE
> expression's tree. I have this in my grammar file.
>
> likeExpression
> :(concatenationExpression 'LIKE' concatenationExpression) ->^('LIKE'
> concatenationExpression+ ESCAPE) ;
>
> I added the imaginary ESCAPE token, but I have not had any luck in my
> Java trying to replace the imaginary escape token with the real text
> "ESCAPE '~'". If only I could add a *real* token through my rewrite rule
> above , I would be in business.
>
> On PAge 176 of Terence's book it shows how to construct an imaginary
> token with from a real token, but I don't have a real token to use (as
> far as I can tell), which means I can't use the TokeRewriteStream with
> the imaginary token, as far as I can tell.
>
>
> -geoff
>
>
>
>
> ----- Original Message ----
> From: Harald M. Müller <harald_m_mueller at gmx.de>
> To: Geoff hendrey <geoff_hendrey at yahoo.com>; antlr-interest at antlr.org
> Sent: Sunday, January 6, 2008 9:04:04 AM
> Subject: RE: [antlr-interest] insert after tree?
>
> Sorry - I don't understand your question:
> - You have an AST?
> - you want to output it again as text?
> - but after some sub-AST of it, you want to have written some additional
> text?
>
> or
>
> - You have an AST
> - you want to insert a node behind a certain sub-AST
> so that later tree parsers/walkers will see that additional node
>
> Is it one of these two - or something differently?
>
> Sorry for being a little bit slow ...
> Regards
> Harald
>
> ------------------------------------------------------------------------
> *From:* antlr-interest-bounces at antlr.org
> [mailto:antlr-interest-bounces at antlr.org] *On Behalf Of *Geoff hendrey
> *Sent:* Sunday, January 06, 2008 9:22 AM
> *To:* antlr-interest at antlr.org
> *Subject:* [antlr-interest] insert after tree?
>
> From my Java program, I want to insert text after a *tree* not
> after a token.
>
> I guess I am looking for a "TreeRewriteStream" as opposed to a
> TokenRewriteStream.
>
> As a workaroud I tried creating an imaginary token in my grammar
> file and using it to identify the rightmost side of a tree. Like this:
>
> a 'func' a ->^('func' a+ IMAGINARY)
>
> But then as I found out, the IMAGINARY Token doesn't seem to work
> with TokenRewriteStream.insertAfter.
>
> Can anybody advise on how I can insert text immediately after a
> particular tree? (The text is static so perhaps there is a way to do
> it from the grammar file...)
>
>
More information about the antlr-interest
mailing list