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 7911561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:17:35+00:00 2026-06-03T13:17:35+00:00

I read the article over at http://parsingintro.sourceforge.net/ and decided to try to rewrite it

  • 0

I read the article over at http://parsingintro.sourceforge.net/ and decided to try to rewrite it as an exercise in Ruby. Two reasons made me do this, I wanted to learn more about how to code Ruby (background in Java, PHP, C and some Python) and I wanted to learn more about parsers / compilers.

I have all the code posted at https://github.com/parse/boatcaptain. The AST tree is being generated, unfortunatly the author of the article doesn’t get into concepts such as code generation and optimizations.

Can anyone help me by pointing me in the right direction on how to achieve this AST tree into “code”? This is the AST tree that is generated

I wrote a calculator in Java a few years ago, it uses a lot of similar terminology and techniques as I used in this parser. But in the calculator I had methods for eval()-ing my “classes” and therefore getting output, should I aim for doing something similar here? Source for calculator: https://github.com/parse/Uppsala-University-Courses/blob/master/ImpOOP-Calculator/src/Calculator.java

I would love feedback on my way of writing Ruby as well, I believe I still write Ruby like I would write Python, missing some nice advantages of Ruby.

  • 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-03T13:17:38+00:00Added an answer on June 3, 2026 at 1:17 pm

    Code Generation in its most basic form is simply traversing your intermediate form – the AST – and emitting corresponding instructions in your target language.

    Firstly you’ll need to choose a target language. What platform do you want your input file to run on? The main options open to you are:

    • A source-to-source translator
    • A compiler to native code
    • A compiler to bytecode (to be run just-in-time on a VM)

    The choice of target language can determine the amount of work you’ll have to put in to map between languages. Mapping object-oriented classes down to ASM could/would be tricky, for example. Mapping inherently procedural code to stack-based code could also prove a challenge.

    Whichever language you choose, the problem will no doubt boil down to the following procedure: visit the nodes of your tree, and depending on their type, emit the corresponding instruction.

    Say you come across the following node in your AST (as in the one you linked to):

            =
    delta       /
          alpha   beta
    

    Seeing as it’s an ‘assignment’ node, the Code Generator then knows it has to evaluate the RHS of the tree before sticking that value into the LHS; ‘delta’. So we follow the RHS node down, and see it is a division operation. We then know we have to evaluate both the LHS and RHS of this node, before dividing them, and sticking the result in ‘delta’.

    So now we move down the LHS, see it’s a variable, and we emit a ‘load’ instruction. We go back up and then down the RHS, and likewise emit a ‘load’ for ‘beta’. We then walk back up the tree (taking both alpha and beta with us), emit the divide instruction on both the operands, store that result, pass it up the tree to the assignment emitter, and let that store it in ‘delta’.

    So the resulting code for this snippet might be:

    load alpha
    load beta
    tmp = div alpha beta
    store delta tmp
    

    As for pre-existing Ruby Code Generator libraries, I’m not aware of any, sorry. I hope this answer wasn’t too general or simplistic for you.

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

Sidebar

Related Questions

I read the article below and would like to try scribble in Ruby but
I've read Phil's article ( http://haacked.com/archive/2010/04/15/sending-json-to-an-asp-net-mvc-action-method-argument.aspx ) and still can't figure out how I
I just read two articles over this topic which provide infomration inconsistent, so I
For a long time I had the following bookmark in Firefox: http://www.codeguru.com/csharp/.net/net_general/keyboard/article.php/c4639 I know
I have read an article about various shuffle algorithms over at Coding Horror .
I've just read this article over at Particle Tree Is this accurate: If security
I've read over this article about placeholders. Previously, i was including a bunch of
How can I add information about read article on my site to facebook RECENT
I read an article where it was recommended that we should ONLY use classes
I read and article talking about moving beyond 960px width for websites. This width

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.