[antlr-interest] Using the results

Kaleb Pederson kaleb.pederson at gmail.com
Tue Mar 16 18:20:10 PDT 2010


On Tue, Mar 16, 2010 at 4:26 PM, Nikolas Everett <nik9000 at gmail.com> wrote:
> Its been a long time since I last used antlr and I'm now getting the chance
> to use it again.  I happened on a rather conspicuous gap in my knowledge: I
> have no idea what the right way take actions based on parsed results.

At least you recognize that :).

> Should I parse my input to an AST and then walk the AST?  This seems like
> it'd be a pain to maintain.
> Should I call actions directly from antlr?  This simpler but in practice is
> really cumbersome and makes using antlrworks much more difficult.

Which one of the above you'll want will be largely dependent on what
your needs are and how complicated everything becomes.

If you're dealing with something simple, then putting the actions
directly in ANTLR's action code is a perfectly reasonable solution and
may result in the least amount of code and the code that's easiest to
understand (for those not versed in the ANTLR tree-walking process).

If you end up needing something more complicated and need information
from various parts in your processing, such as you might if you
support mutually recursive types, then using a tree walker and
deferring as much of the processing and validation as possible until
later.

There's likely a continuum between the two.  For example, I've found
that I can often create builder classes which I call within the
actions of my parser.  These builder classes provide a consistent
interface that I can use to test without dealing directly with the
parser, and provide an easy interface that the parser can use.  When
used with a good set of ANTLR features, such as scopes and actions on
non-terminal fragments, this can make for a very clean structure.

If you want specifics it would be helpful if you could provide more
concrete details.

HTH.

--
Kaleb Pederson

Blog - http://kalebpederson.com
Twitter - http://twitter.com/kalebpederson


More information about the antlr-interest mailing list