ANTLR exception handling with “$”, Java

风流意气都作罢 提交于 2019-12-01 11:57:47

问题


For my grammar in ANTLR, my java code can catch and print errors for inputs containing "$". In my implementation, I need to print out "success" for successful input. So, I do following in my java code,

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 returns numbers greater than 0 for wrong inputs in my case. For "int i;", the output is just;

parsing successful 

When I run my code for a input like int $i;, java code prints out error message with "parsing successful" because getNumberofSyntaxErrors() returns 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*/

回答1:


Possibly the lexer (or parser) tries to recover from (minor) errors and continues tokenizing or parsing. If you want to terminate whenever some illegal character occurs, the easiest is to create some sort of "fall-through" rule that is placed at the end of all your lexer rules which will match if none of the lexer rules above have matched, and let that rule throw an exception:

grammar T;

// parser rules

// lexer rules

/* last lexer rules */
FALL_THROUGH
  :  .  {throw new RuntimeException("Illegal character: " + getText());}
  ;


来源:https://stackoverflow.com/questions/7705845/antlr-exception-handling-with-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!