How to get ANTLR to output hierarchical ASTs?

Assuming your tree is just a 1 dimensional list of nodes, here's how you can create parent/sibling hierarchy.

Assuming your tree is just a 1 dimensional list of nodes, here's how you can create parent/sibling hierarchy: In ANTLR there are two operators for AST creation:! Excludes the node (token) from the (sub)tree; ^ makes a node the root of a (sub)tree. When no operator is provided, the nodes/tokens are added as children of the current root.

This is probably what has happened to you: all you see is a one dimensional list of nodes/tokens. An example: grammar Exp; options {output=AST;} // ... some rules ... addition : Integer '+'^ Integer ';'! ; Integer : '0' | '1'..'9' '0'..'9'* ; The addition rule will create the following tree for the expression 6+9;: + / \ / \ 6 9 As you can see: the + is the root (it has ^ after it), the numbers are tokens (they have no operator) and the semi colon is excluded (it has a!

After it). For a detailed explanation, see chapter 7, Tree Construction, from The Definitive ANTLR Reference. I highly recommend you get a hold of a copy.

The question whether you should start from scratch is for you to decide. I'd just start with an empty grammar file and gradually add rules to it checking frequently to see if all works. Simply sprinkling some tree operators in an existing grammar may be pretty hard to debug: especially if you're not too familiar with ANTLR.

Best of luck!

Ok, that doesn't sound half as bad as I was afraid of. Thanks for the help! – RCIX Dec 23 '09 at 23:09.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions