Discussion:
[Pyparsing] Scientific Calculator - how to handle multiparameter functions
Andrew Nelson
2015-12-17 04:52:03 UTC
Permalink
Dear pyparsing users,
I'm attempting to make a fully functional scientific calculator by
expanding on some example code I found on the pyparsing wiki. The code
I have so far is at:

http://pastebin.com/aAPri29k
6.6, 'H':7.7, 'I':8.8, 'J':9.9, "abc": 20}
arith = Arith( vars_ )
arith.eval("1+2*sin(B)*2")
4.564829440245742

So far so good. However, I would now like to use functions which have
more that one parameter, e.g. math.pow or math.atan2.
I've been trying all day to add this kind of functionality, but
failing miserably. I've mainly been experimenting with delimitedList:

listop = delimitedList(operand)

and adding this listop to the infixNotation call.

Can anyone inform me if there is a simple solution? I'm a total novice
at parsing.

regards,
Andrew.

_____________________________________
Dr. Andrew Nelson


_____________________________________

------------------------------------------------------------------------------
Paul McGuire
2015-12-17 10:17:28 UTC
Permalink
Hm, interesting approach, to make the function an operator.

For your list of args, the operator is not a delimited list, it is just the
',' symbol itself. Try adding this to your infixNotation call, before fnop:

(',', 2, opAssoc.LEFT, lambda t: tuple(t[::2]) if len(t)>1 else
t[0]),

Totally untested.

Good luck!
-- Paul


-----Original Message-----
From: Andrew Nelson [mailto:***@gmail.com]
Sent: Wednesday, December 16, 2015 10:52 PM
To: pyparsing-***@lists.sourceforge.net
Subject: [Pyparsing] Scientific Calculator - how to handle multiparameter
functions

Dear pyparsing users,
I'm attempting to make a fully functional scientific calculator by expanding
on some example code I found on the pyparsing wiki. The code I have so far
is at:

http://pastebin.com/aAPri29k
6.6, 'H':7.7, 'I':8.8, 'J':9.9, "abc": 20}
arith = Arith( vars_ )
arith.eval("1+2*sin(B)*2")
4.564829440245742

So far so good. However, I would now like to use functions which have more
that one parameter, e.g. math.pow or math.atan2.
I've been trying all day to add this kind of functionality, but failing
miserably. I've mainly been experimenting with delimitedList:

listop = delimitedList(operand)

and adding this listop to the infixNotation call.

Can anyone inform me if there is a simple solution? I'm a total novice at
parsing.

regards,
Andrew.

_____________________________________
Dr. Andrew Nelson


_____________________________________

----------------------------------------------------------------------------
--
_______________________________________________
Pyparsing-users mailing list
Pyparsing-***@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/pyparsing-users


---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus


------------------------------------------------------------------------------
Loading...