[antlr-interest] Tree walker exception handling

Cameron Esfahani dirty at apple.com
Mon Jun 4 19:49:47 PDT 2007


Thanks.  That helped a lot.

And based on my requirements, I decided to move the symbol validation  
code back into the parser.  It has access to information that the  
tree walker doesn't.

Now I'm trying to figure out how to manage the information/object  
sharing that's going to have to happen between my parser, tree walker  
and the utility classes I'm creating.

Does anyone have any suggestions on how best to handle this?

To give a concrete example, I want to print out an error when the  
parser finds an undefined symbol.  Right now I can print out the  
symbol name and line number, but I want to print out the file name.   
The ANTLRFileStream class has getSourceName() which I want to use,  
but I'm not sure how to get to it.  It's created in my main program.   
So short of passing it, or my main class object into my parser, I  
don't know how to get to it.

On May 25, 2007, at 1:21 PM, Loring Craymer wrote:

> Take a look at Ter's ErrorManager.java in the tool
> directory.  It has a lot of extraneous stuff (as far
> as your application is concerned), but is not a bad
> model for error accumulation and reporting.  Throwing
> exceptions for semantic errors is not user-friendly:
> you really want to report back as many semantic
> problems as the user can handle, not just the first
> one.
>
> --Loring
>
> --- Cameron Esfahani <dirty at apple.com> wrote:
>
>> I know the questions are a bit open-ended, but does
>> anyone have any
>> suggestions?
>>
>> On May 22, 2007, at 5:25 PM, Cameron Esfahani wrote:
>>
>>> I'm developing a language that supports assignment:
>>>
>>> foo = 125
>>> bar = foo + 1
>>>
>>> Obviously, I need to create a symbol table to hold all of the
>>> assignment operations, and then validate all symbols against that
>>> table.
>>>
>>> From what I can tell, I should be doing this in a tree walker
>>> pass.  The problem I'm having is, what do I do when I find a
>>> logical versus syntactical error?  For example, when the user
>>> erroneously supplies a symbol that doesn't exist?
>>>
>>> Right now, I'm just throwing a RecognitionException from within the
>>> relevant tree walker rule, and I have a @rulecatch which grabs this:
>>>
>>> @rulecatch {
>>> 	catch ( RecognitionException e ) {
>>> 		System.exit( 0 );
>>> 	}
>>> }
>>>
>>> I call System.exit() because I don't want the exception stack frame
>>> printing out.  It would scare my users.
>>>
>>> In the handler, I want to print out some useful information, like
>>> the name and line number of the undefined symbol. Can I still get
>>> this lexer information from a tree walker?
>>>
>>> Is it appropriate to use the RecognitionException for these kind of
>>> logical errors?  I'm almost wondering if I should create my own
>>> class of errors and then throw them as appropriate.
>>>

Cameron Esfahani
dirty at apple.com

"You only live once, and the way I live, once is enough"

Frank Sinatra



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070604/669ec2d6/attachment.html 


More information about the antlr-interest mailing list