[antlr-interest] Exception Handling in ANTLR 3

Gerz, Michael Gerz at fgan.de
Fri Mar 16 05:20:01 PDT 2007

Hi Terence,

yesterday, I wrote a little ANTLR 3-based parser and I ran into a tricky problem:

In the parser, I make a couple of semantic checks. In order to produce smarter error messages, I decided not to use ANTLR's semantic predicates. Instead, I wanted to throw some exceptions with customized messages. Of course, I was in hope that ANTLR handles these exceptions and provides additional line & column info.

Now the problems began:

 - AFAICS, all parser rules throw RecognitionExceptions by default. 
   However, none of the derived subclasses allows you to simply
   specify a plain text error message :-(

   (I think this was possible in ANTLR 2.7.X, wasn't it?)

 - Therefore, I defined my own Exception class, called
   SemanticException. Accordingly, I added a "throw" clause
   to all ANTLR parser rules. 

   Unfortunately, this didn't work either. ANTLR 3 parses these 
   "throw" clauses but does not consider them for Java code output!

Terence, have I overlooked anything? Is there an easy way to provide application-specific exceptions?

I scanned all ANTLR 3 examples but couldn't find a single grammar file that makes use of exceptions. Isn't this a typical scenario?

Kind regards,


PS: The ANTLR 2.7.X documentation displayed all ANTLR exceptions in a hierarchy. The ANTLR 3.0 reference book only provides a flat list. Is there any reason for this? OO people like hierarchies! :-)

Dr. Michael Gerz                                                        
FGAN e.V.                                      Phone:  +49 228 9435-414 
Department FKIE/ITF                            Fax:    +49 228 9435-685 
Neuenahrer Straße 20                           E-Mail: gerz at fgan.de     
53343 Wachtberg-Werthhoven                     WWW:    www.fgan.de     

More information about the antlr-interest mailing list