[antlr-interest] Correct way to handle custom errors?

Stephen McGruer stephen.mcgruer at gmail.com
Fri Feb 4 16:14:27 PST 2011


I was wondering what the "correct" way to handle custom errors in ANTLR 3
was. I'm implementing a parser/lexer for a small subset of C, and want to
give better error messages than the general mismatched tokens error.

 From this post -
http://www.antlr.org/wiki/display/ANTLR3/Error+reporting+and+recovery I
implemented my own error reporting class which grabs the recognition
exception from the Parser. However, I'm then unsure about the best approach
is to start recognising errors - from things as basic as replacing the name
of my variable name rule (ID) with a more user-friendly term such as
"identifier", to more complex things such as giving nice error messages when
the user attempts to set a variable to a value that isn't an integer or
character (the only two allowable types in my subset). The recognition
exception doesn't seem to be too useful - getMessage() seems to return null.
I assume that the getErrorHeader and getErrorMessage methods do something
different to get information. The only way I can think of checking for
errors at the moment is manually scraping the strings returned by these
methods, which seems... clumsy.

So, is there a sort of "normal" or "usual" method to approaching
parser/lexer error message handling when doing programming language parsing?

I hope the above contained enough information, and made sense.

Thanks,
Stephen


More information about the antlr-interest mailing list