I have looked through the python classes and noticed that they dont have the methods that the java one has for adding and removing a error listener, this maybe a bug in ANTLR, however python being python you are allowed to modify the members without requiring a setter as such like in the following example:
I run the example by performing a : antlr4 -Dlanguage=Python2 AlmostEmpty.g4 and then by typing in main.py
AlmostEmpty.g4
grammar AlmostEmpty;
animals: (CAT | DOG | SHEEP ) EOF;
WS: [ \n\r]+ -> skip;
CAT: [cC] [aA] [tT];
DOG: [dD] [oO] [gG];
SHEEP: [sS] [hH] [eE] [pP];
main.py
from antlr4 import *
import sys
from AlmostEmptyLexer import AlmostEmptyLexer
from AlmostEmptyParser import AlmostEmptyParser
from antlr4.error.ErrorListener import ErrorListener
class MyErrorListener( ErrorListener ):
def __init__(self):
super(MyErrorListener, self).__init__()
def syntaxError(self, recognizer, offendingSymbol, line, column, msg, e):
raise Exception("Oh no!!")
def reportAmbiguity(self, recognizer, dfa, startIndex, stopIndex, exact, ambigAlts, configs):
raise Exception("Oh no!!")
def reportAttemptingFullContext(self, recognizer, dfa, startIndex, stopIndex, conflictingAlts, configs):
raise Exception("Oh no!!")
def reportContextSensitivity(self, recognizer, dfa, startIndex, stopIndex, prediction, configs):
raise Exception("Oh no!!")
if __name__ == "__main__":
inputStream = StdinStream( )
lexer = AlmostEmptyLexer(inputStream)
# Add your error listener to the lexer if required
#lexer.removeErrorListeners()
#lexer._listeners = [ MyErrorListener() ]
stream = CommonTokenStream(lexer)
parser = AlmostEmptyParser(stream)
# As mentioned in the comments by @Tim Stewart instead of doing this:
# parser._listeners = [ MyErrorListener() ]
# you can do this:
parser.addErrorListener( MyErrorListener() )
tree = parser.animals()