[Pyparsing] Stuck on recursive parsing
Brought to you by:
ptmcg
From: Gre7g L. <haf...@ya...> - 2008-03-06 22:02:58
|
Hey guys, I'm brand new to pyparsing and I'm already stuck. My boss wants me to make our product "scriptable", so I'm trying to write some Python code to compile an expression entered by the user into something the hardware can execute. Basic op-codes such as +, -, *, and / were easy enough to compile with operatorPrecedence, but I got in trouble when I tried to make my code handle pre-defined function calls. I wrote this using Forward and <<, but pyparsing isn't going for it. Here's a minimal fragment of code illustrating what I was trying to do: http://pastie.textmate.org/162526 As you can see, it's trying to compile the expression "2-5*abs(-6)+3". I was hoping it would return something like: [[2, '-', [5, '*', ['abs' + '(' + ['-' + 6] + ')']] '+' 3]] but instead I got: [[2, '-', [5, '*', 'abs']]] It looks kind of like operatorPrecedence interpretted 'abs' as a literal and then got confused when it hit the parenthesis, and gave up. Can anyone suggest a way to work around this? Many thanks and my apologies if this has been discussed at length. If so, I'd love a link to a pertinent post. Gre7g __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com |