【发布时间】:2019-07-07 22:42:19
【问题描述】:
我是 ANTLR4 的新手,我正在尝试使用它来解析我们从外部规则生成器获得的规则字符串。 规则的形式为 [attribute operator value(s)] ANDed 和 ORed 多次。
我能够解析更简单的,例如:-
[divison3__c == ('AH Marketing', 'Asset Protection Solutions')] OR [hrstatus__c == ('Active')]
但是,一旦我遇到了复合 AND 和 OR 的问题,例如:-
[[divison3__c == ('AH Marketing', 'Asset Protection Solutions')] OR [hrstatus__c == ('Active')]] AND [[hiredate__c > ('2000-01-01')] OR [custom10__c == ('ABCD')]]
下面提到了我的适用于简单规则的语法。对于在解析由复合 AND 和 OR 组成的规则方面需要做什么的任何指示,我将不胜感激。
// Our grammar is called Rules.
grammar Rules;
// Rules
start: grouprules;
grouprules: grouprule (andor grouprule)* EOF;
grouprule: L_SB expression R_SB;
expression: USERATTRIBUTE operator values;
operator: EQ | NE | GE | GT | LE | LT;
values: '(' value (',' value )* ')';
value: STRING | date;
date: '\'' DATE '\'';
andor: AND | OR;
// Tokens
EQ: '==';
NE: '!=';
GT: '>';
GE: '>=';
LT: '<';
LE: '<=';
L_SB: '[';
R_SB: ']';
AND: [aA][nN][dD];
OR: [oO][rR];
NUMBER: [0-9]+;
USERATTRIBUTE: [a-zA-Z][a-zA-Z0-9_]*;
STRING: '\'' ~('"')* '\'' ;
// Not perfect
DATE: [0-9][0-9][0-9][0-9][-][0-1][0-9][-][0-3][0-9] ;
// WS represents a whitespace, which is ignored entirely by skip.
WS: [ \t\u000C\r\n]+ -> skip;
规则:
[divison3__c == ('AH Marketing', 'Asset Protection Solutions')] OR [hrstatus__c == ('Active')]
成功的结果:
(grouprules (grouprule [ [ hiredate__c (operator >) (values ( (value '2000-01-01')] AND [divison3__c == ('AH Marketing', 'Asset Protection Solutions') )) ]) ] <EOF>)
复合规则:
[[divison3__c == ('AH Marketing', 'Asset Protection Solutions')] OR [hrstatus__c == ('Active')]] AND [[hiredate__c > ('2000-01-01')] OR [custom10__c == ('ABCD')]]
不成功的结果:
line 1:1 extraneous input '[' expecting USERATTRIBUTE
line 1:162 extraneous input ']' expecting {<EOF>, AND, OR}
(grouprules (grouprule [ (expression [ divison3__c (operator ==) (values ( (value 'AH Marketing', 'Asset Protection Solutions')] OR [hrstatus__c == ('Active')]] AND [[hiredate__c > ('2000-01-01')] OR [custom10__c == ('ABCD') ))) ]) ] <EOF>)
【问题讨论】:
标签: antlr4