[antlr-interest] Calling a tree parser rule manually (a number of times)

Akhilesh Mritunjai virtualaspirin at yahoo.com
Tue Jun 21 12:25:43 PDT 2005


Hi folks

Here is a small problem. I'm processing a language
that  has compile time macros for generating code. My
parser builds AST and then I have a tree parser to
process the uber-macros macros look like this

// loop variable can only be an 'integer'
for(i = 0; i < 3; i++)
 blah(a[i]);

This should generate:

blah(a[0]);
blah(a[1]);
blah(a[2]);

So the input tree looks like something like this:
#(MAIN
  #(FOR #(FOR_BODY ...ast of (i = 0....) ...)
    #(MACRO_FOR_ITEM
     #(FUNCTION_CALL
      #(IDENTIFIER "blah:)
      ....))))

my processor should convert it to 3 instances of
FUNCTION_CALL as a list. My treeparser rule looks like
this:


macro_for:
 // n is number of iterations computed through an
 // involving code, which is omitted
 #(FOR  n=macro_for_body mi:MACRO_FOR_ITEM)
 {
   for(int i = 0; i < n; i++)
   {
     // Looks disgusting, no ?
     // manually call the rule
     macro_for_item(#mi, i);

     // black magic
     ## = #(null, ##, returnAST);
   }
 }
;

macro_for_item[int valueOfLoopVariable]:
 FUNCTION_CALL
 blah
 {
   search_and_replace loop variable with value
 }
;

To summarise: I want to tree-parse the same subtree a
number of times and add the resulting ast to
constructed tree.

Is there a better way to accomplish this task than
outlined above ? I don't like using 'currentAST'
variable!

- Akhilesh



		
__________________________________ 
Yahoo! Mail 
Stay connected, organized, and protected. Take the tour: 
http://tour.mail.yahoo.com/mailtour.html 



More information about the antlr-interest mailing list