[antlr-interest] How to reuse lexer object and parser object

Randall R Schulz rschulz at sonic.net
Thu Oct 9 06:27:51 PDT 2008


On Thursday 09 October 2008 00:30, Ronghui Yu wrote:

> On Thu, Oct 9, 2008 at 3:12 PM, Gavin Lambert <antlr at mirality.co.nz> 
wrote:
> > At 19:59 9/10/2008, Ronghui Yu wrote:
> >> As I mentioned before, I use the same lexer and parser object for
> >> parsing different SQL statements.
> >
> > Is there some particular reason why you don't want to construct a
> > new instance?
>
> Yes, this is the easiest way to make it work.
> It is not I who designed the architecture. But I think the reason may
> be the performance, the parser object contains over 1000 fields
> needed to be set, it may be much faster to reuse it by resetting some
> fields of it.

Why don't you refactor the design so that pile of state is a separate 
object? It doesn't sound like it belongs in the parser.

I like to define an interface, an "acceptor" that is passed to the tree 
parser constructor. All the AST parser does is the minimum required to 
interpret the AST it's parsing. All the specifics of what happens with 
each individual target language construct resides in the acceptor. I 
usually create a "null acceptor" that implements all the acceptor 
interface's methods but does nothing. It's the default acceptor and can 
be used as a base class for implementing acceptors that don't need to 
handle every construct in the target language.

This setup allows a given language to be processed in many different 
ways. It would also serve to separate the voluminous state you have 
from the AST parser proper.


Randall Schulz


More information about the antlr-interest mailing list