[antlr-interest] Throwing exceptions from rules.

Bharath Sundararaman Bharath.Sundararaman at starthis.com
Tue Feb 1 14:37:33 PST 2005


Hi all,

I have a tree parser which checks the AST generated by my parser for
type safety. I would like to define some exceptions of my own to display
type errors. For example, "TypeMismatchException" or
"InvocationException" or "IncompatibleOperandsException" and so on..

I am trying to throw these exceptions and handle them in the class that
contains the main() method which invokes the compiler so that I can
return errors to an app that passes source code to the compiler. 

The problem is I am forced to add the "throws" clause to each and every
rule in my tree walker for each exception. My tree walker has around
1000 rules and almost 50% of the rules are capable of throwing more than
one of the exceptions I mentioned above. I tried to add exceptions as
they are thrown in a list and return it to the calling program; However,
If I do this, I am unable to stop the compiler execution when an
exception is thrown. If I extend java.lang.RuntimeException to write my
Exception classes, I don't have to add the "throws" clause but it seems
to be a bad idea to mess with RuntimeExceptions in catch blocks.

Any ideas?

RIC: THANKS FOR YOUR ADVICE, I AM ABLE TO INITIALIZE MY LEXER/PARSER
JUST ONCE AND PASS DIFFERENT PIECES OF CODE BY JUST RESETTING STATES!

Thanks,

B.






-----Original Message-----
From: Ric Klaren [mailto:ric.klaren at gmail.com] 
Sent: Tuesday, February 01, 2005 3:11 AM
To: Bharath Sundararaman
Cc: ANTLR Interest
Subject: Re: [antlr-interest] Lexer/Parser invocation

On Mon, 31 Jan 2005 15:29:54 -0600, Bharath Sundararaman
<Bharath.Sundararaman at starthis.com> wrote:
> Lexer l = new lexer("source code goes here");
> Parser p = new Parser(l); // l is the Lexer object
> Parser.topRule(); // top most rule
> 
> I use ANTLR in a Java based environment and I saw the following post
on
> the grp:
> 
>
http://www.antlr.org/pipermail/antlr-interest/2004-June/008230.html
> 
> I could not find the method signatures listed in this post. For
example,
> the parser constructor seems to have a pointer (in C++) to a lexer and
> symbol_information? (Slightly lost here)

The symbol_information is a copy-paste error from the original code
the rest should be ok. The other method signatures should be present,
or maybe a small difference with the original antlr would be possible
since I use custom constructors inside that project. But basically a
look at the header files should make things clear?

Cheers,

Ric


More information about the antlr-interest mailing list