c# 4.0 - Accumulating Lexer errors in ANTLR 4 -
i trying accumulate lexer errors when lexing in run time. have followed exact way of achieving parser errors in this answer. when try 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 @ line " + ":" + e.offendingtoken.column + e.offendingtoken.line + e.message); } }
and register listener below.
class2 z = new class2(); lexer.adderrorlistener(z);
but gives error unexpected.
argument 1: cannot convert 'cssparserantlr.class2' 'antlr4.runtime.iantlrerrorlistener<int>'
and
the best overloaded method match 'antlr4.runtime.recognizer<int,antlr4.runtime.atn.lexeratnsimulator>.adderrorlistener(antlr4.runtime.iantlrerrorlistener<int>)' has invalid arguments
please give me reason why happening.
first, reason getting exception because need declare class2 follows:
class class2 : iantlrerrorlistener<int>
in order match class signature of lexer defined follows:
public abstract class lexer : recognizer<int, lexeratnsimulator>, itokensource
the method signature adderrorlistener defined in recognizer match symbol type class definition follows:
public abstract class recognizer<symbol, atninterpreter> : irecognizer
this means lexer specifies "int" "symbol" when extending recognizer, , adderrorlistener method requires iantlrerrorlistener of type int.
now, real answer question: don't write error listener lexer. errors in lexing fault of lex grammar, not of user input. error listeners useful handling bad user input in parser. lexer grammars should tested coverage during development - i.e., possible input match token. if encounter lex error during development, default error listener fine.
for more information, see answer here: antlr4 lexer error reporting (length of offending characters)
Comments
Post a Comment