米琪卡哇伊
至于我,你应该定义-为更高**('-', 1, pyparsing.opAssoc.RIGHT),('**', 2, pyparsing.opAssoc.RIGHT),这应该可以解决您的问题。最小的工作代码import pyparsingvariable_names = pyparsing.Combine(pyparsing.Literal('$') + pyparsing.Word(pyparsing.alphanums + '_'))integer = pyparsing.Word(pyparsing.nums)double = pyparsing.Combine(pyparsing.Word(pyparsing.nums) + '.' + pyparsing.Word(pyparsing.nums))parser = pyparsing.operatorPrecedence( variable_names | double | integer, [ ('-', 1, pyparsing.opAssoc.RIGHT), ('**', 2, pyparsing.opAssoc.RIGHT), (pyparsing.oneOf('* / // %'), 2, pyparsing.opAssoc.LEFT), (pyparsing.oneOf('+ -'), 2, pyparsing.opAssoc.LEFT), (pyparsing.oneOf('> >= < <= == !='), 2, pyparsing.opAssoc.LEFT), ('not', 1, pyparsing.opAssoc.RIGHT), ('and', 2, pyparsing.opAssoc.LEFT), ('or', 2, pyparsing.opAssoc.LEFT) ] )examples = [ "5 * 10 ** -2", "5 * 10 * -2", "5 * 10 ** (-2)", "5 * -10 ** 2", "5 * (-10) ** 2", "5 and not 8", "5 and -8", "1 ** -2", "-1 ** 2",]longest = max(map(len, examples))for ex in examples: result = parser.parseString(ex) print(f'{ex:{longest}} <=> {result}')结果:5 * 10 ** -2 <=> [['5', '*', ['10', '**', ['-', '2']]]]5 * 10 * -2 <=> [['5', '*', '10', '*', ['-', '2']]]5 * 10 ** (-2) <=> [['5', '*', ['10', '**', ['-', '2']]]]5 * -10 ** 2 <=> [['5', '*', [['-', '10'], '**', '2']]]5 * (-10) ** 2 <=> [['5', '*', [['-', '10'], '**', '2']]]5 and not 8 <=> [['5', 'and', ['not', '8']]]5 and -8 <=> [['5', 'and', ['-', '8']]]1 ** -2 <=> [['1', '**', ['-', '2']]]-1 ** 2 <=> [[['-', '1'], '**', '2']]顺便说一句:为了比较:C 运算符优先级和Python - 运算符优先级编辑:我之前保留时可以得到-500( 5 * -10 ** 2)但我使用[[5, '*', ['-', [10, '**', 2]]]]**-integer = pyparsing.pyparsing_common.signed_integerimport pyparsingvariable_names = pyparsing.Combine(pyparsing.Literal('$') + pyparsing.Word(pyparsing.alphanums + '_'))#integer = pyparsing.Word(pyparsing.nums)integer = pyparsing.pyparsing_common.signed_integerdouble = pyparsing.Combine(pyparsing.Word(pyparsing.nums) + '.' + pyparsing.Word(pyparsing.nums))parser = pyparsing.operatorPrecedence( variable_names | double | integer, [ ('**', 2, pyparsing.opAssoc.RIGHT), ('-', 1, pyparsing.opAssoc.RIGHT), (pyparsing.oneOf('* / // %'), 2, pyparsing.opAssoc.LEFT), (pyparsing.oneOf('+ -'), 2, pyparsing.opAssoc.LEFT), (pyparsing.oneOf('> >= < <= == !='), 2, pyparsing.opAssoc.LEFT), ('not', 1, pyparsing.opAssoc.RIGHT), ('and', 2, pyparsing.opAssoc.LEFT), ('or', 2, pyparsing.opAssoc.LEFT) ] )examples = [ "5 * 10 ** -2", "5 * 10 * -2", "5 * 10 ** (-2)", "5 * -10 ** 2", "5 * (-10) ** 2", "5 and not 8", "5 and -8", "1 ** -2", "-1 ** 2",]longest = max(map(len, examples))for ex in examples: result = parser.parseString(ex) print(f'{ex:{longest}} <=> {result}')结果:5 * 10 ** -2 <=> [[5, '*', [10, '**', -2]]]5 * 10 * -2 <=> [[5, '*', 10, '*', ['-', 2]]]5 * 10 ** (-2) <=> [[5, '*', [10, '**', ['-', 2]]]]5 * -10 ** 2 <=> [[5, '*', ['-', [10, '**', 2]]]]5 * (-10) ** 2 <=> [[5, '*', [['-', 10], '**', 2]]]5 and not 8 <=> [[5, 'and', ['not', 8]]]5 and -8 <=> [[5, 'and', ['-', 8]]]1 ** -2 <=> [[1, '**', -2]]-1 ** 2 <=> [['-', [1, '**', 2]]]Doc forpyparsing_common与其他预定义的表达式