[antlr-interest] Exception Handling in ANTLR 3

Joerg.Werner at qimonda.com Joerg.Werner at qimonda.com
Fri Mar 16 06:15:47 PDT 2007


I complained about this already a few months ago, but nothing happend... :-(

Ter, I second this motion to implement the throws clauses again.



Dr. Jörg Werner (韦尔讷)
Qimonda AG 
Memory Products Division
Am Campeon 1-12
D-85579 Neubiberg
email: joerg.werner at qimonda.com
Tel:  +49 89 60088-2231
Fax:  +49 89 60088-442231

Chairman of the Supervisory Board/Aufsichtsratsvorsitzender: Peter J. Fischl
Management Board/Vorstand: Kin Wah Loh (Chairman/Vorsitzender), Dr. Michael Majerus, Thomas J. Seifert
Register Court/Registergericht: München HRB 152545, Seat/Sitz: München

-----Original Message-----
From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Gerz, Michael
Sent: Friday, March 16, 2007 1:20 PM
To: parrt at cs.usfca.edu; antlr-interest at antlr.org
Subject: [antlr-interest] Exception Handling in ANTLR 3

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