我想通过antlr3解析表达式,但遇到了一些错误。以下是我的语法文件:
grammar Expr;
options {
output = AST;
ASTLabelType = CommonTree;
language = Java;
k =3;
// backtrack=true;
}
tokens {
DIVIDE = '/' ;
PLUS = '+' ;
MINUS = '-' ;
STAR = '*' ;
MOD = '%' ;
AMPERSAND = '&' ;
TILDE = '~' ;
BITWISEOR = '|' ;
COMMA = ',';
DOT = '.';
LPAREN = '(' ;
RPAREN = ')' ;
THRESHOLD = '>';
EQUIValence = '=';
AND = 'AND' ;
OR = 'OR' ;
TRUE = 'TRUE';
FALSE = 'FALSE';
DOUBLE_QUOTE = '\"';
SINGLE_QUOTE = '\'';
}
// LITERALS
fragment
Letter
: 'a'..'z' | 'A'..'Z'
;
fragment
Digit
:
'0'..'9'
;
fragment
Exponent
:
('e' | 'E') ( PLUS|MINUS )? (Digit)+
;
Number
:
(Digit)+ ( DOT (Digit)* (Exponent)? | Exponent)?
;
fragment
UnquotedString
:
( ~(SINGLE_QUOTE|DOUBLE_QUOTE|' '|'\t'|'\n'|LPAREN|RPAREN|COMMA))+
;
fragment
QuotedLiteral
:
DOUBLE_QUOTE ( ~(DOUBLE_QUOTE|'\\') | ('\\' .) )* DOUBLE_QUOTE
;
Parameter
:
UnquotedString | QuotedLiteral
;
WS : (' '|'\r'|'\t'|'\n'|'\u000C')* {$channel=HIDDEN;}
;
/********************************************rule******************************************/
searchCondition
:
// subCondition (( AND | OR ) subCondition)? EOF?
andExpr (OR^ andExpr)*
;
andExpr
:
subCondition (AND^ subCondition)*
;
subCondition
:
LPAREN searchCondition RPAREN
| atom
;
atom returns [QueryBuilder result]
:
//p0=expression (comparisonOperator expression)?
p0=expression
{
$result = $p0.result;
}
;
comparisonOperator
:
THRESHOLD | EQUIValence
;
expression returns [QueryBuilder result]
:
//p0=subExpression (binaryOperator subExpression)*
p0=subExpression
;
subExpression returns [QueryBuilder result]
:
p0=Parameter
;
输入QQ OR (UU AND WW) OR XX
时,解析结果不完整,右括号后的内容不匹配。