Finished tree grammar AST construction

Skip to end of sidebar Go to start of sidebar
Skip to end of metadata
Go to start of metadata

Well, I was wrong. Building ASTs from tree grammars was not as easy as I thought. There were a lot of little changes everywhere and Kay Roepke and I had to examine all of the interfaces to figure out where each method best fit. Previous entry Rewrite Tree Node Stream Design was way too simple. There are lots complications, but I have it working nicely. I added a whole bunch of unit tests. The rewrites even work with the new heterogeneous tree node stuff. I make copious notes in the README.txt file, but I'm not sure if the other target developers can see into my dev branch. Anyway, we'll have to say this for a 3.1 release.

The basic idea is that, given no instructions to the contrary, ANTLR tree grammars will build a copy of the input tree; nodes for node. Just turn on output=AST:

grammar TP;
options {output=AST;}
a : ID ;

Now, that is pretty inefficient if all you want to do is tweak a small subtree within a very large tree. Use the rewrite mode and then rewrite rules alter the tree matched for that alternative. No implicit tree construction is done. Only rewrite rules alter the tree. Rewrites do, however, set the return tree for that rule.

grammar TP;
options {output=AST;}
file : ... ;
...
/** Alter the ASSIGN node to be SET */
assign : ^(ASSIGN ID INT) -> ^(SET ID INT) ;
  1. Aug 06, 2007

    Is this in the current Antlrworks version?

  2. Aug 06, 2007

    Nope. It will be in 3.1; might do an early access soon.

  3. Aug 06, 2007

    If I understand correctly, then you can run this code now, but it wastes time and memory copying the tree, and the new version will change the tree in place.

    Is that correct?

    Also when do you expect 3.1 to come out? 

  4. Aug 06, 2007

    Actually, no tree building from tree grammar at the moment. (wink) 3.1 will have it with two modes: build new and rewrite existing.

  5. Aug 16, 2007

    Could you expand on this a bit?  How would I set up a rewrite?  Would I have to write a tree grammar that describes the entire tree and then only include rewrite rules for those parts of the tree that I want to modify?  Or, do I only write rules that describe enough of the tree so that I rewrite only the parts that I want to?

    I'm interested in how I would make use of this to transform lang A AST to lang B AST, using a series of N transformations.  Would I have to have a tree grammar that completely describes my tree at each of the N stages?

  6. Aug 16, 2007

    At the moment, you'd still need whole tree unless you use the TreeWizard to find subtrees and then pass to a grammar rule. Messy. Am planning on a new syntax to list a series of rules like Andy Tripp does.

  7. Apr 04, 2008

    Perhaps I understand uncorrectly: if I build a language A AST(not simple AST), I want to transform to language B AST, antlr3.0.1 don't support, i can't use tree walker or rewrite rule to do it automatically, I must write code myself? Is that true?

       If it's true, when antlr3.1 comes out?

       3ks a lot

  8. Apr 04, 2008

    true. 3.1 should be out in a month or so.