Reputation: 2834
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?
Upvotes: 4
Views: 621
Reputation: 2834
I managed to find a solution to my problem:
@members {
def displayRecognitionError(self, tokenNames, e):
# do something
pass
antlr3.BaseRecognizer.displayRecognitionError = displayRecognitionError
}
Upvotes: 5