ANTLR exception handling with “$”, Java

2020-03-30 05:22发布

问题:

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());}
  ;