[antlr-interest] What if you want errors to exit the parser? (v3)

Terence Parr parrt at cs.usfca.edu
Tue Oct 31 09:29:27 PST 2006


On Oct 31, 2006, at 8:43 AM, David Holroyd wrote:
> Adding an empty @rulectch seems to mostly do what I want at the  
> moment.

Hi David,

Yep, but this does not deal with mismatched tokens in the middle of  
alternative.  I'm pretty sure that it only deals with no viable  
alternatives. after having looked at the code I note that I foresaw  
this problem it turns out.  I made a method that factors out what  
happens in the middle of alternative when it mismatch is a token:

	/** factor out what to do upon token mismatch so tree parsers can  
behave
	 *  differently.
	 */
	protected void mismatch(IntStream input, int ttype, BitSet follow)
		throws RecognitionException
	{
		MismatchedTokenException mte =
			new MismatchedTokenException(ttype, input);
		recoverFromMismatchedToken(input, mte, ttype, follow);
	}

  so, all you have to do is subclass that and throw the exception  
instead of trying to recover.  Users may even do dynamic automatic  
recovery by adding a Boolean variable test.  In this way some rules  
in your grammar could do automatic recovery where it makes sense but  
all the rules could just blow out the exception and recover elsewhere.

Cool.   Okay, adding to the book instead of altering the code :)
Ter



More information about the antlr-interest mailing list