如何在c++;antlr4的目标?
为了在antlr4 java目标中以不同的方式报告错误,我们执行以下操作: (1) 定义新的侦听器:如何在c++;antlr4的目标?,antlr4,Antlr4,为了在antlr4 java目标中以不同的方式报告错误,我们执行以下操作: (1) 定义新的侦听器: class DescriptiveErrorListener extends BaseErrorListener { public static DescriptiveErrorListener INSTANCE = new DescriptiveErrorListener(); @Override public void syntaxError(Reco
class DescriptiveErrorListener extends BaseErrorListener {
public static DescriptiveErrorListener INSTANCE =
new DescriptiveErrorListener();
@Override
public void syntaxError(Recognizer<?, ?> recognizer,
Object offendingSymbol,
int line, int charPositionInLine,
String msg, RecognitionException e)
{
String printMsg = String.format("ERR: %s:%d:%d: %s",
recognizer.getInputStream().getSourceName(), line,
charPositionInLine+1, msg);
System.err.println(printMsg);
}
}
<> C++中C++中的对应代码是什么?,它几乎是相同的(除了语言特定的方面)。我使用的代码:
struct MySQLParserContextImpl : public MySQLParserContext {
ANTLRInputStream input;
MySQLLexer lexer;
CommonTokenStream tokens;
MySQLParser parser;
LexerErrorListener lexerErrorListener;
ParserErrorListener parserErrorListener;
...
MySQLParserContextImpl(...)
: lexer(&input), tokens(&lexer), parser(&tokens), lexerErrorListener(this), parserErrorListener(this),... {
...
lexer.removeErrorListeners();
lexer.addErrorListener(&lexerErrorListener);
parser.removeParseListeners();
parser.removeErrorListeners();
parser.addErrorListener(&parserErrorListener);
}
...
}
听众们:
class LexerErrorListener : public BaseErrorListener {
public:
MySQLParserContextImpl *owner;
LexerErrorListener(MySQLParserContextImpl *aOwner) : owner(aOwner) {}
virtual void syntaxError(Recognizer *recognizer, Token *offendingSymbol, size_t line, size_t charPositionInLine,
const std::string &msg, std::exception_ptr e) override;
};
class ParserErrorListener : public BaseErrorListener {
public:
MySQLParserContextImpl *owner;
ParserErrorListener(MySQLParserContextImpl *aOwner) : owner(aOwner) {}
virtual void syntaxError(Recognizer *recognizer, Token *offendingSymbol, size_t line, size_t charPositionInLine,
const std::string &msg, std::exception_ptr e) override;
};
在C++中,它几乎是相同的(除了语言特定的方面)。我使用的代码:
struct MySQLParserContextImpl : public MySQLParserContext {
ANTLRInputStream input;
MySQLLexer lexer;
CommonTokenStream tokens;
MySQLParser parser;
LexerErrorListener lexerErrorListener;
ParserErrorListener parserErrorListener;
...
MySQLParserContextImpl(...)
: lexer(&input), tokens(&lexer), parser(&tokens), lexerErrorListener(this), parserErrorListener(this),... {
...
lexer.removeErrorListeners();
lexer.addErrorListener(&lexerErrorListener);
parser.removeParseListeners();
parser.removeErrorListeners();
parser.addErrorListener(&parserErrorListener);
}
...
}
听众们:
class LexerErrorListener : public BaseErrorListener {
public:
MySQLParserContextImpl *owner;
LexerErrorListener(MySQLParserContextImpl *aOwner) : owner(aOwner) {}
virtual void syntaxError(Recognizer *recognizer, Token *offendingSymbol, size_t line, size_t charPositionInLine,
const std::string &msg, std::exception_ptr e) override;
};
class ParserErrorListener : public BaseErrorListener {
public:
MySQLParserContextImpl *owner;
ParserErrorListener(MySQLParserContextImpl *aOwner) : owner(aOwner) {}
virtual void syntaxError(Recognizer *recognizer, Token *offendingSymbol, size_t line, size_t charPositionInLine,
const std::string &msg, std::exception_ptr e) override;
};