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.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"il-antlr-interest" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/il-antlr-interest?hl=en
-~----------~----~----~----~------~----~------~--~---
List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe:
http://www.antlr.org/mailman/options/antlr-interest/your-email-address