[antlr-interest] Execute part of a tree multiple times

dhjdhj dhjdhj at gmail.com
Fri Mar 6 08:04:49 PST 2009


I'm new to ANTLR (love it, by the way) so hopefully the following  
question is not too stupid!

Suppose I have a language whose core I want to execute multiple times,  
typically against different data. (A lot of financial trading systems  
work this way, for example)

So here's an example of such a language


prolog
     count = 0;   // This code gets executed once at startup

main
     count = count + 1; // Count how many times we executed this piece

    stockPrice = getStockPrice();
    ...do some calculations


epilog
    write(count); // Indicate how many times the main loop was run



If I generate a tree out of this, I might get something like

(PROLOG  (ASSIGN count 0))
(MAIN  (ASSIGN count (+ (count 1))  (ASSIGN stockPrice (function  
getStockPrice)) .....)
(EPILOG (WRITE count))


So the top piece of the grammar probably looks like

program:  prolog? main epilog?;   // Prolog and epilog are optional

prolog:    PROLOG statementList;
main:       MAIN statementList
epilog:     EPILOG statementList



The normal way to execute this would be to write something like
     walker.program();


However, what I'd like to do is something like the following:

walker.prolog();

for (int i = 0; i < externalDataItems.Count(); i++)
    walker.main();

walker.prolog();


However, to do this, I need a way to reset the "top" of the parse tree  
so I can restart from the MAIN node.

I can't see an obvious way to do this in the ANTRL runtime --- is it  
possible (without too much hacking?)


If I can do this, then I don't need to go the extra step of generating  
target code out of the tree.


Thanks in advance

David Jameson






More information about the antlr-interest mailing list