简体   繁体   English

具有讽刺意味的.NET:表达式运算符优先级

[英]Irony .NET: Expression operator precedence

I'm using Irony to parse a DSL, which has expressions that can be combined with ANDs and ORs: 我正在使用Irony来解析DSL,该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;

I have the following expression: "A and (B and C or D)". 我有以下表达:“ A和(B和C或D)”。 It's parsed as "A and (B and (C or D))", but I need it to be "A and ((B and C) or D)". 它被解析为“ A和(B和(C或D))”,但我需要它为“ A和((B和C)或D)”。

I thought registering the and keyword as an operator with a higher priority than the or keyword would do that. 我认为将and关键字注册为比or关键字具有更高优先级的运算符可以做到这一点。 What am I missing? 我想念什么?

I got it working by creating separate rules for AND and OR, and putting those in the right order: 我通过为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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM