4

I'm writing a simple parser for my compilers class (just a proof of concept that I can get the tools working) and am using ANTLR with python bindings. I've gotten our trivial grammar to properly tokenize and parse, but I want to also handle the errors in a custom way. According to the ANTLR documentation (more specifically: http://www.antlr.org/wiki/display/ANTLR3/Error+reporting+and+recovery), I can put the following code in my grammar file to do so:

@members {
    private List<String> errors = new LinkedList<String>();
    public void displayRecognitionError(String[] tokenNames,
                                        RecognitionException e) {
        String hdr = getErrorHeader(e);
        String msg = getErrorMessage(e, tokenNames);
        errors.add(hdr + " " + msg);
    }
    public List<String> getErrors() {
        return errors;
    }
}

However, this is a Java example that I can't seem to replicate in python (I can replicate the code, but just can't really seem to get it to run). Does anyone know how I might go about doing so?

Chris Covert
  • 2,684
  • 3
  • 24
  • 31

1 Answers1

5

I managed to find a solution to my problem:

@members {
def displayRecognitionError(self, tokenNames, e):
    # do something
    pass

antlr3.BaseRecognizer.displayRecognitionError = displayRecognitionError
}
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
Chris Covert
  • 2,684
  • 3
  • 24
  • 31