[antlr-interest] declare throws-clause for rule

tilman walter tilman.walter at gmx.de
Fri Jul 4 01:07:11 PDT 2008


Many thanks Gerard for your reply.
Probably this is the way i have to go, although i hoped to be able to 
say something like:

rule throws MyException :
     //...
     ;

so that the compiler does not complain about not throwing or declaring 
the exception i am about to catch.

But i'll go for your solution.
Thanks a lot again.

Tilman
Gerard van de Glind schrieb:
> 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