# [Maxima] A simple way to parse/check equations?

Richard Fateman fateman at cs.berkeley.edu
Tue Oct 9 10:29:17 CDT 2007

```What you are asking for is a simple way to do semantic checking of an
expression, a component of almost any compiler.
This is taught in an introductory course on programming languages and
compilers.
The first step is to parse the expression into a tree, and then to traverse
the tree checking each sub-tree for semantic correctness (in your case,
checking for argument count and type.)

The first step is already done for you in Maxima. The internal form is
obvious in lisp.  It is almost as obvious if you use the inpart() function
in Maxima, which you can use to look at each part of a tree or subtree.

The second part is a recursive walk through the expression tree, which is

You do need some data that will tell you the correct calling structure so
that you know foo(x,y):=x+y  means that foo has 2 arguments, as well as the
built-in functions.

RJF

> -----Original Message-----
> From: maxima-bounces at math.utexas.edu
> [mailto:maxima-bounces at math.utexas.edu] On Behalf Of Juhana
> Antero Yrjölä
> Sent: Tuesday, October 09, 2007 7:59 AM
> To: maxima at math.utexas.edu
> Subject: [Maxima] A simple way to parse/check equations?
>
> Hi
> I have s problem and I was wondering that is there an easy
> solution to the following problem.
>
> I have some kind of Maxima equation with functions and
> possible variables.
> Now I need to determine that all function calls are correct
> and if not
> then display the incorrect function name with call parameters (call
> parameters are not mandatory but it would be nice).
>
> For example if we have the equation  sin(ex(x)+1)+2, where
> the ex() is an
> unknown incorrect function, then the right output would be ex(x). Note
> that since sin is correct and it has right number of parameters it
> should not display sin(ex(x)+1) . Also note that when testing
> the validity of these functions, all validity tests must be done with
> original parameter set, for example
> diff(x,x,1); is valid but
> diff(x,1) is not (this is gives an error, but could be any function
> really).
>
> So it is not enough just parse the function names in a list
> without call
> parameters. (The biggest problem with a text parse is that it
> is difficult
> to say which parentheses closes the function call, since
> there could be
> quite many parentheses)
>
> This function must be able to work with more complicated logic as
> sqrt(log(y)+tan(x))+cos(y+sin(ex(x)+1))
> and recursion must go all the way down.
>
>
> Final thoughts:
> Unknown()-function in Maxima is very useful but it is not
> enough. I need
> to form a tree(or something like that) and possible even replace some
> functions in order to evaluate these (how to replace
> functions? String
> parse?). For example in equation sin(ex(x)+1)+2, it is not
> enough to just
> simply parse the equation into
> sin(ex(x)+1)
> ex(x)
> 1
> 2.
>
> sin(ex(x)+1) is not valid according to unknown() because of
> ex()-function. So we need to make some kind of substitution that would
> transform sin(ex(x)+1) into sin(z+1) and then use unknown()-function.
> (z is some kind of symbol).
>
> Trust me this is not a joke. As a matter a fact I have
> implemented this
> in a clever way but unfortunately it did not meet all the above
> requirements so it was rejected. Any solution to this problem
> must comply
> with  all the conditions given above(well a small improvement
> is better
> than nothing). So therefore I am asking your help. Can anyone help me?
>
> I might be able to patch my original code to work if there exist a
> Maxima function that is able to say if system has some function. This
> function must work with strings. Strings are essential since
> they are the
> only objects that are easily extractable by function
> tokens(string,alphacharp), For example to check if system has
> a function
> cos(x) i have the string "cos".
> _______________________________________________
> Maxima mailing list
> Maxima at math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima
>

```