Pretty在Ruby中打印树数据结构

时间:2022-12-01 13:48:13

I am working on a building a compiler and within that I generate a tree that represents the source program that is passed in. I want to display this is a tree like fashion so I can display the structure of the program to anyone interested.

我正在构建一个编译器,在其中我生成一个树,代表传入的源程序。我想显示这是一个像时尚的树,所以我可以向任何感兴趣的人显示程序的结构。

Right now I just have the tree printing on a single line like this:

现在我只需要在一行上打印树,如下所示:

ProgramNode -> 'Math' BlockNode -> DeclarationNode -> ConstantDeclarationNode -> const ConstantListNode -> [m := 7, ConstantANode -> [n := StringLiteralNode -> ""TEST"" ]] ; 

What I would like is something like this:

我想要的是这样的:

   ProgramNode 
    /     \
'Math' BlockNode
           |
    DeclarationNode
           |
    ConstantDeclarationNode ------------------------------
        /      \                                         |
     const ConstantListNode                              |
             /  |  \      \                              |
             m  :=  7    ConstantANode                   |
                            /  |    \                    |
                           n   :=  StringLiteralNode     |
                                      /    |   \         |
                                      "   TEST  "        ;

I haven't really worked with trees in Ruby, how are they usually represented?

我没有真正使用Ruby中的树,它们通常如何表示?

Any help would be appreciated.

任何帮助,将不胜感激。

2 个解决方案

#1


3  

This kind of pretty printing requires quite a bit of math. Besides, it's unclear what should happen if the tree grows too wide for the console window. I don't know of any existing libraries that'll do this. I personally use awesome_print.

这种漂亮的印刷需要相当多的数学。此外,如果树对于控制台窗口变得太宽,还不清楚会发生什么。我不知道任何现有的库会这样做。我个人使用awesome_print。

tree = {'ConstantDeclarationNode' => ['const',
                                      'ConstantListNode' => ['m', ':=', '7']]}

require 'awesome_print'

ap tree
# >> {
# >>     "ConstantDeclarationNode" => [
# >>         [0] "const",
# >>         [1] {
# >>             "ConstantListNode" => [
# >>                 [0] "m",
# >>                 [1] ":=",
# >>                 [2] "7"
# >>             ]
# >>         }
# >>     ]
# >> }

It has tons of options, check it out!

它有很多选项,请查看!

#2


2  

You need to check out the Graph gem. It is amazing and remarkably simple to work with. You can choose the direction of your tree and the shape of the nodes, as well as colors and so much more. I first found out about it at Rubyconf last year and was blown away.

您需要查看Graph gem。这很棒,而且非常简单。您可以选择树的方向和节点的形状,以及颜色等等。去年我第一次在Rubyconf上发现了它并且被吹走了。

It is as simple as:

它很简单:

digraph do
  edge "Programnode", "Blocknode"
  edge "Programnode", "Math"
  edge "Blocknode", "DeclarationNode"
end

Obviously you would want to programmatically enter the edges :)

显然你想要以编程方式输入边缘:)

Here is a link to a pdf of the talk which will give more information on it:

这是一个谈话的pdf链接,它将提供更多信息:

There is also a video of the talk on Confreaks if you are interested.

如果您有兴趣,还有关于Confreaks的演讲视频。

Cheers, Sean

#1


3  

This kind of pretty printing requires quite a bit of math. Besides, it's unclear what should happen if the tree grows too wide for the console window. I don't know of any existing libraries that'll do this. I personally use awesome_print.

这种漂亮的印刷需要相当多的数学。此外,如果树对于控制台窗口变得太宽,还不清楚会发生什么。我不知道任何现有的库会这样做。我个人使用awesome_print。

tree = {'ConstantDeclarationNode' => ['const',
                                      'ConstantListNode' => ['m', ':=', '7']]}

require 'awesome_print'

ap tree
# >> {
# >>     "ConstantDeclarationNode" => [
# >>         [0] "const",
# >>         [1] {
# >>             "ConstantListNode" => [
# >>                 [0] "m",
# >>                 [1] ":=",
# >>                 [2] "7"
# >>             ]
# >>         }
# >>     ]
# >> }

It has tons of options, check it out!

它有很多选项,请查看!

#2


2  

You need to check out the Graph gem. It is amazing and remarkably simple to work with. You can choose the direction of your tree and the shape of the nodes, as well as colors and so much more. I first found out about it at Rubyconf last year and was blown away.

您需要查看Graph gem。这很棒,而且非常简单。您可以选择树的方向和节点的形状,以及颜色等等。去年我第一次在Rubyconf上发现了它并且被吹走了。

It is as simple as:

它很简单:

digraph do
  edge "Programnode", "Blocknode"
  edge "Programnode", "Math"
  edge "Blocknode", "DeclarationNode"
end

Obviously you would want to programmatically enter the edges :)

显然你想要以编程方式输入边缘:)

Here is a link to a pdf of the talk which will give more information on it:

这是一个谈话的pdf链接,它将提供更多信息:

There is also a video of the talk on Confreaks if you are interested.

如果您有兴趣,还有关于Confreaks的演讲视频。

Cheers, Sean