Page 1 of 1
Evaluating a formula entered in a text box.
Posted: Mon Jul 14, 2008 12:34 am
by sithik_frns
Hi buddies,
I want to evaluate a formula (with variable names and limited operators eg. (a+b)-c )entered in a textbox using JavaScript or PHP. Can anyone here assist me?
Thanks in Advance
-: Sithik :-
Re: Evaluating a formula entered in a text box.
Posted: Mon Jul 14, 2008 1:08 am
by prometheuzz
sithik_frns wrote:Hi buddies,
I want to evaluate a formula (with variable names and limited operators eg. (a+b)-c )entered in a textbox using JavaScript or PHP. Can anyone here assist me?
Thanks in Advance
-: Sithik :-
You'll have to ask a more specific question than that.
Note that you have now posted in the regex forum. People often make the mistake of trying to write a mathematical expression parser with regex: this is really not the way to go. Regex is not the right tool.
So, I highly recommend you post your question in a more general part of this forum and ask a more specific question.
Best of luck.
Re: Evaluating a formula entered in a text box.
Posted: Mon Jul 14, 2008 1:41 am
by VladSun
Re: Evaluating a formula entered in a text box.
Posted: Mon Jul 14, 2008 4:18 pm
by Ollie Saunders
People often make the mistake of trying to write a mathematical expression parser with regex: this is really not the way to go. Regex is not the right tool.
Regex can definitely be useful but it's but it's only a tiny part of the solution.
sithik_frns, this is actually a pretty common thing so try searching for some existing code/library and articles. You'll want to search for "expression evaluation"
If you want to roll you own solution for fun or other reasons start with some kind of language definition - what is it going to be capable of? and a bunch of examples - this code should give this answer. Then write down the process your brain goes through in order to compute those expressions - that is what you'll have to describe to the computer.
Re: Evaluating a formula entered in a text box.
Posted: Tue Jul 15, 2008 12:23 am
by prometheuzz
Ollie Saunders wrote:People often make the mistake of trying to write a mathematical expression parser with regex: this is really not the way to go. Regex is not the right tool.
Regex can definitely be useful but it's but it's only a tiny part of the solution.
...
True: as a part of the lexer, regex can be useful but, as you already mentioned, that is just a small part.