pyparsing如何使用infixNotation表示iif(cond,if true,if false)

2024-03-29 10:46:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要用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,),
                                    ]
                                    )

我在用我以前的一些规则。现在我投票结束这篇文章。你知道吗


Tags: falsetrueifvaluepyparsingleftoperatorcomparison
1条回答
网友
1楼 · 发布于 2024-03-29 10:46:09

正如@sepp2k在他的评论中提到的,您试图解析的字符串不是中缀表示法,尽管您最终可能会在中缀表示法中用作操作数。传递给iif的参数本身可能是中缀符号表达式。所以中缀符号肯定是这个解析器的一部分,但它不是解析iif函数调用的部分。你知道吗

下面是函数调用在pyparsing中的外观:

fn_call = pp.Group(var_name + LPAREN - pp.Group(pp.Optional(pp.delimitedList(arith_expr))) + RPAREN)

用于定义算术表达式的操作数本身可能包含函数调用,因此解析器的递归将要求您使用pyparsing的Forward类。你知道吗

arith_expr = pp.Forward()

这将允许您在完全定义arith_expr的样子之前,在其他子表达式中使用arith_expr(就像我们刚才在fn\u call中所做的那样)。你知道吗

直截了当地说,这里有一个解析iif函数的最小解析器:

import pyparsing as pp

# for recursive infix notations, or those with many precedence levels, it is best to enable packrat parsing
pp.ParserElement.enablePackrat()
LPAREN, RPAREN = map(pp.Suppress, "()")

arith_expr= pp.Forward()

var_name = pp.pyparsing_common.identifier()
integer = pp.pyparsing_common.integer()
fn_call = pp.Group(var_name + LPAREN - pp.Group(pp.Optional(pp.delimitedList(arith_expr))) + RPAREN)
arith_operand = fn_call | var_name | integer

rel_comparison_operator = pp.oneOf("< > <= >=")
eq_comparison_operator = pp.oneOf("== !=")
plus_minus_operator = pp.oneOf("+ -")
mult_div_operator = pp.oneOf("* / %")

arith_expr <<= pp.infixNotation(arith_operand,
                                [
                                    # add other operators here - in descending order of precedence
                                    # http://www.tutorialspoint.com/cprogramming/c_operators_precedence.htm
                                    (mult_div_operator, 2, pp.opAssoc.LEFT,),
                                    (plus_minus_operator, 2, pp.opAssoc.LEFT,),
                                    (rel_comparison_operator, 2, pp.opAssoc.LEFT,),
                                    (eq_comparison_operator, 2, pp.opAssoc.LEFT,),
                                ]
                                )

使用runTests,我们可以针对几个测试用例进行测试:

tests = """\
    cos(60)
    sqrt(1 - sin(60) * sin(60))
    divmod(a, 100)
    iif(iif(condition1,value1,value2)>iif(condition2,value1,value2),value3,value4)
    """
arith_expr.runTests(tests)

印刷品:

cos(60)
[['cos', [60]]]
[0]:
  ['cos', [60]]
  [0]:
    cos
  [1]:
    [60]


sqrt(1 - sin(60) * sin(60))
[['sqrt', [[1, '-', [['sin', [60]], '*', ['sin', [60]]]]]]]
[0]:
  ['sqrt', [[1, '-', [['sin', [60]], '*', ['sin', [60]]]]]]
  [0]:
    sqrt
  [1]:
    [[1, '-', [['sin', [60]], '*', ['sin', [60]]]]]
    [0]:
      [1, '-', [['sin', [60]], '*', ['sin', [60]]]]
      [0]:
        1
      [1]:
        -
      [2]:
        [['sin', [60]], '*', ['sin', [60]]]
        [0]:
          ['sin', [60]]
          [0]:
            sin
          [1]:
            [60]
        [1]:
          *
        [2]:
          ['sin', [60]]
          [0]:
            sin
          [1]:
            [60]


divmod(a, 100)
[['divmod', ['a', 100]]]
[0]:
  ['divmod', ['a', 100]]
  [0]:
    divmod
  [1]:
    ['a', 100]


iif(iif(condition1,value1,value2)>iif(condition2,value1,value2),value3,value4)
[['iif', [[['iif', ['condition1', 'value1', 'value2']], '>', ['iif', ['condition2', 'value1', 'value2']]], 'value3', 'value4']]]
[0]:
  ['iif', [[['iif', ['condition1', 'value1', 'value2']], '>', ['iif', ['condition2', 'value1', 'value2']]], 'value3', 'value4']]
  [0]:
    iif
  [1]:
    [[['iif', ['condition1', 'value1', 'value2']], '>', ['iif', ['condition2', 'value1', 'value2']]], 'value3', 'value4']
    [0]:
      [['iif', ['condition1', 'value1', 'value2']], '>', ['iif', ['condition2', 'value1', 'value2']]]
      [0]:
        ['iif', ['condition1', 'value1', 'value2']]
        [0]:
          iif
        [1]:
          ['condition1', 'value1', 'value2']
      [1]:
        >
      [2]:
        ['iif', ['condition2', 'value1', 'value2']]
        [0]:
          iif
        [1]:
          ['condition2', 'value1', 'value2']
    [1]:
      value3
    [2]:
      value4

相关问题 更多 >