Skip to content Skip to sidebar Skip to footer

How To Catch Extraneous Input In ANTLR4 In Python3?

Yes, this is nearly a duplicate of How to catch extraneous input in ANTLR4? - but that is in the case of Java, I need a solution for Python, and porting the Java solution is just n

Solution 1:

The solution; both the Lexer and the Parser are "Recognizers". The Recognizer is where the listener listens.

In consequence, we have two types of errors, Lexing errors and Parsing errors. I was only attaching my listener to the Lexer, I needed to also attach it to the Parser. Revised code:

        ...
        lexer:PlSqlLexer = PlSqlLexer(st)
        # Add your error listener to the lexer if required
        lexer.removeErrorListeners()
        lexer.addErrorListener( my_listener )
        
        stream = CommonTokenStream(lexer)
        
        parser = PlSqlParser(stream)
        # Add your error listener to the _parser_ if required
        parser.removeErrorListeners()
        parser.addErrorListener( my_listener )
        ...

Post a Comment for "How To Catch Extraneous Input In ANTLR4 In Python3?"