[antlr-interest] Must all grammar rules be implemented for tree parsers?

Paul Johnson gt54-antlr at cyconix.com
Tue Dec 6 01:14:41 PST 2005


Rob Greene wrote:
>>From the example "An Example Tree Transformation" at
> http://www.antlr.org/doc/sor.html#_bb1, I think I can do some basic
> tree optimization before getting to the point of generating code for
> my compiler.
> 
> 1) From my experimenting, I cannot just pick nodes that are
> optimizable, can I? From what I'm seeing, I think I need to build
> enough of the language into the tree parser to get to the optimizable
> parts. As a simple example, numeric constants that are operated on
> together can be computed by the compiler instead of the resulting code
> (ie, "a = 24 * 60" can be replaced with "a = 1440"). Am I correct in
> this? I cannot just write a rule for the "*" in that example. I need
> to write the rule for the "=" and anything else preceding it?
> 
> 2) Is there a reference of optimizations that are listed on some web
> site (or in the ANTLR docs that I somehow haven't found)? The only
> ones I've thought of are numeric or string constants being used in an
> expression.

Tony Hoare: "Premature optimisation is the root of all evil in programming".

If you've got a real application, then you're going to be lucky if your 
  parsers form anything more than a tiny fraction of the total work 
involved. I would concentrate on getting something working first 
(probably with manual tree parsing, at least to start with), which will 
give you a better overview of the whole process. The optimisations in 
the example tree transformation are useful for illustrating tree grammar 
ops, but I personally can't see that they're relevant to production code.

HTH

Paul



More information about the antlr-interest mailing list