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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:29:04+00:00 2026-06-03T21:29:04+00:00

I have an AST (abstract syntax tree) and now i want to test my

  • 0

I have an AST (abstract syntax tree) and now i want to test my compiler by giving it 2 or more numbers and expect an output with the result of math operations (like a calculator).

My question is, what is the best way to build the interpreter? The visiting of the AST nodes is recursive, so i don’t know how many encapsulated calculations exists until i get to the end of the tree. But since this is done iteration by iteration, how can i make all the operations in the end?

Thank you

  • 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-03T21:29:05+00:00Added an answer on June 3, 2026 at 9:29 pm

    Intepreters are pretty easy to code, once you have an AST:

     int interpret(tree t)
     { /* left to right, top down scan of tree */
       switch (t->nodetype) {
         case NodeTypeInt:
            return t->value;
         case NodeTypeVariable:
            return t->symbtable_entry->value
         case NodeTypeAdd:
            { int leftvalue= interpret(t->leftchild);
              int rightvalue= interpret(t->rightchild);
              return leftvalue+rightvalue;
            }
         case NodeTypeMultiply:
            { int leftvalue= interpret(t->leftchild);
              int rightvalue= interpret(t->rightchild);
              return leftvalue*rightvalue;
            }
         ...
         case NodeTypeStatementSequence: // assuming a right-leaning tree
            { interpret(t->leftchild);
              interpret(t->rightchild);
              return 0;
            }
         case NodeTypeAssignment:
            { int right_value=interpret(t->rightchild);
              assert: t->leftchild->Nodetype==NodeTypeVariable;
              t->leftchild->symbtable_entry->value=right_value;
              return right_value;
            }
         case NodeTypeCompareForEqual:
            { int leftvalue= interpret(t->leftchild);
              int rightvalue= interpret(t->rightchild);
              return leftvalue==rightvalue;
            }
         case NodeTypeIfThenElse
            { int condition=interpret(t->leftchild);
              if (condition) interpret(t->secondchild);
              else intepret(t->thirdchild);
              return 0;
         case NodeTypeWhile
            { int condition;
              while (condition=interpret(t->leftchild))
                    interpret(t->rightchild);
              return 0;
    
         ...
       }
     }
    

    What’s annoying to do is “goto”, because this changes the point of attention of the interpreter. To implement goto or function calls, one has to search the tree for the label or the function declaration, and continue execution there. [ One can speed this up by prescanning the tree and collecting all the label locations/function declarations in a lookup table. This is the first step towards building a compiler.] Even this isn’t quite enough; you have to adjust the recursion stack, which we hid in function calls so it isn’t easy to do. If one converts this code to a iterative loop with an explicitly managed recursion stack, it is a lot easier to fix up the stack.

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

Sidebar

Related Questions

I have an abstract syntax tree which I need to iterate. The AST is
Does anyone have a simple example using ast.NodeVisitor to walk the abstract syntax tree
Antlr users usually create a parser that generates the AST(Abstract syntax tree), and a
I have a tree parser that's doing semantic analysis on the AST generated by
I have a simple grammar: grammar sample; options { output = AST; } assignment
I have a general idea of what an AST is, but I want to
I'm using ANTLR 3 to create an AST. I want to have two AST
I have an sbt plugin, that needs to get compiler AST's from source files.
I'm currently developing an Abstracty Syntax Tree visitor for a lombok.ast tree that should
The last nodes of an AST tree must have the information of the traduction

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.