如何加快pyparsing程序输出?

时间:2016-06-01 01:50:05

标签: python pyparsing

我有以下用于使用解析将**运算符更改为幂函数的pyparsing代码。 Paul McGuire帮我在这个论坛上编写了以下代码

# define some basic operand expressions
number = Regex(r'\d+(\.\d*)?([Ee][+-]?\d+)?')
ident = Word(alphas+'_', alphanums+'_')
#fn_call = ident + '(' + Optional(delimited_list(expr)) + ')'

# forward declare our overall expression, since a slice could 
# contain an arithmetic expression
expr = Forward()
slice_ref = '[' + expr + ']'

# define our arithmetic operand
operand = number | Combine(ident + Optional(slice_ref))
#operand = number | fn_call | Combine(ident + Optional(slice_ref))
inequalities = oneOf("< > >= <= = == !=")

# parse actions to convert parsed items
def convert_to_pow(tokens):
    tmp = tokens[0][:]
    ret = tmp.pop(-1)
    tmp.pop(-1)
    while tmp:
        base = tmp.pop(-1)
        # hack to handle '**' precedence ahead of '-'
        if base.startswith('-'):
            ret = '-power(%s,%s)' % (base[1:], ret)
        else:
            ret = 'power(%s,%s)' % (base, ret)
        if tmp:
            tmp.pop(-1)
    return ret

def unary_as_is(tokens):
    return '(%s)' % ''.join(tokens[0])

def as_is(tokens):
    return '%s' % ''.join(tokens[0])


# simplest infixNotation - may need to add a few more operators, but start with this for now
arith_expr = infixNotation( operand,
    [
    ('**', 2, opAssoc.LEFT, convert_to_pow),
    ('-', 1, opAssoc.RIGHT, unary_as_is),
    ((inequalities,inequalities), 3, opAssoc.LEFT, as_is),
    (inequalities, 2, opAssoc.LEFT, as_is),
    (oneOf("* /"), 2, opAssoc.LEFT, as_is),
    (oneOf("+ -"), 2, opAssoc.LEFT, as_is),
    (oneOf('and or'), 2, opAssoc.LEFT, as_is),
    ])
#('-', 1, opAssoc.RIGHT, as_is),
# now assign into forward-declared expr
expr <<= arith_expr.setParseAction(lambda t: '(%s)' % ''.join(t))


def translatepowerToFun(expression):
     xform = expr.transformString(expression)[1:-1]
     print xform 

上述计划正在给出预期结果

案例1

expression="(a+1)**2"
translatepowerToFun(expression)

输出

power((a+1),2)

案例2

expression="(-B)**2"
translatepowerToFun(expression)

输出

power(-B,2)

案例3

expression="-B**2"
translatepowerToFun(expression)

输出

-power(B,2)

案例4

expression="((Z**(_N1+1)-1)/(Z-1))*(Z-1))"
translatepowerToFun(expression)

输出

((power(Z,(_N1+1))-1)/(Z-1))*(Z-1))

但问题发生在案例4中。它返回预期结果但返回预期结果需要花费数小时。请建议我如何提高性能或加快流程。

0 个答案:

没有答案