我有以下用于使用解析将**运算符更改为幂函数的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 + ']'
slice_ref = '[' + expr + ZeroOrMore("," + 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,
[
('-', 1, opAssoc.RIGHT, as_is),
('**', 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))
#Changing function of the formate f(n) to f[n]. It assist the pasring
def transferToFunctionSyntax(expression):
if "(" in expression and ")" in expression:
p = regex.compile(r'\b[a-zA-Z_]\w*(\((?>[^()]|(?1))*\))')
result=(p.sub(lambda m: m.group().replace("(", "[").replace(")", "]"), expression))
else:
result=expression
return result
def translatepowerToFun(expression):
expression=transferToFunctionSyntax(str(expression))
xform = expr.transformString(expression)[1:-1]
xform=xform.replace('[','(')
expression=xform.replace(']',')')
当我致电以下时,它会返回"RuntimeError: maximum recursion depth exceeded while calling a Python object"
。
translatepowerToFun('(((2.00000000000000)*(((xn8(_n1, __v2(_n1))+((-__v2(_n1)*xn8(_n1, __VERIFIER_nondet_double2(_n1))**3 + xn8(_n1, __v2(_n1)))/(2.00000000000000)))-xn8(_n1, __v2(_n1)))))>((1.00000000000000e-6)*(((xn8(_n1, __v2(_n1))+((-__VERIFIER_nondet_double2(_n1)*xn8(_n1, __v2(_n1))**3 + xn8(_n1, __VERIFIER_nondet_double2(_n1)))/(2.00000000000000)))+(xn8(_n1, __v2(_n1))+((-__v2(_n1)*xn8(_n1, __v2(_n1))**3 + xn8(_n1, __v2(_n1)))/(2.00000000000000)))))))')
有人可以建议我该怎样做才能避免这种情况?