[antlr-interest] Re: AST writer
thrutchy
eric_mahurin at yahoo.com
Fri Jul 30 17:58:35 PDT 2004
--- In antlr-interest at yahoogroups.com, "atripp54321" <atripp at c...> wrote:
> --- In antlr-interest at yahoogroups.com, "thrutchy" <eric_mahurin at y...>
> wrote:
> > Is there a way to write out an AST in the same format as it was
> parsed
> > into the AST without having to write a tree-parser rule for each
> rule
> > of the parser? To do this, I would assume you couldn't put in any
> > "!"/"^" in the rules and the text can't be munged or ignored (i.e.
> > whitespace/comments).
> >
> > I would like to write a language munger that does this:
> >
> > 1. Read language input into an AST
> >
> > 2. Do manipulations to the AST (via a tree-walker or manually)
> >
> > 3. Write the AST out in the same input language
> >
> > I would prefer not to have to specify the language twice - once in
> the
> > parser and once in the writer.
>
> It seems to me that if you avoid "!" and all the other fancy
> ANTLR constructs in your grammar (other than "^", which you do
> need, as Monty points out), then your language output really
> could be just an in-order traversal of the AST.
>
> One thing you might want to try is to compare the java.g that
> compes with ANTLR with the print() method in
> this code that prints out a Java AST:
> http://jazillian.com/antlr/JavaEmitter.java
> The java.g strips out all the "syntax stuff", and this code
> puts it all back in.
>
> Andy
Thanks guys. I've finally had some time to figure out the AST's.
Below is what I came up with if you don't do any of this:
- use "!" or manipulate the text (i.e. remove quotes from strings)
- root anything but the first thing in a rule (you could use imaginary
tokens where needed)
I'm also using the stuff from preserve whitespace example. Here what
I have:
public static void printHiddenBefore(AST a) {
java.util.List before = new ArrayList();
for (
CommonHiddenStreamToken t =
((CommonASTWithHiddenTokens)a).getHiddenBefore() ;
t!=null ;
t = t.getHiddenBefore() )
{
before.add(t.getText());
}
for (int i=before.size()-1;i>=0;--i) {
System.out.print(before.get(i));
}
}
public static void printAST(AST a) {
while (a!=null) {
String s = a.toString();
if (s!=null) System.out.print(s);
for (
CommonHiddenStreamToken t =
((CommonASTWithHiddenTokens)a).getHiddenAfter() ;
t!=null ;
t = t.getHiddenAfter() )
{
System.out.print(t.getText());
}
printAST(a.getFirstChild());
a = a.getNextSibling();
}
}
Just call these two in sequence using the root AST node.
Eric
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/antlr-interest/
<*> To unsubscribe from this group, send an email to:
antlr-interest-unsubscribe at yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
More information about the antlr-interest
mailing list