[antlr-interest] overriding BaseRecognizer methods for lexer, parser and tree parser

Sam Ellis sam.ellis at arm.com
Fri Dec 21 08:40:47 PST 2007


On 21/12/2007 14:33, "Mark Volkmann" <r.mark.volkmann at gmail.com> wrote:

> I'd like to override the displayRecognitionError method for all three
> of my generated parser classes, the lexer, the token parser and the
> tree parser. It seems that the only way to do this is to repeat my new
> version of that method three times in my grammar files. Is there a
> better way? My first thought was to create my own subclass of
> BaseRecognizer and then tell ANTLR to use that instead of
> BaseRecognizer as the superclass of Lexer, Parser and TreeParser which
> are the superclasses of the generated classes. Of course that doesn't
> make sense because Java can't just switch the superclass of classes
> that aren't generated.

I solve this by creating a new class that is responsible for all error
reporting, and then pass an instance of this class to the lexer and parser.
I override the lexer and parser constructor to pass the error reporting
class as a new parameter, but an alternative is do this via a separate
method call instead. In the lexer and parser I override the error reporting
methods to call my new error reporting class. This means most of the error
reporting code is centralised, and the changes to the lexer and parser are
trivial.


-- 
Sam Ellis, Engineering Manager,
Product Engineering Group,                 Tel: +44 (0) 1223 400516
System Design Division,                    Fax: +44 (0) 1223 400887
ARM Ltd., 110 Fulbourn Road,               skype:armsamellis
Cambridge, CB1 9NJ                         mailto:sam.ellis at arm.com



-- 
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium.  Thank you.




More information about the antlr-interest mailing list