2

我正在使用 Irony 解析 DSL,它具有可以与 AND 和 OR 组合的表达式:

/* snip */

RegisterOperators(4, orKeyword);
RegisterOperators(5, andKeyword);
RegisterOperators(9, lessThanOperator, lessEqualOperator, equalOperator, notEqualOperator, moreThanOperator, moreEqualOperator);

infixOperator.Rule = andKeyword | orKeyword;
expression_in_parens.Rule = L_PAR + expression + R_PAR;
primaryExpression.Rule = expression_in_parens | methodCall | identifier | numberIdentifier | stringIdentifier;
unaryExpression.Rule = notKeyword + L_PAR + expression + R_PAR;
binaryExpression.Rule = expression + infixOperator + expression;
compareExpression.Rule = expression + compareOperator + expression + ReduceHere();
expression.Rule = primaryExpression | unaryExpression | compareExpression | binaryExpression;

我有以下表达式:“A and (B and C or D)”。它被解析为“A and (B and (C or D))”,但我需要它是“A and ((B and C) or D)”。

我认为将 and 关键字注册为具有比 or 关键字更高优先级的运算符会这样做。我错过了什么?

4

1 回答 1

1

我通过为 AND 和 OR 创建单独的规则并将它们按正确的顺序排列来使其工作:

binaryAndExpression.Rule = expression + andKeyword + expression;
binaryOrExpression.Rule = expression + orKeyword + expression;
compareExpression.Rule = expression + compareOperator + expression + ReduceHere();
expression.Rule = compareExpression | binaryAndExpression | binaryOrExpression | unaryExpression | primaryExpression;
于 2012-09-19T10:58:31.200 回答