Antlr3 C 运行时相当于抛出错误报告异常的是什么

发布于 2024-10-13 05:47:39 字数 366 浏览 4 评论 0原文

我非常熟悉 Antlr 2.X 和 Antlr 3.1.X CSharp 和 python 目标。

然而,我现在被迫在项目中使用 Antlr 3 C 目标。

我的问题是如何报告语法或树语法中的错误。

考虑一个与标记匹配的规则,我们将其放入映射中。我们希望确保令牌是唯一的。通常,如果令牌已经在映射中,我会抛出异常,并在解析器之外捕获异常以报告错误。

Antlr C 运行时相当于以下规则是什么?

token_match: ID 
{
    if(mp.find($ID.Text))
        throw std::exception("Non unique token found");
}

I'm pretty familiar with Antlr 2.X and the Antlr 3.1.X CSharp and python targets.

However I'm now forced to use the Antlr 3 C target for a project.

My issue is how do I report errors in my grammar, or tree grammar.

Consider a rule that matches a token and we put it into a map. We want to ensure the token is unique. Normally I'd throw an exception if the token was already in the map and catch the exception outside of hte parser to report the error.

What is Antlr C runtime equivalent of the following rule?

token_match: ID 
{
    if(mp.find($ID.Text))
        throw std::exception("Non unique token found");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫九 2024-10-20 05:47:39

我建议使用您自己的方法来显示语义错误。但是,如果您为此需要留在 ANTLR 中:

如果您想创建自定义异常类型,首先您必须创建自己的错误处理程序。查看 antlr3baserecognizer.c:1000 的原始版本。

static void         
displayRecognitionErrorNew  (pANTLR3_BASE_RECOGNIZER recognizer,
                             pANTLR3_UINT8 * tokenNames)
{
...
    switch  (ex->type)
    {
    case    ANTLR3_UNWANTED_TOKEN_EXCEPTION:
...
    case    NUTF_EXCEPTION:
      printf("Non unique token found");
      break;

然后编写一些函数或规则代码来处理异常结构。查看antlr3baserecognizer.c:325来设置您的异常。

ex = antlr3ExceptionNew(ANTLR3_RECOGNITION_EXCEPTION,
                       (void *)ANTLR3_RECOGNITION_EX_NAME,
                       NULL,
                       ANTLR3_FALSE);

ex->type                = NUTF_EXCEPTION
ex->line                = ins->getLine (ins);
ex->charPositionInLine  = ins->getCharPositionInLine    (ins);  
ex->index               = is->index (is);
ex->streamName          = ins->fileName;
ex->message             = "That was totally unexpected";

接下来,当您检测到错误时,您需要实际抛出异常。我认为执行此操作的唯一方法是覆盖添加到代码中并从规则中调用它的mismatch()。

static  void
mismatchNew(pANTLR3_BASE_RECOGNIZER recognizer, ANTLR3_UINT32 ttype, pANTLR3_BITSET_LIST follow)
{
  ...
}

最后告诉 ANTLR 使用你的新函数。

@parser::apifuncs {
  RECOGNIZER->displayRecognitionError       = displayRecognitionErrorNew;
  RECOGNIZER->antlr3RecognitionExceptionNew = antlr3RecognitionExceptionNewNew;
  RECOGNIZER->mismatch                      = mismatchNew;
}

I'd recommend using your own methods to display semantic errors. But if you need to stay in ANTLR for this:

First you have to create your own error handler if you want to create custom exception types. Look at antlr3baserecognizer.c:1000 for the original.

static void         
displayRecognitionErrorNew  (pANTLR3_BASE_RECOGNIZER recognizer,
                             pANTLR3_UINT8 * tokenNames)
{
...
    switch  (ex->type)
    {
    case    ANTLR3_UNWANTED_TOKEN_EXCEPTION:
...
    case    NUTF_EXCEPTION:
      printf("Non unique token found");
      break;

Then code some function or rule code to process an exception structure. Look at antlr3baserecognizer.c:325 to setup your exception.

ex = antlr3ExceptionNew(ANTLR3_RECOGNITION_EXCEPTION,
                       (void *)ANTLR3_RECOGNITION_EX_NAME,
                       NULL,
                       ANTLR3_FALSE);

ex->type                = NUTF_EXCEPTION
ex->line                = ins->getLine (ins);
ex->charPositionInLine  = ins->getCharPositionInLine    (ins);  
ex->index               = is->index (is);
ex->streamName          = ins->fileName;
ex->message             = "That was totally unexpected";

Next you need to actually throw your exception when you detect the error. I think the only way to do this is to override mismatch() adding in your code and calling it from your rule.

static  void
mismatchNew(pANTLR3_BASE_RECOGNIZER recognizer, ANTLR3_UINT32 ttype, pANTLR3_BITSET_LIST follow)
{
  ...
}

Finally tell ANTLR to use your new functions.

@parser::apifuncs {
  RECOGNIZER->displayRecognitionError       = displayRecognitionErrorNew;
  RECOGNIZER->antlr3RecognitionExceptionNew = antlr3RecognitionExceptionNewNew;
  RECOGNIZER->mismatch                      = mismatchNew;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文