[antlr-interest] Antlr parser error messages

Benjamin Niemann pink at odahoda.de
Tue Jul 17 04:10:08 PDT 2007

Shmuel Siegel wrote:

> IIRC, way back in the early days of ANTLR 3 early access, ANTLR had a
> very nice error reporting feature. When a parse rule decided that
> something didn't match, ANTLR would tell you the parse hierarchy of the
> location of the failure. Sort of like a traceback but rule based.
> Where has that feature gone? What can I do to activate it?

You can reimplement this by overriding the getErrorMessage() of the
You can get the 'rule traceback' using the method getRuleInvocationStack().
An enriched version of getErrorMessage() could look like

public String getErrorMessage(RecognitionException e,
                              String[] tokenNames) {
  return getRuleInvocationStack(e, this.getClass().getName())
         + " "
         + super.getErrorMessage(e, tokenNames);


The book features a slightly extended version of this (should be
in "errors/E2.g" in the code package from

Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/

More information about the antlr-interest mailing list