[antlr-interest] Question regarding C based exception handling in predicates

Kurt Otte kurtotte at gmail.com
Mon Aug 18 18:51:54 PDT 2008


What is the suggested way for doing error handling when using the C
target during token validation with semantic predicates and in action
blocks?  I currently have an AST which I am processing with a tree
grammar.  This is working great except for triggering exceptions which
can be converted to useful error messages.

Is there a trick to get validating semantic predicates in the tree
parsers to return the token which failed validation?

Here is a toy example:

   foo:
      ^(NAME detail {isValid($detail)}? )

This does what I would expect and calls isValid() on the token
'detail'.  isValid returns True or False after processing the token.
If it returns false, I would like it to trigger an exception which it
does; however, when it gets to displayRecognitionError() the token
which the exception handler has it not 'detail'.  It is some imaginary
token (perhaps NAME).  Is this a bug or what is a better way to
validate the contents of detail and produce a custom error message?

Along those lines, is there any way to throw an exception inside a
isValid()?  I only have a tree pointer passed in as an argument and I
don't see a way to get back to the parser object so it doesn't seem
like there is a way to throw an exception.  Is there a recommendation
for how to handle errors in these blocks?

What is the recommendation for handling errors in action blocks since
there isn't a return value which is linked to an exception?  Is there
a trick to throwing an exception in these blocks?

Thanks,

Kurt


More information about the antlr-interest mailing list