Dashboard > ANTLR 3 > 2007 > July > 27 > Finished tree grammar AST construction
  ANTLR 3 Log In | Sign Up   View a printable version of the current page.  
  Finished tree grammar AST construction
Added by Terence Parr, last edited by Kay Röpke on Jul 27, 2007  (view change)
Labels: 

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) ;

Is this in the current Antlrworks version?

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

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? 

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

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?

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.

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

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

  July 2007
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        

ANTLR 3.0.1 bug fix release

Site powered by a free Open Source Project / Non-profit License (more) of Confluence - the Enterprise wiki.
Learn more or evaluate Confluence for your organisation.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.1 Build:#806 May 06, 2007) - Bug/feature request - Contact Administrators