Dashboard > ANTLR 3 > Browse Space > News from
  ANTLR 3 Log In | Sign Up   View a printable version of the current page.  
  News from Jul 27, 2007
  2007/07/27
Last changed: Jul 27, 2007 15:50 by Kay Röpke
Labels: trees, rewriting

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) ;
Posted at 27 Jul @ 2:08 PM by Terence Parr | 8 comments

  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        

Aug 16, 2007

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