Basically, you cannot do interesting optimizations at the AST level, because you need information on how data is transferred from one part of the program to another. Although the data flow is implicit in the meaning of the AST, it is difficult to determine it by checking only the AST, therefore, people building compilers and optimizers create other representations of programs (including symbol tables, flowchart diagrams, achievement definitions, data flow and SSA forms, etc. .).
Having a parser for a language is an easy part of parsing / manipulating that language. You need everything you need to do to do a good job.
If you have all of these other ideas, you might consider optimizing at the AST level. Most compiler developers are not bothered; they are converted to a data stream representation and simply optimize it. But if you want to reproduce the source code with the changes, you need an AST. You will also need a cute printer that allows you to restore the source code. If you go this far, you will get the original source of the program conversion system.
The DMS Software Reengineering Toolkit is a system that converts ASTs using all of these other views to provide the analysis needed for the transformations.
source share