[antlr-interest] v3: going through the AST

Dieter Frej dieter_frej at gmx.net
Tue Aug 15 23:29:57 PDT 2006


Hi,

I added output=AST; to the options section of the latest java.g grammar in
order to get the AST. This makes a new method public Object getTree()
available.
What I would like to get was something like the tree structure visualised
in antlrworks. So I tried the following:

CommonTree tree = (CommonTree) parser.compilationUnit().getTree();
String[] names = parser.getTokenNames();
int childCount = tree.getChildCount();
for(int i = 0; i < childCount; ++i) {
    CommonTree ct = (CommonTree) tree.getChild(i);
    System.out.println("childcount: " + ct.getChildCount());
    System.out.println("tree type: " + ct.getType() + "|" + names[ct.getType()]);
    System.out.println("text: " + ct.token.getText());
    System.out.println("line: " + ct.getLine());
    System.out.println("toString: " + ct.toString());
    System.out.println("toStringTree: " + ct.toStringTree());
    System.out.println();
}

but tree.getChildCount() is always 1. It is more like a linked list than a
tree. 

Any suggestions? Or is my code wrong? (because of the lack of examples
that could also be the case ;-)

- Didi
-- 


"Feel free" – 10 GB Mailbox, 100 FreeSMS/Monat ...
Jetzt GMX TopMail testen: http://www.gmx.net/de/go/topmail


More information about the antlr-interest mailing list