[antlr-interest] declare throws-clause for rule

Raphael Reitzig r_reitzi at cs.uni-kl.de
Sat Jul 12 03:34:45 PDT 2008


Hi!

Throwing custom exceptions from generated code was something I also wished
to do. I do not feel that throwing a RuntimeException is a clean way to
solve that problem.
Is there any particular reason for generated parsers "swallowing" all
Exceptions (and providing no means of changing that behaviour) or is it
just a thing no one ever got disturbed by?

Regards

Raphael

On Thu, 3 Jul 2008 13:24:02 +0200, "Gerard van de Glind"
<g.vandeglind at beinformed.nl> wrote:
> Hi,
> 
> Yes, you can throw an exception (a RuntTimeException to be precise,
> because these are unchecked) from your grammar. And you can catch these
> exceptions in the class that invokes the generated parser.
> 
> Example:
> expression : ruleA | ruleB { if (condition) {throw new
> RunTimeException("");} };
> 
> Also, code that you place in the @member action gets generated into your
> parser.
> 
> Good luck!
> 
> Cheers, Gerard
> 
> 
> -----Original Message-----
> From: antlr-interest-bounces at antlr.org
> [mailto:antlr-interest-bounces at antlr.org] On Behalf Of tilman walter
> Sent: donderdag 3 juli 2008 12:15
> To: ANTLR List
> Subject: [antlr-interest] declare throws-clause for rule
> 
> Hi List,
> 
> I am a newby to antlr.
> Is there a possibility to let a rule throw a custom exception that can 
> be catched by the class invoking the parser?
> I think i could place this piece of code in the generated class, but i 
> wonder if i can reach this through a grammar construct....
> 
> I would appreciate for any hint.
> Thanks is advance.
> 
> Tilman



More information about the antlr-interest mailing list