[antlr-interest] Processing/traversing a rule

Tyler Distad tyler.distad at gmail.com
Thu Apr 8 04:16:28 PDT 2010


I have a tree parser which is traversing the nodes of my AST. At points
within the grammar, I use action blocks to call methods on my processing
object, thus keeping large amounts of action code abstracted into a separate
file.

This has worked fine, but now I have the "if" and "while" statements visible
below:

stmtIf
    : ^(STMT_IF expr trueStmts=stmt* (ELSE_KW falseStmts=stmt*)?)
        { proc.outStmtIf($expr.start, $trueStmts.start,
$falseStmts.start);    }
    ;

stmtWhile
    : ^(STMT_WHILE expr stmt*)
        { proc.outStmtWhile($expr.start, $stmt.start);    }
    ;

Obviously, this works fine for actions which take place after the entire if
or while tree has been processed. But I want to do some processing before
the tree is traversed, as well as some at specific points during. I could
create more method calls in my processing object and sprinkle them
throughout the above AST, but that feels so clunky. I'd like to be able to
override the entire traversal of the rule at this point and do some custom
work.

So, two questions:
 * In general, is my method of making calls to a processing object
sane/normal/best practice?
 * In the above cases, is there any practical method of manually processing
an entire rule and its descendant tree?

Tyler Distad


More information about the antlr-interest mailing list