无法识别的字符的 ANTLR 打印索引(在行中)
ANTLR print index (in the line) of unrecognized char
在我的 ANTLR 语法中,我有以下语句:
@lexer::apifuncs
{
LEXER->rec->displayRecognitionError = recognition_error;
}
@parser::apifuncs
{
RECOGNIZER->displayRecognitionError = recognition_error;
}
C函数"recognition_error"(处理ANTLR抛出的识别错误)定义如下:
void recognition_error(pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_UINT8 *token_list)
{
pANTLR3_EXCEPTION ex;
pANTLR3_LEXER lexer;
pANTLR3_PARSER parser;
pANTLR3_TREE_PARSER tparser;
pANTLR3_INPUT_STREAM ins;
pANTLR3_INT_STREAM is;
pANTLR3_COMMON_TOKEN_STREAM cts;
pANTLR3_TREE_NODE_STREAM tns;
if (recognizer->type == ANTLR3_TYPE_LEXER)
{
lexer = (pANTLR3_LEXER) (recognizer->super);
ins = lexer->input;
is = ins->istream;
}
else if (recognizer->type == ANTLR3_TYPE_PARSER)
{
parser = (pANTLR3_PARSER) (recognizer->super);
cts = (pANTLR3_COMMON_TOKEN_STREAM)(parser->tstream->super);
is = parser->tstream->istream;
}
else if (recognizer->type == ANTLR3_TYPE_TREE_PARSER)
{
tparser = (pANTLR3_TREE_PARSER) (recognizer->super);
tns = tparser->ctnstream->tnstream;
is = tns->istream;
}
else
{
printf("UNKNOWN ERROR!\n");
return;
}
printf("INFO <%d> * <%d> * <%d> * <%d>\n", recognizer->state->exception->line, recognizer->state->exception->charPositionInLine, recognizer->state->exception->index, ((pANTLR3_COMMON_TOKEN) recognizer->state->exception->token)->getCharPositionInLine);
}
每次我使用此语法解析一些字符错误的文本时,函数 "recognition_error" 不会打印发现错误的字符的索引(它只是打印垃圾,如 -1 或0).
如何固定此语法 and/or C 函数以打印正确的索引?
可用的错误信息取决于异常的类型和它出现的令牌类型(例如,树重写生成的虚拟节点)。在 MySQL Workbench 中,我编写了一些错误处理代码,这些代码花费了一些时间来找到要报告的好信息。这里重要的是检查 exception->type
值并采取相应的行动。
在我的 ANTLR 语法中,我有以下语句:
@lexer::apifuncs
{
LEXER->rec->displayRecognitionError = recognition_error;
}
@parser::apifuncs
{
RECOGNIZER->displayRecognitionError = recognition_error;
}
C函数"recognition_error"(处理ANTLR抛出的识别错误)定义如下:
void recognition_error(pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_UINT8 *token_list)
{
pANTLR3_EXCEPTION ex;
pANTLR3_LEXER lexer;
pANTLR3_PARSER parser;
pANTLR3_TREE_PARSER tparser;
pANTLR3_INPUT_STREAM ins;
pANTLR3_INT_STREAM is;
pANTLR3_COMMON_TOKEN_STREAM cts;
pANTLR3_TREE_NODE_STREAM tns;
if (recognizer->type == ANTLR3_TYPE_LEXER)
{
lexer = (pANTLR3_LEXER) (recognizer->super);
ins = lexer->input;
is = ins->istream;
}
else if (recognizer->type == ANTLR3_TYPE_PARSER)
{
parser = (pANTLR3_PARSER) (recognizer->super);
cts = (pANTLR3_COMMON_TOKEN_STREAM)(parser->tstream->super);
is = parser->tstream->istream;
}
else if (recognizer->type == ANTLR3_TYPE_TREE_PARSER)
{
tparser = (pANTLR3_TREE_PARSER) (recognizer->super);
tns = tparser->ctnstream->tnstream;
is = tns->istream;
}
else
{
printf("UNKNOWN ERROR!\n");
return;
}
printf("INFO <%d> * <%d> * <%d> * <%d>\n", recognizer->state->exception->line, recognizer->state->exception->charPositionInLine, recognizer->state->exception->index, ((pANTLR3_COMMON_TOKEN) recognizer->state->exception->token)->getCharPositionInLine);
}
每次我使用此语法解析一些字符错误的文本时,函数 "recognition_error" 不会打印发现错误的字符的索引(它只是打印垃圾,如 -1 或0).
如何固定此语法 and/or C 函数以打印正确的索引?
可用的错误信息取决于异常的类型和它出现的令牌类型(例如,树重写生成的虚拟节点)。在 MySQL Workbench 中,我编写了一些错误处理代码,这些代码花费了一些时间来找到要报告的好信息。这里重要的是检查 exception->type
值并采取相应的行动。