[antlr-interest] exceptions in the lexer

Terence Parr parrt at cs.usfca.edu
Mon Dec 6 17:03:00 PST 2004



On Dec 6, 2004, at 4:10 PM, Paul J. Lucas wrote:

>
> On Mon, 6 Dec 2004, Terence Parr wrote:
>
>> Are you familiar with the filter option on the lexer grammar?
>
> 	Yes in the sense that I've read that bit of the documentation,
> 	but I've never used it.
>
> 	I don't want my entire lexer to be a filter since I need it to
> 	be a real lexer for a parser.
>
> 	My particular case at hand is dealing with pragmas that can
> 	appear anywhere comments can.  Since comments are most easily
> 	handled in the lexer, I handle pragmas there to for the same
> 	reason.  The problem is what to do if there's an error in a
> 	pragma.  The lexer can't throw user-defined exceptions.  (I
> 	might be able to work around it by hiding the real exception
> 	inside a Runtime exception, but that's ugly.

Ah.  An example makes everything clear.  What precisely do you want to 
happen when there is an error?  I guess at minimum an error, but the 
question is: how to trap it?  Recovery is also an issue.  Let me look 
at the output for a second...

Ok, generated nextToken looks like it catches and rethrows:

             catch (RecognitionException e) {
                 throw new TokenStreamRecognitionException(e);
             }

Shoot.  Well, the token stream exception can be caught easily in a 
TokenStream wrapped around the Lexer.  You can even ask it to retry for 
another token.

Ok, for a REAL hack, you could wrap a reference to a lexer rule with 
actions containing a "try {" and then "} catch...", though ANTLR might 
get pissed about the unbalanced curlies.  Might have to escape them 
with \.

Hmm...I'd probably go for the TokenStream filter that catches the 
exception.  Easiest thing.

Ter
--
CS Professor & Grad Director, University of San Francisco
Creator, ANTLR Parser Generator, http://www.antlr.org
Cofounder, http://www.jguru.com
Cofounder, http://www.knowspam.net enjoy email again!





 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
    antlr-interest-unsubscribe at yahoogroups.com

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





More information about the antlr-interest mailing list