Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When writing a compiler, it is useful to include the ability to dump the abstract syntax tree in a human readable format, for debugging purposes. This output might also be useful for other tools like IDEs and code analyzers, so I might as well provide it as a documented feature.

Given that, what format would be the most useful? It doesn't make much difference to me as long as I can eyeball it, so I might as well provide e.g. whatever other tools would find easiest to read.

share|improve this question
if your syntax is context free you can use xml – ratchet freak Mar 6 '12 at 17:03
Y'know, I'm just going out on a limb to be different here--When I wrote a compiler, I used a lot of different things. A dump of the AST wasn't one of them. You will run into very challenging problems when writing a compiler, and by comparison, you should find your AST to relatively simple. Compared to other debugging you will run into, the AST just works :) – Stargazer712 Mar 6 '12 at 19:33
up vote 3 down vote accepted

There are lots of viewers for the DOT file format. It is fairly simple to output ( plain text ) and produces ( depending on the viewer ) adequate to very nicely formatted output.

I believe DOT is what ANTLRWorks uses for its AST visualization.

I don't know what "tools" you are referring to reading the output, but something hierarchical like Xml Orientend Gcc AST ANalyzer or some equivalent JSON output would probably work as a generic input format to some visualization tool you might create.

share|improve this answer
What would be the target audience for that? I had been assuming parties other than myself would be more interested in having the data for input to tools rather than viewing by eye on a graph display, is this not the case? – rwallace Mar 6 '12 at 17:33
"as I can eyeball it" can't get more eyeball friendly than plain text that can be viewed as nice orgainized graphics really easily. – Jarrod Roberson Mar 6 '12 at 17:43
I've ended up using XML. Works fine as an eyeball format for me, code to pretty-print it was already available, and if and when anyone wants to parse it, code for that is already available as well. – rwallace Mar 9 '12 at 22:41

I'd suggest taking a look at s-expressions.

The tooling side is a bit weak, but there seems to be a tool to convert s-expressions to DOT files.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.