Re: Fastening the run-time interpretation of mathematical expressions

haberg@math.su.se (Hans Aberg)
11 Nov 2006 21:30:49 -0500

          From comp.compilers

Related articles
Fastening the run-time interpretation of mathematical expressions paolopantaleo@gmail.com (PAolo) (2006-11-11)
Re: Fastening the run-time interpretation of mathematical expressions haberg@math.su.se (2006-11-11)
Re: Fastening the run-time interpretation of mathematical expressions mailbox@dmitry-kazakov.de (Dmitry A. Kazakov) (2006-11-13)
Re: Fastening the run-time interpretation of mathematical expressions akk@privat.de (Andreas Kochenburger) (2006-11-13)
Re: Fastening the run-time interpretation of mathematical expressions martin@gkc.org.uk (Martin Ward) (2006-11-13)
Re: Fastening the run-time interpretation of mathematical expressions tommy.thorn@gmail.com (Tommy Thorn) (2006-11-13)
Re: Fastening the run-time interpretation of mathematical expressions 148f3wg02@sneakemail.com (Karsten Nyblad) (2006-11-15)
Re: Fastening the run-time interpretation of mathematical expressions martin@gkc.org.uk (Martin Ward) (2006-11-15)
[3 later articles]
| List of all articles for this month |

From: haberg@math.su.se (Hans Aberg)
Newsgroups: comp.compilers
Date: 11 Nov 2006 21:30:49 -0500
Organization: Mathematics
References: 06-11-052
Keywords: interpreter
Posted-Date: 11 Nov 2006 21:30:49 EST

"PAolo" <paolopantaleo@gmail.com> wrote:


> I am writing a piece of software that accept in input the definition
> of a mathematical function and a list of points in which evaluate the
> function and puts the result in output. The main feature of the progam
> is that once the function definition is read, the program evaluates
> the function very fast [Well this isn't done already...]. I was
> thinking to build a pair of stacks, one containig the operands
> (floats) and the other containig the operators (function pointers).


This is typically used when the user is allowed to define operators
with a large number of precedence levels. One can then integrate it in
an otherwise static grammar. I once write it, but I do not immediately
recall if I used one or two stacks. The trick is to the operator
precedence. Add an end marker # to the expression; when it appears,
the rest of the stacks are computed. Try a two value/operator stack
approach on "a + b * c #". First push 'a', '+', 'b'. When the '*'
appears, check the operator stack, to see that it is a '+' with lower
precedence on top; so then '*' and 'c' should be pushed. Then '#'
appears, saying that all stuff on the stacks should be computed, first
b * c, which is put back on the value stack, and then '+' is applied
to that.


When you know how to compute the expression, converting it to other
formats, such as the Australian notation
<http://en.wikipedia.org/wiki/Reverse_Polish_notation>, is easy.


--
    Hans Aberg


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.