Re: Parsing and storing formulas

From: Stefan Simek (simek.blah_at_kascomp.blah.sk)
Date: 10/28/04


Date: Thu, 28 Oct 2004 17:17:25 +0200

Hi,

"Stefan Rosi" <invalid@invalid.com> wrote in message
news:uc0TP5PvEHA.1300@TK2MSFTNGP14.phx.gbl...
>I was wondering how I can parse a mathematical formula in a storable way.
> For example I have two parameters like X and Y. And the user has to
> specify
> a formula to calculate the result. Like 2(Y+2X). Then this formula has
> to
> be stored for in the database. Each time the user visits a specific page,
> the calculations have to be done according to his own formula.
>
> Till now I have two ideas, the first one is to store the formula in just a
> string and make the variable names identical to their representation
> columns
> in the database. So I can just execute the query against the proper table
> and SQL Server will parse the string and return the result. Anyway this
> method has two drawbacks, first I can't use stored procedure anymore, and
> second the mathematical operations can be very complex, which will be a
> problem because sql server is not a mathematic program.

Are you sure this will be a problem? Have you measured? It would probably be
the easiest way to do it...

>
>
>
> The second idea is to represent the formula as a tree. Each inner node
> stands for an operation like multiplication, addition . etc. and the
> leaves
> are either variables or constants: to clarify this idea I will attach a
> diagram with this post. When I want to make the calculations I have just
> to
> cal a function on the root, and it will call the same function on it
> children and so on recursively. The tree will be serialized and saved as
> xml
> in the database.

As for creating a syntax tree, it's essentially what each
compiler/interpreter/expression evaluator does. There are numerous tools to
generate the scanners and parsers required to generate such trees, like
ANTLR (http://www.antlr.org), or my personal favorite - the ages old
flex/bison combination. Once you get used to these tool, it's pretty easy to
write a simple expression evaluator, and only a bit more difficult to write
a simple compiler.

Another way to do it - write a wrapper source for your expression and then
compile it using an implementation of System.CodeDom.Compiler.ICodeCompiler
(any .NET language). I expect you will be calling the function quite often,
so parsing, or even executing the tree for each loop might provide quite
costly. So precompiling the expression (if it's not gonna change a lot)
might be the best way to go.

>
> Anyway this tree will be hard to implement.

Not at all, see above... ;)

>
> So I just wanted to ask if somebody knows a better way to do this task. I
> am
> sure I am not the first person faced with this problem.
>
> How can I parse and store an entered formula? Is there any ready component
> to do it?
>
>
>
> Any answer will be appreciated.
>
> Stefan
>
>
>

HTH,
Stefan



Relevant Pages

  • Re: Parsing and storing formulas
    ... you could easily store different views/stored ... >I was wondering how I can parse a mathematical formula in a storable way. ... > be stored for in the database. ... > The second idea is to represent the formula as a tree. ...
    (microsoft.public.dotnet.languages.csharp)
  • Parsing and storing formulas
    ... I was wondering how I can parse a mathematical formula in a storable way. ... be stored for in the database. ... Anyway this tree will be hard to implement. ...
    (microsoft.public.dotnet.languages.csharp)
  • parsing a tree like structure
    ... I'm wondering if there is a nice way to parse this data, ... so I can pic out the tree like structure and replicate it in some way ... so that I can put it in an array or into a database ?? ...
    (perl.beginners)
  • Re: JTree & database: ID column ?
    ... > from database and for it I use ID1 column which is unique ... > and every number in a code represents the place in a parent node. ... And if you can construct a tree ... reverse-index is NOT an auto-inserted id value, although it has to be unique ...
    (comp.lang.java.gui)
  • Re: Managing Genealogy Relationships in FMP 8.5
    ... the database (the file has tables for managing photos, documents, ... family tree, he's variously a brother, husband, grandfather, son, ... but a complete map is hella more efficient. ... Replace a Sort field to 1. ...
    (comp.databases.filemaker)