creating algorithm for formula parsing - 07-02-2004 , 02:42 PM
I'm trying to find out what the best way would be to parse a formula so I
can then calculate it with its real values.
For example, a user may enter
and I would need to parse all of that so that I can get the tokens that need
to be replaced with data (sys_empHours, db_empSal, etc...) and then plug
them back in.
creating algorithm for formula parsing - 07-02-2004 , 07:47 PM
Well, barring any built in function, you will have to
worm through the string and check for validity on the
part since the last time you stopped when you hit a
Re: creating algorithm for formula parsing - 07-02-2004 , 09:02 PM
// It ain't pretty but it does exactly what you asked for...
string sys_empHours = "40";
string db_empSal = "35000.00";
string db_totalHours = "80";
string userEntered = "((sys_empHours*db_empSal)/db_totalHours)";
string tokenReplace = userEntered.Replace("sys_empHours", sys_empHours);
tokenReplace = tokenReplace.Replace("db_empSal", db_empSal);
tokenReplace = tokenReplace.Replace("db_totalHours", db_totalHours);
// Now, if you want to actually perform the calculations in the user-entered
string, you've got a
// whole new thing coming. And it doesn't account for user-entered garbage
// ((sys_empHours*db_empSal)/db_totalHourssys_EmpHours), which would be
// but .Replace() doesn't care about all that. Have fun.
// If you want to do a real parser that actually performs the math for you,
there are lots of
// examples around the Web in lots of languages. There are plenty in Pascal
and C (which should
// translate fairly readily to C#).
"VM" <vonchi_m (AT) yahoo (DOT) com> wrote
Re: creating algorithm for formula parsing - 07-03-2004 , 01:40 AM
Also you might want to look into RegularExpression class. I think it's called RegEx and that's pretty helpful. I just started taking a compiler course and this is the stuff we will be dealing with. To parse and analyze what the user entered we will be using lex and yacc, which I don't think there is an equivalent class in the CLR, but there are 3rd party tools out there.
Also if you don't want to get into compiler, you can just place the identifiers/variables as a node in a tree and the operators would be the parent of the nodes. Then you would do a in-order operation. You would need to make sure that you take into account operator presedence. Like multiply and divide would be higher than add and subtract.
I don't know if this helps, but it opens the doors to some other possiblities.
"Michael C" wrote:
Re: creating algorithm for formula parsing - 07-03-2004 , 10:49 AM
Take a look at http://www.engr.mun.ca/~theo/Misc/exp_parsing.htm , from
which you will learn how
to write a simple recursive descent parser that will solve this problem.
<anonymous (AT) discussions (DOT) microsoft.com> wrote
Re: creating algorithm for formula parsing - 07-05-2004 , 08:56 AM
You might also want to look into generating code dynamically and
compiling it (very fast and efficient and extremely flexible). Heres
an example that could be modified to do what you want:
RAyRAy <RAyRAy (AT) discussions (DOT) microsoft.com> wrote
|Thread Tools||Search this Thread|