1
對於我在ANTLR中的語法,我的java代碼可以捕獲並打印包含「$」的輸入的錯誤。在我的實施中,我需要打印出成功輸入的「成功」。所以,我按照我的Java代碼,大於0爲我的情況下,輸入錯誤
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回報數字。對於「int i;」,輸出只是;
parsing successful
當我運行我的代碼,如int $一個輸入我;, 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*/
巴特,非常感謝。 –
不客氣John。 –