[antlr-interest] Re: A way to reuse Lexer, Parser and TreeWalker instances?

Brian Hagenbuch bhagenbuch at didera.com
Fri Apr 18 08:34:31 PDT 2003


--- In antlr-interest at yahoogroups.com, "oneway_111" <oneway_111 at y...>
wrote:
> --- In antlr-interest at yahoogroups.com, mzukowski at y... wrote:
> > I believe everything uses an InputState object to hold state. 
That
> needs
> > reset and you can reuse your lexer, parser or tree parser.  You
are
> > responsible for handling any instance variables you introduce.
> > 
> > Monty
> 
> Thanks, looks like it could work. Has anyone actually had success
with
> this?

We found that there are a few protected fields in the lexer and parser that we needed  
to reset in addition to resetting the input state object.  Worse, there don't seem to be  
public set functions for all of them.  We  use a too-complicated subclassing scheme 
to  solve the problem (as least we think we've solved it).  We'd love to hear that there's 
an easier way.

In outline, here's what we do (manual excerpt, typos likely): 


class L extends GeneratedLexer {

	private boolean savedCaseSensitive;
	private boolean savedCaseSensitiveLiterals;

	public L(Reader in) {
		super(in)
		savedCaseSensitive = lexer.getCaseSensitive();
		savedCaseSensitiveLiterals = lexer.getCaseSensitiveLiterals();
	}
	
	public void prepareNextInput(Reader in) {
	
		CharBuffer buf = new CharBuffer(in);
		LexerSharedInputState state = new LexerSharedInputState(buf);
		this.setInputState(state);
		
		this.setCaseSensitive(savedCaseSensitive);
		
		// no set method for this protected field.
		this.caseSensitiveLiterals = savedCaseSensitiveLiterals; 
	}
}

class P extends GeneratedParser {

	public P(TokenStream lexer)
	{
		super(lexer);
	}

	public void resetState() {
	
	// no set method for this protected field.
	this.traceDepth = 0;

	this.getInputState().reset();
}

To reset a lexer and associated parser:

	lexer.prepareNextStatement(...some Reader...);
	parser.resetState();

Hope this helps.


 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 




More information about the antlr-interest mailing list