I wrote a simple compiler in C++ and want to visualize the Abstract Syntax Tree it produces. Currently, I dump the AST in a super long string similar to something below:
Program(decls=[ConstDecl(type=BasicTypeKind::Int, value=Num(n=1, loc=Location(1, 21)), name=positive, loc=Location(1, 10)), ConstDecl(type=BasicTypeKind::Int, value=Num(n=-1, loc=Location(2, 21)), name=negative, loc=Location(2, 10)), ConstDecl(type=BasicTypeKind::Int, value=Num(n=100, loc=Location(3, 26)), name=max_heap_size, loc=Location(3, 10)), ConstDecl(type=BasicTypeKind::Character, value=Char(c=99, loc=Location(4, 23)), name=...
As you can see, this dump isn't very human friendly in terms of visualization. One cannot naturally relate the notion a tree to such a long string. I tried the approach to pretty-print the AST and found astpretty, which is for Python. It aims a lot in debugging but what if I want an illustration of the AST? A graphical format surely fits better.
Actually I have the picture about what the output I am looking forward. Graphviz
does a great job on this field and various graphs the C++ document tool Doxygen
generates are very close to my purpose conceptually.
Putting these together, I want a way to turn an AST in memory as C++ objects into decent graphic output (static one is ok). Any good starting point?
Edit: as comments said, dumping my AST in format Graphviz
recognizes is a good starting point. I will try do it this way until new and more concrete problems arise. Thanks guys.