tool for ast graph visualization

394 views
Skip to first unread message

alex-coder

unread,
Jun 13, 2023, 5:14:37 PM6/13/23
to golang-nuts
Hi All !

Could you please advice me a tool to visualize an ast graph.

Thank you.

Vraj Reshamdalal

unread,
Jun 14, 2023, 5:01:10 AM6/14/23
to golang-nuts
Hi alex-coder!
You can try using Graphviz.
Graphviz (https://graphviz.org/) is a powerful open-source graph visualization software. It provides a collection of tools for visualizing and manipulating graph structures (such as AST graphs).

To generate and visualize a graph using Graphviz, you can follow these steps:
  1. Create a DOT output file (e.g., graph.dot).
  2. Write the graph structure in the DOT language. For example, you can use the following code:
    digraph G {
        Node1 [label="C"];
        Node2 [label="D"];
        Node3 [label="d"];
        Node4 [label="f"];
        Node1 -> Node2;
        Node2 -> Node3;
        Node3 -> Node4;
    }

  3. Save the file as graph.dot
  4. Open your command-line interface and navigate to the directory where graph.dot is located.
  5. Use the dot command from Graphviz to generate an image file. Run the following command: ` dot -Tpng graph.dot -o graph.png `
    This command specifies that the input file is graph.dot, the output format is PNG (-Tpng flag), and the output file name is graph.png (-o graph.png flag).
  6. graph.png will be generated in the same directory. This file will contain the visual representation of the graph based on the DOT description.

Thanks,
Vraj

Brian Candler

unread,
Jun 14, 2023, 5:24:15 AM6/14/23
to golang-nuts
Aside: draw.io also has functionality like graphviz for the web 2.0 generation:

ben...@gmail.com

unread,
Jun 14, 2023, 6:45:20 PM6/14/23
to golang-nuts
Hi Alex, could you please give a bit more context about what language or kind of AST you're referring to, and what you mean by visualize? As Vraj mentioned, Graphviz is good for the general case. If you just want to "visualize" Go AST in a text format, you can use go/ast.Print: https://pkg.go.dev/go/a...@go1.20.5#Print

alex-coder

unread,
Jun 15, 2023, 12:01:54 AM6/15/23
to golang-nuts
Xa, context is the king on a field :-)

You gave a good sample, and I would like to get ast for GO code in a json format for further processing.


четверг, 15 июня 2023 г. в 01:45:20 UTC+3, ben...@gmail.com:

Vraj Reshamdalal

unread,
Jun 15, 2023, 6:28:52 AM6/15/23
to golang-nuts
Hi alex-coder,
To get ast for GO code in a json format you can try a web tool: https://astexplorer.net/
or
https://github.com/asty-org/asty

Thanks,
Vraj

alex-coder

unread,
Jun 15, 2023, 3:53:25 PM6/15/23
to golang-nuts
Thank you so much !

четверг, 15 июня 2023 г. в 13:28:52 UTC+3, Vraj Reshamdalal:
Reply all
Reply to author
Forward
0 new messages