Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8101897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:06:02+00:00 2026-06-05T23:06:02+00:00

I am working on a building a compiler and within that I generate a

  • 0

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?

Any help would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T23:06:05+00:00Added an answer on June 5, 2026 at 11:06 pm

    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.

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on building a tree structure in MySQL and have been experimenting with
I'm building an open source 2D game engine called YoghurtGum. Right now I'm working
I'm working on building Cppcheck on AIX with the xlC compiler (see previous question
I am working on building a PHP based filter that will narrow down a
I'm working on building a MySQL database and I'm thinking that rather than encode
I got this error when I want to build project that is using Qt4iOS
I'm working on building a mobile friendly site of our companies main website. The
I'm working on building a database to manage project assignments, and the one part
I'm working on building the Boost platform for a currently unsupported toolchain. Obviously we'd
My organization is working on building RESTful webservices on JBoss appserver. The QA team

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.