我使用 AST 树使用 Antlr 3 开发了一个复杂的语法。ANTLR 生成 Lexer 和 Parser。问题是,当用户输入无效的语法时,例如,语法需要';'。用户没有输入这个,然后在我的 Eclipse IDE 中我得到以下异常:
line 1:24 mismatched input '<EOF>' expecting ';'
这个异常怎么处理,因为我试图捕捉这个异常,但是这个异常没有被捕捉到。这是一个例外吗?我似乎不明白为什么没有捕捉到这个异常。我试图找出答案,但是 Antlr 网站似乎已经关闭了一段时间。
我查看了以下内容:使用“$”进行 ANTLR 异常处理,Java并遵循该示例,但是当 Lexer 通过添加 RuntimeException() 生成代码时,我得到了无法访问的代码。
我不知道该怎么办。
当我尝试从解析器获取语法错误的数量时,它显示为 0。
编辑:
我通过查看找到了一个可行的解决方案:ANTLR not throwing errors on invalid input
但是,当我尝试取回异常消息时,它为空。我是否正确设置了所有内容?请参阅示例语法:
grammar i;
options {
output=AST;
}
@header {
package com.data;
}
@rulecatch {
catch(RecognitionException e) {
throw e;
}
}
// by having these below it makes no difference
/**@parser::members {
@Override
public void reportError(RecognitionException e) {
throw new RuntimeException("Exception : " + " " + e.getMessage());
}
}
@lexer::members {
@Override
public void reportError(RecognitionException e) {
throw new RuntimeException("Exception : " + " " + e.getMessage());
}
}*/
编辑:
请看看我到目前为止有什么:
grammar i;
options {
output=AST;
}
@header {
package com.data;
}
@rulecatch {
// ANTLR does not generate its normal rule try/catch
catch(RecognitionException e) {
throw e;
}
}
@parser::members {
@Override
public void displayRecognitionError(String[] tokenNames, RecognitionException e) {
String hdr = getErrorHeader(e);
String msg = getErrorMessage(e, tokenNames);
throw new RuntimeException(hdr + ":" + msg);
}
}
@lexer::members {
@Override
public void displayRecognitionError(String[] tokenNames, RecognitionException e) {
String hdr = getErrorHeader(e);
String msg = getErrorMessage(e, tokenNames);
throw new RuntimeException(hdr + ":" + msg);
}
}
operatorLogic : 'AND' | 'OR';
value : STRING;
query : (select)*;
select : 'SELECT'^ functions 'FROM table' filters?';';
operator : '=' | '!=' | '<' | '>' | '<=' | '>=';
filters : 'WHERE'^ conditions;
members : STRING operator value;
conditions : (members (operatorLogic members)*);
functions : '*';
STRING : ('a'..'z'|'A'..'Z')+;
WS : (' '|'\t'|'\f'|'\n'|'\r')+ {skip();}; // handle white space between keywords
public class Processor {
public Processor() {
}
/**
* This method builds the MQL Parser.
* @param args the args.
* @return the built IParser.
*/
private IParser buildMQLParser(String query) {
CharStream cs = new ANTLRStringStream(query);
// the input needs to be lexed
ILexer lexer = new ILexer(cs);
CommonTokenStream tokens = new CommonTokenStream();
IParser parser = new IParser(tokens);
tokens.setTokenSource(lexer);
// use the ASTTreeAdaptor so that the grammar is aware to build tree in AST format
parser.setTreeAdaptor((TreeAdaptor) new ASTTreeAdaptor().getASTTreeAdaptor());
return parser;
}
/**
* This method parses the MQL query.
* @param query the query.
*/
public void parseMQL(String query) {
IParser parser = buildMQLParser(query);
CommonTree commonTree = null;
try {
commonTree = (CommonTree) parser.query().getTree();
}
catch(Exception e) {
System.out.println("Exception :" + " " + e.getMessage());
}
}
}
public class ASTTreeAdaptor {
public ASTTreeAdaptor() {
}
/**
* This method is used to create a TreeAdaptor.
* @return a treeAdaptor.
*/
public Object getASTTreeAdaptor() {
TreeAdaptor treeAdaptor = new CommonTreeAdaptor() {
public Object create(Token payload) {
return new CommonTree(payload);
}
};
return treeAdaptor;
}
}
所以当我输入以下内容时:SELECT * FROM table
没有 ';' 我得到一个 MismatchedTokenException:
catch(Exception e) {
System.out.println("Exception : " + " " e);
}
当我尝试:
e.getMessage();
它返回空值。