Re: expression tree?

56 views
Skip to first unread message

Richard Morris

unread,
Jun 2, 2013, 3:45:36 PM6/2/13
to jep-...@googlegroups.com
Farida,

It is a tricky problem. It would be very difficult to solve it by changing the parsing stage. A better approach is to modify the parse tree after parsing. As a first step you can use the com.singularsys.jep.walkers.ExpressionConsolidator this can compress nested additions (or multiplications) into a single nary operator giving something like

                         +   
       *          *             *            *
   A    B   C    D     A    D     B    C

you could then walk the tree again and split the nary operators as desired.

Richard

On Sunday, June 2, 2013 11:56:24 AM UTC+1, Farida Sabry wrote:
I am new to JEP, I am trying to use it to get the expression tree to be used in another task rather than evaluation.
But I found that using:  
String expression = "A*B+C*D+A*D+B*C";
Node tree = jep.parse(expression); 
results in a tree constructed  in a way different than that I need.
The tree produced is having the structure:
                             +
                   +                 *
            +             *       B   C
       *          *    A    D
   A    B   C    D

while I want it to be:

                         +   
            +                         +
       *          *             *            *
   A    B   C    D     A    D     B    C

Is there a way in JEP to parse the expression in that way?
Or do I have to overload a parse function to do that?

Thanks for the good work done in JEP. 

Best regards,

Farida
Reply all
Reply to author
Forward
0 new messages