# Math expression parser?

**URL:** <https://forum.inductiveautomation.com/t/math-expression-parser/2488>\
**Category:** Archived Topics\
**Created:** [January 6, 2011, 4:30pm UTC](https://forum.inductiveautomation.com/t/math-expression-parser/2488 "2011-01-06T16:30:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pat](https://avatars.discourse-cdn.com/v4/letter/p/8491ac/32.png) [@Pat](https://forum.inductiveautomation.com/u/Pat)\
**Post date:** [January 6, 2011, 4:30pm UTC](https://forum.inductiveautomation.com/t/math-expression-parser/2488/1 "2011-01-06T16:30:56Z")

</div>

Is there a math expression parser available in either python or java that I can use in FPMI? I’m looking to allow the user to enter a string such as (4\*25)+(11/12) and use a parser to calculate the value.

---

<div class="post-metadata">

**Author:** ![Robert.McKenzie](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/robert.mckenzie/32/4134_2.png) [@Robert.McKenzie](https://forum.inductiveautomation.com/u/Robert.McKenzie)\
**Post date:** [January 7, 2011, 11:35pm UTC](https://forum.inductiveautomation.com/t/math-expression-parser/2488/2 "2011-01-07T23:35:59Z")

</div>

Unfortunately I don’t know of an easy way to accomplish this, but a quick Google search will show you that there are a few out there. Depending on the complexity you need, you might be able to write a small one yourself. A recursive function looking for ()\*±/ and numbers shouldn’t be too bad.

If you don’t like that, then you could let our standard python do it for you in an expression. This:`runScript("(0+4)*(13.0/2)")`will return 26, so you can make two DB Tags run this expression for you.

One called ‘Expression’ that’s a string with ‘(0+4)\*(13.0/2)’ as it’s value, and one that’s a float with ‘runScript({[.]Expression})’ as it’s value. Now just bind the expression tag to an Input Box on the screen and grab the result from the second tag.

😃

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [January 22, 2011, 11:31pm UTC](https://forum.inductiveautomation.com/t/math-expression-parser/2488/3 "2011-01-22T23:31:39Z")

</div>

Python is a dynamic scripting language. If you have a user-entered expression, Python will happily run it as python code using the eval function. Arithmetic expressions are valid Python code.

For example, put a Text Field, Button, and Label on a window. Put this code on the button:

[code]textField = event.source.parent.getComponent(“Text Field”)  
label = event.source.parent.getComponent(“Label”)

expression = textField.text  
result = eval(expression)  
label.text = str(result)[/code]

Ta-da!

[color=#FF4000]Obligatory Warning[/color]: You’d better trust the user typing into the text box, because if they’re malicious, they could run any Python code they want, which could be bad.

---

<div class="post-metadata">

**Author:** ![bestcode](https://avatars.discourse-cdn.com/v4/letter/b/ccd318/32.png) [@bestcode](https://forum.inductiveautomation.com/u/bestcode)\
**Post date:** [December 1, 2013, 3:40am UTC](https://forum.inductiveautomation.com/t/math-expression-parser/2488/4 "2013-12-01T03:40:27Z")

</div>

[mathparsers.com](http://mathparsers.com) has math expression parsers for various languages including java ([http://www.mathparsers.com/math-parser-for-java/](http://www.mathparsers.com/math-parser-for-java/)). [bestcode.com](http://bestcode.com) has PyMathParser ([http://www.bestcode.com/html/evaluate\_math\_expressions\_pyth.html](http://www.bestcode.com/html/evaluate_math_expressions_pyth.html)) that uses eval in a safe way by limiting the scope to a pre-defined (or user defined) set of math functions to prevent injection attacks that are otherwise possible with eval function.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [December 2, 2013, 3:38pm UTC](https://forum.inductiveautomation.com/t/math-expression-parser/2488/5 "2013-12-02T15:38:58Z")

</div>

Slightly spammy post aside (Resurrecting a two-year thread is a bit of a flag, but mitigated because the python code is free 😉 ), bad code could still get in.

I wrote an eval function to add to the app.math script some time ago, but never uploaded it. Lemme see if I can find it…

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [December 3, 2013, 5:36pm UTC](https://forum.inductiveautomation.com/t/math-expression-parser/2488/6 "2013-12-03T17:36:28Z")

</div>

Yeah … hey “bestcode” - please don’t raise the dead [threads] around here.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [December 3, 2013, 8:00pm UTC](https://forum.inductiveautomation.com/t/math-expression-parser/2488/7 "2013-12-03T20:00:21Z")

</div>

> [@Carl.Gould](#):
>
> please don't raise the dead [threads] around here.

Unless it's Easter, of course... ![:mrgreen:](https://us1.discourse-cdn.com/inductiveautomation/original/1X/d23659b203d2d24d394be983a52adb2fa6fe23d4.gif "Mr. Green") Sorry, couldn't resist...
