我需要用pyparsing:iif(condition,value if true,value if false)
来解析这个,但是这种三元比较应该有另一个比较,我的意思是:
`iif(iif(condition1,value1,value2)>iif(condition2,value1,value2),value3,value4)`
我发现这个:
integer = Word(nums)
variable = Word(alphas, alphanums)
boolLiteral = oneOf("true false")
operand = boolLiteral | variable | integer
comparison_op = oneOf("== <= >= != < >")
QM,COLON = map(Literal,"?:")
expr = infixNotation(operand,
[
(comparison_op, 2, opAssoc.LEFT),
((QM,COLON), 3, opAssoc.LEFT),
])
它能够解析:
expr.parseString("(x==1? true: (y == 10? 100 : 200) )")
但我无法修改此代码以满足我的需要。我怎样才能做到这一点?你知道吗
更新
多亏了保罗先生,我想出了这个解决办法:
arith_expr = Forward()
iif = CaselessKeyword("iif")
open = Literal("(")
close = Literal(")")
var_name = pyparsing_common.identifier()
fn_call = Group(iif + open - Group(Optional(delimitedList(arith_expr))) + close)
arith_operand = fn_call | num
rel_comparison_operator = oneOf("< > <= >=")
eq_comparison_operator = oneOf("== !=")
plus_minus_operator = oneOf("+ -")
mult_div_operator = oneOf("* / %")
arith_expr <<= infixNotation(arith_operand,
[
# add other operators here - in descending order of precedence
# http://www.tutorialspoint.com/cprogramming/c_operators_precedence.htm
('-', 1, opAssoc.RIGHT),
(mult_div_operator, 2, opAssoc.LEFT,),
(plus_minus_operator, 2, opAssoc.LEFT,),
(rel_comparison_operator, 2, opAssoc.LEFT,),
(eq_comparison_operator, 2, opAssoc.LEFT,),
]
)
我在用我以前的一些规则。现在我投票结束这篇文章。你知道吗
正如@sepp2k在他的评论中提到的,您试图解析的字符串不是中缀表示法,尽管您最终可能会在中缀表示法中用作操作数。传递给
iif
的参数本身可能是中缀符号表达式。所以中缀符号肯定是这个解析器的一部分,但它不是解析iif
函数调用的部分。你知道吗下面是函数调用在pyparsing中的外观:
用于定义算术表达式的操作数本身可能包含函数调用,因此解析器的递归将要求您使用pyparsing的Forward类。你知道吗
这将允许您在完全定义
arith_expr
的样子之前,在其他子表达式中使用arith_expr
(就像我们刚才在fn\u call中所做的那样)。你知道吗直截了当地说,这里有一个解析
iif
函数的最小解析器:使用runTests,我们可以针对几个测试用例进行测试:
印刷品:
相关问题 更多 >
编程相关推荐