# [Tutor] Re: Algebraic symbol manipulation program idea

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Sun, 19 Aug 2001 00:14:15 -0700 (PDT)

```On Fri, 10 Aug 2001, Christopher Smith wrote:

> When given 2*3+2^2/3 Pythonica returns:
>
> 	in FullForm: Divide[Plus[Times[2,3],Power[2,2]],3]
> 	evaluates to: 3.33333333333
>
> but it should probably be something like:
>
> 	Plus[Times[2,3],Divide[Power[2,2],3]]
>
> and evaluate to 7.33333333333

[I'm not quite sure if my message is appropriate on tutor, but perhaps
someone is interested in recursive descent parsing.  Recursive descent
parsing is a technique that's used to try converting arithmetic
expressions into a form that's supposedly easy to work with.  *grin*]

Pythonica seems pretty interesting!  I'm taking a closer look at this now.
Does anyone have a formal context free grammar for Pythonica or
Mathematica?  I'm working on a Pythonica recursive descent parser now to
fix this bug.  Here's the link to what I have so far:

http://hkn.eecs.berkeley.edu/~dyoo/python/pythonica/

As a warning, this is ROUGH code; it doesn't even connect to Pythonica
yet.  For now, PythonicaParser.py can only build the parse tree for simple
arithmetic expressions.  Here's a sample run:

###
dyoo@einfall:~/pythonica\$ python PythonicaParser.py
>>> 3 * 4 + 5 * (foo + bar)
['Expression',
['Term',
['Factor', 'NUMBER', '3'],
['Term1', '*', ['Factor', 'NUMBER', '4'], None]],
['Expression1',
'+',
['Term',
['Factor', 'NUMBER', '5'],
['Term1',
'*',
['Factor',
'GROUP',
['Expression',
['Term', ['Factor', 'IDENTIFIER', 'foo'], None],
['Expression1',
'+',
['Term', ['Factor', 'IDENTIFIER', 'bar'], None],
None]]],
None]],
None]]
None
###

I'll see if I can coax it to handle more interesting things like
exponentiation tonight.  *grin*

```