parenthesis

Joshua Marshall jmarshal at mathworks.com
Tue Nov 5 16:03:35 EST 2002


Scott Gilbert <xscottgjunk at yahoo.com> wrote:
> mis6 at pitt.edu (Michele Simionato) wrote:
>>
>> Suppose I want to parse the following expression:
>> 
>> >>> exp='(a*(b+c*(2-x))+d)+f(s1)'
>> 

> I'm not sure if you want to understand why simple regexps can't do
> what you want, or if you just want a solution to your problem.  Here
> is one path you could try:

>>>> from compiler import parse
>>>> parse(exp)

> Module(None, Stmt([Discard(Add((Add((Mul((Name('a'), Add((Name('b'),
> Mul((Name('c'), Sub((Const(2), Name('x'))))))))), Name('d'))),
> CallFunc(Name('f'), [Name('s1')], None, None))))]))

I wasn't aware of this one.  Much nicer than the output from the parser module.



More information about the Python-list mailing list