[antlr-interest] How does . (dot) work in tree pattern matching?

Viktor Lioutyi Viktor_Lioutyi at i2.com
Mon Jun 8 14:51:50 PDT 2009


Tree pattern matching seems as an ideal tool for implementing sequence of AST transformations for a small project I am trying to prototype.
A transformation must convert for-loop like this "for (int i = exp1; i <= exp2; i++) {..." to something like that "for i from exp1 to exp 2 {..."

Below is an extract from tree pattern matching grammar

tree grammar RewriteFOR;

options {
...
                ASTLabelType = CommonTree;
                output = AST;
filter = true;
...
}

topdown
   ...
                |              ^(FOR ^('=' v1=ID e3=.) ^('<=' v2=ID e4=.) ^(for_inc_op v3=ID) s=.
            {$v1.text.equals($v2.text) && $v1.text.equals($v3.text)}?)
        -> ^(FORVAR $v1 $e3 $e4 $s)
...
                ;

for_inc_op
                :               INC_BEFORE
                |              INC_AFTER
                ;

This rule works for trees like this  "for (j = 1; j <= itemPos + 1; j++) {"
(FOR (= j 1) (<= j (+ itemPos 1)) (INC_AFTER j) ...) -> (FORVAR j 1 (+ itemPos 1) ...)

But it does not work for "for (j = 1; j <= (abs(ExpectedArray[i+1] - (itemPos + 1))); j++) {"
(FOR (= j 1) (<= j (CALL_FUNC abs (ARGS (- (INDEX ExpectedArray (+ i 1)) (+ itemPos 1))))) (INC_AFTER j)

Seems that "e4=." can only be matched by simple expressions of limited depth.

I have not found the formal definition of . in an AST grammar and just tried to follow the example from http://www.antlr.org/wiki/display/~admin/2008/11/30/Example+tree+rewriting+with+patterns
My assumption is that . (dot) denotes any AST or simple node.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090608/f00a6b9e/attachment.html 


More information about the antlr-interest mailing list