One of the features the compiler for my programming language is supposed to have is the ability to draw AST-es, to make it easier to detect when syntax doesn't mean exactly what the programmer meant. That part of my compiler is runnable in a browser, you can see it here.
Thus far, I've come up with an algorithm that successfully draws diagrams like this one:
However, it fails to print good diagrams for expressions with "deeper" AST-es, such as even:

The diagram it draws for that one is a lot wider than what's needed to make it legible, so much so that it's illegible because of that.
So, which algorithm would you use to draw AST-es?

  • $\begingroup$ There are algorithms out there for tree drawing (a special case of graph drawing). Have you checked any? $\endgroup$ Apr 5, 2020 at 16:09
  • $\begingroup$ @YuvalFilmus Well, no, I haven't checked any actual tree drawing algorithm. There are obviously many of them that are suitable for different purposes. Which particular one would you recommend me for drawing AST-es? $\endgroup$ Apr 5, 2020 at 16:13
  • $\begingroup$ I'm not too familiar with the subject, but I wonder whether one of these algorithms is suitable for you. $\endgroup$ Apr 5, 2020 at 16:51
  • $\begingroup$ en.wikipedia.org/wiki/Graph_drawing. I would suggest you do some research, try standard methods, see if they meet your needs, then edit the question to show what progress you've made and why you rejected standard methods (if you have). We expect you to do a significant amount of research before asking here; see meta.stackoverflow.com/q/261592/781723. $\endgroup$
    – D.W.
    Apr 5, 2020 at 17:34


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.