4

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.

2
  • 1
    if your syntax is context free you can use xml Commented Mar 6, 2012 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 :) Commented Mar 6, 2012 at 19:33

2 Answers 2

3

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.

3
  • 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? Commented Mar 6, 2012 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. Commented Mar 6, 2012 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. Commented Mar 9, 2012 at 22:41
3

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.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.