Daniele Ligato
Daniele Ligato

Reputation: 3

How do I optimize an abstract syntax tree?

I'm trying to create a script in python that given an input string like a+a*b, reads it, parses it, and creates an abstract syntax tree.

At this point, the AST should have tokens and be something like (Var_A, PLUS, (Var_A, MUL, Var_B)).

I managed to do everything until here and now I should optimize the AST and get in output something that has as few variable as possible, so having an output like (b+1)*a

Does anyone know an algorithm or a code that will do this sort of optimization?

Ps. I can't use any external library, I have to implement this from zero

Upvotes: 0

Views: 683

Answers (0)

Related Questions