[Pyparsing] operatorPrecedence: Problems with Power and Unary Minus
Brought to you by:
ptmcg
From: Eike W. <eik...@gm...> - 2008-09-06 20:11:53
|
The parser generated by the function "operatorPrecedence" can not correctly parse the power operator in conjunction with the unary minus operator. The Python reference says: "The power operator binds more tightly than unary operators on its left; it binds less tightly than unary operators on its right." [http://docs.python.org/ref/power.html] I think this can't be done with operatorPrecedence. Is there a more elegant (shorter) way to get the correct syntax than what I did in this example program here? http://pastebin.com/f7eb6be8a Maybe a parser that gets this right could be added to operatorPrecedence. The syntax could look like this for example: expression = operatorPrecedence( u_expr, [OpPower(power='**', sign=oneOf('+ -')), (oneOf('* /'), 2, opAssoc.LEFT), (oneOf('+ -'), 2, opAssoc.LEFT), ]) I think an example like the little program I've posted here, should be included in the examples, so that newbies get mathematical expressions right. For me mathematical expressions are by far the most tricky part in writing the parser for my toy language. Kind regards, Eike |