Hello,
I have a simple math expression defining grammar and I generated visitor and parser in JavaScript.
I am able to assign values to variables and to then evaluate expressions (similar to that example in the ANTLR4 book).
Now... I am brainstorming about the following 2 things.
1) Expression simplification.
I have a ANTLR 4 parse tree for say A = 1 + x + x^3 + 2*x + 3. What algorithms are there to simplify this to
B = x^3 + 3*x + 4. I found a read somewhere that I need to traverse the parse tree of A and either modify it in place,
or build a new one by following certain rules. But OK... then two questions come up:
a) how do I know which sub-trees to group together (1 with 3) and (x with 2*x).
b) how do I build that new/resulting parse tree from zero just by a program.
What do I mean? I realized that currently the only way I know of building a parse tree
is through parsing a string-written expression (one like A). But what if I already have a parse tree (from A),
then how do I build the parse tree of B in a programmatic way? Are there other ways? Are there any references or examples?
2) Expression Differentiation.
I have the exact same questions here / a) and b) /.
Also, I know for a person to do 1) is easier than to do 2).
But I start to think that maybe computationally 1) is a harder problem than 2).
Not sure.
In general are there any good references or books or algorithms about this?
And in particular: how do I go about a) and b)?
Many thanks in advance.
Regards,
Peter