English 中文(简体)
我该如何校正一个标志 然后再从安特洛里安特林的 失败的上游恢复?
原标题:How do I correct a token and then recover from a failed predicate in ANTLR?

我有一套ANTLR规则 用来制造有上游测试的AST。它看起来像这个:

checked_rule
  : kwd=keyword name=name nl { noBadChars($name.start.getText()) }?
    -> ^(...)
  ;

如果 $name 有一些坏字, 那么我想有一个错误。 我做到现在这样:

catch [FailedPredicateException fpe] {
  BadNameException error = new BadNameException(input, $kwd.text, $name.start.getText());
  reportError(error);
  recover(input, error);
  retval.tree = (Object)adaptor.errorNode(input, retval.start, input.LT(-1), error);
}

最后一点是从 ANTRR 生成的代码中复制出来的, 所以也许这不是正确的方法。

不管怎么说,我想做的就是通过更正名字和再次尝试规则来从错误中恢复过来。在假代码中,我想说:

catch [FailedPredicateException fpe] {
  ...
  reportError(...)
  input.LT(-2) = removedBadChars($name.start.getText());
  return checked_rule();
}

所以这个错误仍然会到达用户, 但是我们只要尽可能少的干扰, 就可以继续分析。 我该怎么做呢?

最佳回答

为什么不简单地:

checked_rule
  : kwd=keyword name=name nl 
    {
      if(!noBadChars($name.start.getText())) {
        // print/report error
        String withoutBadChars = removedBadChars($name.start.getText());
        $name.start.setText(withoutBadChars);
        // and continue happily ever after...
      }
    }
    -> ^(...)
  ;

?????

问题回答

暂无回答




相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

How to get instance from string in C#?

Is it possible to get the property of a class from string and then set a value? Example: string s = "label1.text"; string value = "new value"; label1.text = value; <--and some code that makes ...

XML DOM parsing br tag

I need to parse a xml string to obtain the xml DOM, the problem I m facing is with the self closing html tag like <br /> giving me the error of Tag mismatch expected </br>. I m aware this ...

Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

Locating specific string and capturing data following it

I built a site a long time ago and now I want to place the data into a database without copying and pasting the 400+ pages that it has grown to so that I can make the site database driven. My site ...

热门标签