[antlr-interest] (POST THIRD TIME) how to bail on MissingTokenException

Geoff hendrey geoff_hendrey at yahoo.com
Thu Oct 30 22:40:42 PDT 2008


I already have the following in my grammer:

@members{
protected void mismatch(IntStream input, int ttype, BitSet follow)throws RecognitionException{
    throw new MismatchedTokenException(ttype, input);
}
public Object recoverFromMismatchedSet(IntStream input, RecognitionException e, BitSet follow)throws RecognitionException{
    throw e;
}
}
@rulecatch{
catch(RecognitionException e){
    throw e;
}

And when I look in the generated parser I find this:

    protected void mismatch(IntStream input, int ttype, BitSet follow)throws RecognitionException{
        throw new MismatchedTokenException(ttype, input);
    }
    public Object recoverFromMismatchedSet(IntStream input, RecognitionException e, BitSet follow)throws RecognitionException{
        throw e;
    }

 
But it doesn't seem to make any difference. The parser still silently accepts syntactically incorrect inputs. In ANTLRWorks I can see the MissingTokenExceptions in the concrete parse tree. (see attached picture). But the AST just happily builds, even though my input includes a token that is supposed to be surrounded with a pair of angle brackets, but isn't. I've got a rule in my grammar that should force the Identifier to be surrounded by angle brackets.

typedSURID    
    :    'SURID' '<' queryNames'>' Identifier ->^(SURID queryNames Identifier)


I can substitute square brackets instead of angle brackets in my input, and I still just get MissingTokenException in the concrete parse tree, with the AST happily building and no exception thrown out to my application that invokes the parser.

-geoff






________________________________
From: Terence Parr <parrt at cs.usfca.edu>
To: Geoff hendrey <geoff_hendrey at yahoo.com>
Cc: antlr-interest at antlr.org
Sent: Thursday, October 30, 2008 10:55:51 AM
Subject: Re: [antlr-interest] (POST THIRD TIME) how to bail on MissingTokenException

Missing comes from the automatic in-line error recovery. You need to override

    protected Object recoverFromMismatchedToken(IntStream input, int ttype, BitSet follow)

just throw an exception. make sure you don't catch anything later... this should be in the book under how to make the parser bail on first exception.
Ter
On Oct 29, 2008, at 9:25 PM, Geoff hendrey wrote:

> Hi,
> 
> thanks for the response, I really appreciate it. Do you mean "mismatch" or "missing"? Just to be sure we are on the same page, I am getting a MissingTokenException, not MismatchedTokenException. My grammar file already deals with MismatchedTokenException, nd I have no problem with that. I need to find out how to abort parsing on MISSINGTokenException.
> 
> Thanks, and sorry for using caps and bold. Just wanted to make sure we are on the same page.
> 
> -geoff
> 
> 
> 
> From: Terence Parr <parrt at cs.usfca.edu>
> To: Geoff hendrey <geoff_hendrey at yahoo.com>
> Cc: antlr-interest at antlr.org
> Sent: Wednesday, October 29, 2008 12:58:28 PM
> Subject: Re: [antlr-interest] (POST THIRD TIME) how to bail on MissingTokenException
> 
> Hi Geoff, isn't there something in the source code that says " override me if you want to turn off automatic in-line error recovery for mismatch token"?
> Ter
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
> 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20081030/4c16e912/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Picture 2.png
Type: image/png
Size: 25124 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20081030/4c16e912/attachment.png 


More information about the antlr-interest mailing list