ANTLRv3:C 目标错误处理。改变 System.err 出来

ANTLRv3: C target error handling. Changing System.err out

使用 ANTLRv3 我可以构建我的解析器,默认情况下它通常在控制台上将错误字符串打印到 System.err。

但是,我有不同的输出来打印错误,例如打印到文件(用于记录),即我不在控制台上工作。我想知道在这种情况下我必须做什么来打印错误字符串?我想我只需要更改负责打印错误字符串的函数的定义,但我找不到它。

从这个文档 https://theantlrguy.atlassian.net/wiki/spaces/ANTLR3/pages/2687258/Error+reporting+and+recovery 我可以发现我只需要覆盖 emitErrorMessage() 但我无法在我的 C 目标中找到它所以我有点困惑它是如何工作的。

我正在研究 ANTLR 3.5.2 C 目标。使用 ANTLRv4 不是一个选项。

不幸的是,我找不到很多使用 C 的参考项目,所以我无法从中学习。

如有任何帮助,我将不胜感激。

您不能覆盖 C 函数(因为它在 C 目标中不是虚拟的),但您可以用您自己的错误处理程序替换错误处理程序以转而转发错误。在 old MySQL Workbench parser code.

中查看我是如何做到的
@parser::postinclude {
#ifdef __cplusplus
extern "C" { 
#endif

  // Custom error reporting function.
  void onMySQLParseError(struct ANTLR3_BASE_RECOGNIZER_struct *recognizer, pANTLR3_UINT8 *tokenNames); 

#ifdef __cplusplus
};
#endif
}

@parser::members {
}

@parser::apifuncs
{
 // Install custom error collector for the front end.
 RECOGNIZER->displayRecognitionError = onMySQLParseError;
}

函数 onMySQLParseError 显然是您必须在 C 代码中更改和实现的内容。