Re: [Pyparsing] operatorPrecedence: Problems with Power and Unary Minus
Brought to you by:
ptmcg
From: Paul M. <pt...@au...> - 2008-09-07 14:32:02
|
I worked on this problem a bit last night. The opAssoc.RIGHT parameter is there to address this problem, and in fact, this expression: a**b**c Does get correctly evaluated as (a**(b**c)). But things get muddled when unary signs are added, and I think this points up a bug in operatorPrecedence. As your pastebin code comments state: #Power and unary operations are intertwined to get correct operator precedence: # -a**-b == -(a ** (-b)) Which should be supported using this form of operatorPrecedence: u_expr = Word(nums) | Word(alphas) expression = operatorPrecedence( u_expr, [('**', 2, opAssoc.RIGHT), (oneOf('+ -'), 1, opAssoc.RIGHT), (oneOf('* /'), 2, opAssoc.LEFT), (oneOf('+ -'), 2, opAssoc.LEFT), ]) For the moment, this expression correctly parses "a**b**c" as "(a**(b**c))", but can only parse "-a**-b" if it is given as "-a**(-b)". Ideally, your request should be handled by the operatorPrecedence API as it exists today - I don't think any more specialized interface should be needed. But I'll see if I make any progress with finding this problem in the next day or so. -- Paul |