Kim Kuen Tang wrote:
Hi all,
considering the following expression created with proto:
"3 *var_+5=2;
.. The tree would be : = / \ + 2 / \ * 5 / \ 3 var_
Here the term "var_" is somewhat a placeholder.
I wonder wheter it is possible now to change the structure of the tree. That is i want to define a grammar that is able to produce another expression like this.
"var_=(2-5)/3;"
In tree:
= / \ var_ div / \ - 3 / \ 2 5
With this expression i can now define a context that return the final result of the right hand side. But is this possible in proto with the existing functions?
Yes.
This situation is somewhat different from the examples in the documentation. Here i dont want to replace the child node. The whole structure of the input tree should be transformed in another one.
A general equation solver will be challenging to write, but should be
possible. You'll need to familiarize yourself with Proto grammars and
transforms. Here is a toy example to get you started. Hope it helps.
#include <iostream>
#include
assign( plus( terminal(var_) , terminal(1) ) , terminal(2) ) assign( terminal(var_) , minus( terminal(2) , terminal(1) ) )
HTH, -- Eric Niebler BoostPro Computing http://www.boostpro.com