Altering and Redirecting ANTLR Error Messages
By default, ANTLR sends all errors to standard error, but we can change the destination and the content by providing an implementation of interface ANTLRErrorListener. The interface has a syntaxError method that applies to both lexers and parsers. Method syntaxError receives all sorts of information about the location of the error as well as the error message. It also receives a reference to the parser, so we can query it about the state of recognition.
For example, hereâs an error listener (in test rig TestE_Listener.java) that prints out the rule invocation stack followed by the usual error message augmented with offending token information:
â | âpublicâ â |
Get The Definitive ANTLR 4 Reference, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.