[antlr-interest] Error reporting with the C runtime: tokenNames
Jim Idle
jimi at temporal-wave.com
Tue May 19 08:28:45 PDT 2009
Ned Gill wrote:
> Hi list.
>
> I'm writing a custom displayRecognitionError for my parser, based heavily
> on the default in antlr3baserecognizer.c. For reporting the names of
> tokens, I'm using the tokenNames array passed in for messages such as "...
> Missing ']'". However, for named tokens (e.g. those created in the tokens
> { ... } stanza), tokenNames contains er.. the name of the token. So for
> example, I'm using the names RPAREN for the token ')', and I see messages
> like "... Missing RPAREN." Unfortunately these names aren't going to make
> any sense for users.
>
It is the same as the other targets, in that you need to create a local
function that returns/displays/adds to the message, the name you want to
use for error display. It is just a switch statement on the token type
basically, or you could create a local map and initialize it the first
time it is required. It is just a bit of slog really. Java provides a
method to override to do this, but in C, you just call your own local
function.
> I understand why tokenNames is the way it is (named tokens could be more
> compilcated than a simple string). However, is there a way that I can
> show the matching string for simple tokens rather than the name, (possibly
> limited to just those declared with "tokens {...}")?
>
> I considered removing some or all of the simple named tokens, but this
> would be inconvenient as my grammar generates trees which re-use the same
> tokens.
>
> I'm already maintaining my own version of C.stg for other reasons, so I
> could make changes to that if necessary.
>
Let me know if there is something that you changed that really needs to
be in the main template - I know of at least two things that I need to
resolve (@after for instance), but obviously would prefer that you can
use the main branch. I don't know when you split, but there have been
one or two tweaks to C.stg, so make sure you pick them up.
Jim
More information about the antlr-interest
mailing list