@staticmethod<EOL><INDENT>def __parser(expression):<DEDENT>
expr_stack = []<EOL>def push_first(strg, loc, toks):<EOL><INDENT>expr_stack.append(toks[<NUM_LIT:0>])<EOL><DEDENT>def push_u_minus(strg, loc, toks):<EOL><INDENT>if toks and toks[<NUM_LIT:0>] == '<STR_LIT:->':<EOL><INDENT>expr_stack.append('<STR_LIT>')<EOL><DEDENT><DEDENT>point = Literal('<STR_LIT:.>')<EOL>_e = CaselessLiteral('<STR_LIT:E>')<EOL>fnumber = Combine(Word('<STR_LIT>' + nums, nums) +<EOL>Optional(point + Optional(Word(nums))) +<EOL>Optional(_e + Word('<STR_LIT>' + nums, nums)))<EOL>ident = Word(alphas, alphas + nums + '<STR_LIT>')<EOL>plus = Literal("<STR_LIT:+>")<EOL>minus = Literal("<STR_LIT:->")<EOL>mult = Literal("<STR_LIT:*>")<EOL>div = Literal("<STR_LIT:/>")<EOL>lpar = Literal("<STR_LIT:(>").suppress()<EOL>rpar = Literal("<STR_LIT:)>").suppress()<EOL>addop = plus | minus<EOL>multop = mult | div<EOL>expop = Literal("<STR_LIT>")<EOL>_pi = CaselessLiteral("<STR_LIT>")<EOL>x = CaselessLiteral("<STR_LIT:X>")<EOL>expr = Forward()<EOL>atom = (Optional("<STR_LIT:->") + (x | _pi | _e | fnumber | ident + lpar + expr + rpar).setParseAction(push_first) |<EOL>(lpar + expr.suppress() + rpar)).setParseAction(push_u_minus)<EOL>factor = Forward()<EOL>factor << atom + ZeroOrMore((expop + factor).setParseAction(push_first))<EOL>term = factor + ZeroOrMore((multop + factor).setParseAction(push_first))<EOL>expr << term + ZeroOrMore((addop + term).setParseAction(push_first))<EOL>expr.parseString(expression)<EOL>return expr_stack<EOL>
adopted from Paul McGuire example. http://pyparsing.wikispaces.com/file/view/fourFn.py