开发者

What is the meaning of this ANTLR3 log message?

Antlr3 produces the following output to System.out, while doing everything else correctly:

line 0:0 null
line 0:0 null
line 0:0 null
...

What is it about?

ps. The problem is that during parsing time to time I through RecognitionException(). Looks like null is the result of this. Instead of null there should be some meaningful message, but RecognitionException doesn't have such a constructor. What to do?


grammar Bar;
document: ( CHAR { Foo.validate($CHAR.text); } )+ EOF;
CHAR: 'a'..'开发者_如何转开发z';

Foo class somewhere in the same package:

public class Foo {
  public static void validate(String txt) throws RecognitionException {
    if ("q".equals(txt)) {
      throw new RecognitionException();
    }
  }
}

Now a unit test:

public class BarTest {
  @Test public void testEverything() throws Exception {
    this.parse("abc"); // valid
    this.parse("abcq"); // invalid
  }
  private void parse(String txt) {
    CharStream input = new ANTLRStringStream(txt);
    BarLexer lexer = new BarLexer(input);
    TokenStream tokens = new CommonTokenStream(lexer);
    BarParser parser = new BarParser(tokens);
    parser.document();
  }
}

The output is:

line 0:0 null


A RecognitionException is supposed to be thrown by the parser itself. Such exceptions have all kind of information that is updated while the parser goes through the token stream (line number, index, current token, etc.). So, you shouldn't throw them yourself.

Have a look at these two Wiki articles that handles error reporting and recovery:

  • Error reporting and recovery
  • Custom Syntax Error Recovery
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜