[antlr-interest] How to feedback to users the string	expected	on	MismatchedTokenException
    Benjamin Niemann 
    pink at odahoda.de
       
    Wed Jun 13 00:57:15 PDT 2007
    
    
  
Jonathan Thomas wrote:
> In previous versions of Antlr you could put in 'paraphrase' option to
> spit out whatever you liked as the error message for that token. On
> this
>
page:http://www.antlr.org/wiki/display/ANTLR3/Migrating+from+ANTLR+2+to+ANTLR+3
> down the bottom it mysteriously says there is something similar, but
> you need the book.  I'm still waiting for my book to arrive ... :-)
The book only describes paraphrasing for rules (up to the page I am at now -
but I have finished the error recovery chapter just yesterday).
To elaborate my suggestion a bit more:
getErrorMessage() takes the tokenNames array as an argument, so you could
override it with a method that calls BaseRecognizer.getErrorMessage() with
a custom array.
I'd suggest to fill this custom array from a mapping, because token types
may jump around in the mapping, when the grammar is modified.
A rough example in Python syntax (still too early for me to switch my brain
into - a very limited - Java mode ;) )
# this clones the original array
myTokenNames = TParser.tokenNames[:] 
# a mapping of token types and there new name
overrides = {
  PLUS: 'plus sign',
  DOLLAR: 'much money',
  ...
}
# changes names of those token type mentioned in overrides
for ttype, name in overrides.items():
    myTokenNames[ttype] = name
And you getErrorMessage() looks like (if you'd do it in Python):
def getErrorMessage(self, exc, tokenNames):
    return BaseRecognizer.getErrorMessage(self, exc, myTokenNames)
-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/
    
    
More information about the antlr-interest
mailing list