[antlr-interest] AST tree rewrite that can recognize sequence of nodes

Viktor Lioutyi Viktor_Lioutyi at i2.com
Fri Jun 19 13:05:22 PDT 2009


Hi,

I submitted this post before, but in a wrong discussion thread. Sorry for the mess. I am resubmitting this post in a new thread with additional information.


I am writing a prototype of converter from one language (TSL of WinRunner) to another (VB Script of QTP).

One of the tasks includes identification of statement sequence similar to one below

int i = 0;
A[i++] = e1;
A[i++] = e2;
...
A[i++] = eN;

and rewriting it to something like this (the code may be not exact, but the idea should be clear)

Dim A(20)
A(0) = e1
A(1) = e2
...
A(20) = e20

In my version of AST the tree rewrite may look something like this.

tree grammar TSLrewriteArray;

options {
    tokenVocab = TSL;
    ASTLabelType = CommonTree;
    output = AST;
    filter = true;
    rewrite = true;
}

topdown

   :    ^(EXPRESSION_STATEMENT ^(ASSIGN ID INT)) ( ^(EXPRESSION_STATEMENT ^(ASSIGN ^(INDEX ID ^(INC_AFTER ID)) e=. )) )+
        -> ... result tree here...
        ;

I was not able to make AST tree rewrites to work directly. I think that the reason is that tree rewrites are based on a tree visitor pattern. The pattern assumes that only one node at a time may be visited and changed, while here I need to change sequence of nodes with arbitrary length. The replacement is also a sequence of nodes and potentially of different length (it could always be presented as one node by adding an artificial root to a sequence, but I prefer sequence).

The generated recognizer is capable of properly doing pattern matching. Seems like I only need to create an appropriate visitor that understands that more than one node may be visited and replaced. I do not know yet if this could be done, if I will encounter other issues, for example with rollbacks.

Has anybody encountered such a problem? Am I following the right approach?

I have limited experience with ANTLR and any help would be appreciated.

Thanks in advance,
Viktor

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090619/689372ad/attachment.html 


More information about the antlr-interest mailing list