[antlr-interest] suppressing evaluation in tree parsers

Michael Laszlo mjl at myacc.net
Tue Nov 20 09:47:15 PST 2007


For my interpreter, I ended up writing a tree grammar that consumes
a tree and produces a tree of Java nodes implementing a common interface
(promising an evaluate() method). This required writing a Java class
hierarchy corresponding to expression types. The rule
arguments and return values supported by antlr was handy in the
tree grammar. The antlr tree is the AST for a program,
and the node tree it produces is the AST for program evaluation.
Other node trees could be produced for other tasks
(eg. type checking); alternatively, I could write an antlr tree grammar for
producing a tree that admits visitors, using the visitor design pattern.

I find that the real benefit of the antlr tree grammar is its
straightforward
notation for pattern matching trees. It might be worthwhile
designing a notation for rewriting trees in the RHS of tree grammar rules.
This notation might use or extend the existing notation for rewrite rules in
string grammars. Tree rewrites would allow you to write trees for various
tasks based on other trees, without depending on the concrete
syntax and without (or postponing) having to write Java code.

When I have an opportunity, I'll try to write a lazy version of
ANTLRReaderStream,
which I still think is needed for interactive applications. Thanks.



More information about the antlr-interest mailing list