1

I am trying to accumulate lexer errors when Lexing in run time. I have followed the exact way of achieving this for parser errors as in this answer. But when I try to do this to the Lexer.

class Class2 : IAntlrErrorListener<IToken> 
{
    public void SyntaxError(IRecognizer recognizer, IToken offendingSymbol, int line, int charPositionInLine, string msg, RecognitionException e)
    {
        Console.WriteLine("Error in lexer at line " + ":" + e.OffendingToken.Column + e.OffendingToken.Line + e.Message);
    }
}

and register this listener as below.

        Class2 z = new Class2();

        lexer.AddErrorListener(z);

But this gives a error which is unexpected.

Argument 1: cannot convert from 'CSSParserAntlr.Class2' to 'Antlr4.Runtime.IAntlrErrorListener<int>'    

and

The best overloaded method match for 'Antlr4.Runtime.Recognizer<int,Antlr4.Runtime.Atn.LexerATNSimulator>.AddErrorListener(Antlr4.Runtime.IAntlrErrorListener<int>)' has some invalid arguments

Please give me some reason why this is happening.

Community
  • 1
  • 1
diyoda_
  • 5,274
  • 8
  • 57
  • 89

1 Answers1

2

First, the reason you are getting this exception is because you need to declare Class2 as follows:

class Class2 : IAntlrErrorListener<int> 

in order to match the class signature of Lexer defined as follows:

public abstract class Lexer : Recognizer<int, LexerATNSimulator>, ITokenSource

The method signature for AddErrorListener is defined in Recognizer to match the Symbol type from the class definition as follows:

public abstract class Recognizer<Symbol, ATNInterpreter> : IRecognizer

This means that Lexer specifies "int" as the "Symbol" when extending Recognizer, and so the AddErrorListener method requires an IAntlrErrorListener of type int.

Now, the real answer to your question: don't write an error listener for a lexer. Errors in the lexing are the fault of the lex grammar, not of the user input. Error listeners are useful for handling bad user input in a Parser. Lexer grammars should be tested for coverage during development - i.e., all possible input will match some token. If you encounter a lex error during development, the default error listener will do just fine.

For more information, see the answer here: ANTLR4 Lexer error reporting (length of offending characters)

Community
  • 1
  • 1