使用 ANTLR 的嵌套布尔表达式解析器
我正在尝试解析嵌套布尔表达式并分别获取表达式中的各个条件。例如,如果输入字符串为:
(A = a OR B = b OR C = C AND ((D = d AND E = e) OR (F = f AND G = g)))
我想获得正确订单的条件。即
D = d 和 E = e OR F = F 和 G = G 和 A = A OR B = B 或 C = c
我正在使用ANTLR 4来解析输入文本,这是我的语法:
grammar SimpleBoolean;
rule_set : nestedCondition* EOF;
AND : 'AND' ;
OR : 'OR' ;
NOT : 'NOT';
TRUE : 'TRUE' ;
FALSE : 'FALSE' ;
GT : '>' ;
GE : '>=' ;
LT : '<' ;
LE : '<=' ;
EQ : '=' ;
LPAREN : '(' ;
RPAREN : ')' ;
DECIMAL : '-'?[0-9]+('.'[0-9]+)? ;
IDENTIFIER : [a-zA-Z_][a-zA-Z_0-9]* ;
WS : [ \r\t\u000C\n]+ -> skip;
nestedCondition : LPAREN condition+ RPAREN (binary nestedCondition)*;
condition: predicate (binary predicate)*
| predicate (binary component)*;
component: predicate | multiAttrComp;
multiAttrComp : LPAREN predicate (and predicate)+ RPAREN;
predicate : IDENTIFIER comparator IDENTIFIER;
comparator : GT | GE | LT | LE | EQ ;
binary: AND | OR ;
unary: NOT;
and: AND;
以下是我用来解析它的Java代码:
ANTLRInputStream inputStr = new ANTLRInputStream(input);
SimpleBooleanLexer lexer = new SimpleBooleanLexer(inputStr);
TokenStream tokens = new CommonTokenStream(lexer);
SimpleBooleanParser parser = new SimpleBooleanParser(tokens);
parser.getBuildParseTree();
ParseTree tree = parser.rule_set();
System.out.println(tree.toStringTree(parser));
输出为:
(rule_set (nestedCondition ( (condition (predicate A (comparator =) a) (binary OR) (component (predicate B (comparator =) b)) (binary OR) (component (predicate C (comparator =) c)) (binary AND) (component (multiAttrComp ( (predicate ( D (comparator =) d) (and AND) (predicate E (comparator =) e) ))) (binary OR) (component (multiAttrComp ( (predicate F (comparator =) f) (and AND) (predicate G (comparator =) g) )))) ) )) <EOF>)
我正在寻找有关如何解析此树以按正确顺序获取条件的帮助?在 ANTLR 3 中,我们可以指定 ^ 和 !决定如何构建树(参考此线程),但我了解到ANTLR 4不支持此功能。
有人可以建议我如何使用ANTLR创建的ParseTree在Java中以正确的顺序解析字符串吗?