[antlr-interest] Access to lexer warning/error messages after parsing

Sam Ellis sam.ellis at arm.com
Wed Jul 2 00:30:00 PDT 2008


On 1/7/08 20:19, "Terence Parr" <parrt at cs.usfca.edu> wrote:
> My intention was that you override one method and do what you want.
> Not sure what else anybody would want. Replace System.err in override
> with errors.add(...) and you're done.  Not sure what the long
> conversation is about.  Can't think of an easier way.

I feel the current approach of the parser writing to stderr does not cleanly
separate out the responsibilities of the objects. The parser is for parsing,
not error reporting :-) I also had the same problem as somebody else, in
that I had to go and examine the antlr sources to work out how to override
the error reporting. Admittedly this was not hard, but gave me the
impression I was needing to know more about the internals of the parser
implementation than I would ideally like.

The approach I used in my grammar was to define an IErrorReporter interface,
and to pass an object that implements this interface to both the lexer and
parser. The parser then calls this interface when an error occurs, and I
have some custom logic in the lexer that checks for illegal chars and
reports these too (but does not throw an exception because I want it to
continue). The same object is also used elsewhere in my code for reporting
other errors. I have an implementation of this interface for reporting to
stderr, and another implementation that stores the errors in a data
structure (ultimately for consumption by Eclipse) and a null implementation
that just throws errors away.


-- 
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