[antlr-interest] Tree Grammar for loops

Christian Pontesegger christian.pontesegger at web.de
Thu Feb 18 08:18:00 PST 2010


Hi there,

I hope this list is intendend to ask for support.

I am desperately trying to write a tree grammar for a do-while loop. I
got the following so far:

The source for the loop:
----------------------------

    int $loop = 0
    do {
        $loop = $loop + 1
        foo()
    } while ($loop < 3)


for such a loop my parser gives me:
---------------------------------------

    (= int ($ loop) 0) (do { (= ($ loop) (+ ($ loop) 1)) foo } (< ($
loop) 3))


TreeGrammar.g:
-----------------------

doWhileStatement
    :
        ^(                                           
        head='do' '{'                     {    int loopHead =
((CommonTree) head).getTokenStartIndex(); }
            statementList
        '}' e=expression              {    if (e.isTrue()) {
                                                           
input.seek(loopHead);
                                                           
doWhileStatement();
                                                        }
                                                    }
        )
    ;

the TreeGrammar is able to recognize the loop, but I am not able to run
the loop a second time. as calling doWhileStatement() runs into an
exception. How can I traverse parts of my tree again?

I was looking for tutorials and found:
http://www.antlr.org/wiki/display/CS652/Tree-based+interpreters

... but it seems a bit out of date.

any help is appreciated
regards
Christian



More information about the antlr-interest mailing list