[antlr-interest] C Target: Reverse look up of tokens for error reporting

Jim Idle jimi at temporal-wave.com
Tue Nov 3 15:13:37 PST 2009


You have to supply a function that takes the token as defined by the expecting element, perform a switch() on it and supply the text you really want. It is pretty easy but there is no automated way to do it and if we had the alias stuff that was in v2 it really doesn't save you any typing. Better to do it explicitly yourself.

Jim

> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Peterson, Joe
> Sent: Tuesday, November 03, 2009 2:56 PM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] C Target: Reverse look up of tokens for error
> reporting
> 
> Hello all,
> 
> I am trying to generate more useful error messages when I get a file
> that doesn't match the grammar. One of the complaints is that because I
> named my tokens, the error messages look like this:
>     syntax error detected in file missing_semicolon.test at line number
> 6, position 5; expecting SEMICOLON
> 
> To me, this seems obvious, but my customers don't like the token names,
> they'd rather see:
>     syntax error detected in file fail_missing_colon.ext at line number
> 6, position 2; expecting ';'
> 
> I prefer to have the names, though. Is there an easy way for me to
> reverse the look up so the error can replace SEMICOLON with ';' (as
> well as all my other tokens, of course)?
> 
> I can get the NAME of the token with:
> 	recognizer->state->tokenNames[recognizer->state->exception-
> >expecting]
> I can't seem to see how to get the text of that token, though.
> 
> Thanks,
> -JEEP
> Joe Peterson
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address





More information about the antlr-interest mailing list