[antlr-interest] AST -> text

hendrik brower hendrik.brower at gmail.com
Sat Nov 7 08:35:43 PST 2009


I'm interested in parsing a stream into an AST, then manipulating the AST by
removing, adding, or replacing nodes.  When replacing or adding nodes, I was
plannig
on creating the new or replacement nodes by passing a replacement stream
through an
appropriate parsing routines and then replacing or adding the generated AST
to the
original AST.

With the modified AST, I'd like to be able to extract the text stream that
reflects
the manipluated streams. Since I'm forming the AST with multiple streams
through
the addition or replacement of nodes generated with distinct streams and
parsing
runs, token streams will be referencing different source streams.  I'm
assuming
that token streams property reference their source streams and so by taking
this
approach, I'm essentially using the AST to reference slices of text from
varous
streams, so generating a text stream seems feasible enough.  Though, I
haven't
figured out how to do this or if the functionality was actually present in
antlr
at this point.

If it is, can someone offer a few pointers to help me get started?

If not, can anyone offer some alternatives?  I suppose I chose this approach
because
I was going to use the nodes of the AST elsewhere within my program and I
didn't
want to re-parse an entire file because it would be slower and it would
sever any
references to the AST nodes that I'm referencing.  It is of course possible
to
add an intermediate layer so that the rest of the code references something
that
"looks up" the correct node, though it adds a substantial level of
complexity that
I can avoid if I take the node manipulation route.

Also, assuming that the manipulation of the AST and extraction of its text
is feasible,
I suspect that the primary issue that I will encounter is propertly
associating hidden
channel data with inserted, removed, and/or replaced elements. For example,
if the
AST represented a c source file and a particular node that was being
replaced
represented a function (the definition, arguments, and body), I'd have to
decide
whether or not to replace the comments before the function, or to leave them
to
preceed the replacement text.  Does anyone have any suggestions to help me
get started with property identifying and manipulation the surrounding
hidden
channel data?

Thanks in advance!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20091107/8d7e934e/attachment.html 


More information about the antlr-interest mailing list