HighTechTalks DotNet Forums  

creating algorithm for formula parsing

CSharp microsoft.public.dotnet.languages.csharp


Discuss creating algorithm for formula parsing in the CSharp forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
VM
 
Posts: n/a

Default creating algorithm for formula parsing - 07-02-2004 , 01: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
((sys_empHours*db_empSal)/db_totalHours)
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.

Thanks.



Reply With Quote
  #2  
Old   
 
Posts: n/a

Default creating algorithm for formula parsing - 07-02-2004 , 06: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
delimiter.
Quote:
-----Original Message-----
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
((sys_empHours*db_empSal)/db_totalHours)
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.

Thanks.


.


Reply With Quote
  #3  
Old   
Michael C
 
Posts: n/a

Default Re: creating algorithm for formula parsing - 07-02-2004 , 08: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
either, like
// ((sys_empHours*db_empSal)/db_totalHourssys_EmpHours), which would be
totally meaningless
// 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

Quote:
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
((sys_empHours*db_empSal)/db_totalHours)
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.

Thanks.





Reply With Quote
  #4  
Old   
RAyRAy
 
Posts: n/a

Default Re: creating algorithm for formula parsing - 07-03-2004 , 12: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.

RAyRAy

"Michael C" wrote:

Quote:
// 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
either, like
// ((sys_empHours*db_empSal)/db_totalHourssys_EmpHours), which would be
totally meaningless
// 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 in message
news:udSLBPGYEHA.1000 (AT) TK2MSFTNGP12 (DOT) phx.gbl...
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
((sys_empHours*db_empSal)/db_totalHours)
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.

Thanks.






Reply With Quote
  #5  
Old   
Fred Mellender
 
Posts: n/a

Default Re: creating algorithm for formula parsing - 07-03-2004 , 09: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

Quote:
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
delimiter.
-----Original Message-----
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
((sys_empHours*db_empSal)/db_totalHours)
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.

Thanks.


.




Reply With Quote
  #6  
Old   
AT
 
Posts: n/a

Default Re: creating algorithm for formula parsing - 07-05-2004 , 07: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:
http://www.codeproject.com/csharp/matheval.asp

HTH
Kieran

RAyRAy <RAyRAy (AT) discussions (DOT) microsoft.com> wrote

Quote:
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.

RAyRAy

"Michael C" wrote:

// 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
either, like
// ((sys_empHours*db_empSal)/db_totalHourssys_EmpHours), which would be
totally meaningless
// 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 in message
news:udSLBPGYEHA.1000 (AT) TK2MSFTNGP12 (DOT) phx.gbl...
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
((sys_empHours*db_empSal)/db_totalHours)
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.

Thanks.






Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.