【发布时间】:2011-10-09 19:09:59
【问题描述】:
对于我在 ANTLR 中的语法,我的 java 代码可以捕获并打印包含“$”的输入的错误。在我的实现中,我需要为成功输入打印出“成功”。所以,我在我的 java 代码中执行以下操作,
CharStream charStream = new ANTLRFileStream(filePath);
myLexer lexer = new myLexer(charStream);
TokenStream tokens = new CommonTokenStream(lexer);
myParser parser = new myParser(tokens);
/*if there is an error throws an exception message*/
parser.program();
/*if there is an error find how many, if 0 then print success,*/
int errorsCount = parser.getNumberOfSyntaxErrors();
if(errorsCount == 0){
System.out.println("parsing successful");
}
getNumberofSyntaxErrors 在我的例子中返回大于 0 的错误输入。对于“int i;”,输出只是;
parsing successful
当我为 int $i; 之类的输入运行代码时,java 代码会打印出带有“解析成功”的错误消息,因为 getNumberofSyntaxErrors() 返回 0;
line 1:4 no viable alternative at character '$' /*this is what I expect to see*/
parsing successful /*this is not what i expect to see*/
【问题讨论】:
标签: java error-handling antlr