Ondrej Certik wrote: > On Sun, May 3, 2009 at 9:30 AM, Robert Kern <[email protected]> wrote: >> On Sun, May 3, 2009 at 11:06, Robert Kern <[email protected]> wrote: >>> On Sun, May 3, 2009 at 10:59, Ondrej Certik <[email protected]> wrote: >>> >>>> Google app engine supports eval(), so we may just write a simple >>>> preparser based on the "re" module (also available on the app engine), >>>> that converts things like 1/2 to Integer(1)/Integer(2) and "x" to >>>> Symbol("x") and we should be fine. I don't know if eval() can handle >>>> large expressions (to fix #976), that would have to be tried. Usually >>>> a large expression is one big Add instance, so one solution that just >>>> occured to me is to split it into several smaller Add instances, >>>> evaluate each using eval() and add them together. >>> It's feasible to write and maintain a pyparsing parser for Python's >>> expression subgrammar. This also opens up the possibility for easily >>> extending the grammar to add other operators and such. >> Another alternative is Fredrik Lundh's hand-built parser here: >> >> http://effbot.org/zone/simple-top-down-parsing.htm >> >> It has the advantage of already being written. > > Thanks for both suggestions. I played with both and it should not be > that difficult as I feared. > > I still think that an easier option is to just write a preparse() > function, that takes a string, and returns a string: > >>>> preparse("3/4 + x") > 'Integer(3)/Integer(4) + Symbol("x")' > > And then we just pass it to eval(). That should be easy to debug and > maintain. Just like the lambdify() works. > > Then the question is, how to write the preparse() function, it seems > to me a simple regex based substitution should do the job, just like > in preparse_numeric_literals() in sage.misc.preparser.py. >
I had to do some preparsing for django-sympy, and I used regular expressions, so might be worth to take a look at http://git.sympy.org/?p=django-sympy.git;a=blob;f=dsympy/parser.py;h=7ce868f822a7ab3be73fd9214b103ccb84031d45;hb=HEAD > Ondrej > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sympy" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/sympy?hl=en -~----------~----~----~----~------~----~------~--~---
