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*/