[antlr-interest] case sensitivity for ANTLR v3 lexers

Don Caton dcaton at shorelinesoftware.com
Tue May 16 19:30:45 PDT 2006


Ter:

> Sample v3 output:
> 
>          catch (RecognitionException re) {
>              reportError(re);
>              recover(input,re);
>          }
>
> note not a single literal in the output...almost as if i'd 
> learned my lesson!  Yep, you shall revel in the joy that is v3.

Doesn't look any different than v2.  The problem is not here, the problem is
in the exception classes.

> If you override reportError you are golden...no strings...

No, I think you're missing the point (or maybe the Java implementation is
completely different than C++).

The default reportError() does nothing more than call re.toString() and send
it to cerr.  It makes sense to override it if you want to direct the error
message to a destination other than cerr.  

It doesn't make any sense to override reportError() to change the behavior
of RecognitionException.  Why should reportError() have any knowledge of the
implementation details of any of the exception classes?

All I'm saying is that the exception subclasses should not have hard-coded
strings, they should call some sort of helper or utility method to obtain
the string.  A simple example is in NoViableAltForCharException's
toString(), which is:

   return string("unexpected char: ")+charName(foundChar);

This is bad.  It ought to be something like:

   sprintf( buffer, getString( ANTLR_UNEXPECTED_CHAR ), charName( foundChar
) );
   return string( buffer );

Sprintf probably isn't the best thing to use since you can't rearrange the
order of the arguments in the format string, but I think you get the idea.
There shouldn't be a hard-coded string anywhere in the code, other than the
default English strings (or whatever other languages you want to include).
I don't know exactly where something like getString() ought to reside, maybe
in some sort of utility class or whatever, but it should be overridable in
some manner.

Don




More information about the antlr-interest mailing list