gpt4 book ai didi

c# - 具有讽刺意味的.NET : Expression operator precedence

转载 作者:太空宇宙 更新时间:2023-11-03 13:55:14 25 4
gpt4 key购买 nike

我正在使用 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 和(B 和(C 或 D))”,但我需要它是“A 和((B 和 C)或 D)”。

我认为将 and 关键字注册为优先级高于 or 关键字的运算符可以做到这一点。我错过了什么?

最佳答案

我通过为 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;

关于c# - 具有讽刺意味的.NET : Expression operator precedence,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12474539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com