0
我想解析由antlr3一個表情,但遇到了一些和error.The以下是我的語法文件: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
,解析結果是不完整的對不對括號後的內容沒有匹配。
爲什麼不只是看各種語法中的許多表達式規則之一。支持表達式的語言的每種語法都有這樣的規則。 –