DevPinoy.org
A Filipino Developers Community

>>> First two to make 3 wins! <<<

formula parser for .NET
my current requirement is to support a formula parser for our application.  although it supports just a subset of the common MS Excel functions, what's trivial for me is the support for grouping symbols.  we had a similar requirement back in college in parsing mathematical expressions but we ended up not supporting it.  some groups, especially the programming geeky guys like wyu had them and now is the time that i write my own.  

i'm looking for algorithms for this and i've found some like converting infix to postfix, bison parser, etc.  there are also available libraries (for C++, java and even in .NET) for this but i think i will be coding my own (or maybe not) so i finally had an experience with this parsing routine.  automated unit tests will be beneficial for requirements like this.

Posted 08-29-2006 1:24 PM by jokiz
Filed under: , ,

Comments

cruizer wrote re: formula parser for .NET
on 08-28-2006 9:56 PM
go go go! :) recursion siguro OK dyan...
smash wrote re: formula parser for .NET
on 08-28-2006 10:10 PM
cruizer is right. a recursive descent interpreter would be much simpler to code by hand.
jokiz wrote re: formula parser for .NET
on 08-28-2006 10:19 PM

thanks guys, looks like the c++ sample (boost) that i provided uses what you guys are suggesting.

cruizer wrote re: formula parser for .NET
on 08-28-2006 10:32 PM
but don't forget to develop tests first :P
Jop wrote re: formula parser for .NET
on 08-28-2006 10:51 PM
If you are pressed for time, how about converting the formula into a C# method and compiling it dynamically? ..or maybe using Boo (http://boo.codehaus.org/) to eval the formula for you. But if you really need to make your own (you should!) try both the infix to postfix and the recursive descent parser (with and without parser generators). Infix to postfix is specific to mathematical formulae and is very simple. Recursive descent parsers are more generic. Bison and other parser generators use this type of algo: you just supply the BNF and the code that you want executed for each node and it will generate a parser for you. /jop
Simon Mourier wrote re: formula parser for .NET
on 09-06-2006 4:01 PM
You could reuse JScript or VBScript quite easily, although it sounds strange :-) If you want to do it, you just have to interop with the ScriptControl COM object (it has an Evaluate method). The advantages: 1) it's in process 2) you don't need to compile, 3) it's quite fast (if you cache the parsed result). See here http://www.thescarms.com/VBasic/Scripting.asp, http://support.microsoft.com/kb/q185697/
jokiz wrote re: formula parser for .NET
on 09-08-2006 12:40 AM

thanks simon but i would love to write something for myself, tdd way.


Copyright DevPinoy 2005-2008