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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:26:52+00:00 2026-06-11T18:26:52+00:00

I have an ANTLR3 AST which I need to traverse using a post-order, depth-first

  • 0

I have an ANTLR3 AST which I need to traverse using a post-order, depth-first traversal which I have implemented as roughly the following Clojure:

(defn walk-tree [^CommonTree node]
  (if (zero? (.getChildCount node))
    (read-string (.getText node))
    (execute-node
      (map-action node)
      (map walk-tree (.getChildren node)))))))

I would like to convert this to tail recursion using loop…recur, but I haven’t been able to figure out how to effectively use an explicit stack to do this since I need a post-order traversal.

  • 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-11T18:26:54+00:00Added an answer on June 11, 2026 at 6:26 pm

    Instead of producing a tail recursive solution which traverses the tree and visits each node, you could produce a lazy sequence of the depth first traversal using the tree-seq function and then get the text out of each object in the traversal. Lazy sequences never blow the stack because they store all the state required to produce the next item in the sequence in the heap. They are very often used instead of recursive solutions like this where loop and recur are more diffacult.

    I don’t know what your tree looks like though a typical answer would look something like this. You would need to play with the “Has Children” “list of children” functions

    (map #(.getText %) ;; Has Children?      List of Children    Input Tree
         (tree-seq #(> (.getChildCount #) 0) #(.getChildren %) my-antlr-ast))
    

    If tree-seq does not suit your needs there are other ways to produce a lazy sequence from a tree. Look at the zipper library next.

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

Sidebar

Related Questions

I have just started using antlr3 and am trying to serialize the AST output
I currently have a working, simple language implemented in Java using ANTLR. What I
I'm using ANTLR 3 to create an AST. I want to have two AST
Hello I have the following code to retrieve data from my db using nhibernate
I have an ANTLR grammar which creates an AST, and then I have written
I have a tree parser that's doing semantic analysis on the AST generated by
What is the purpose of using AST while building a compiler (with ANTLR). Is
I am doing an ANTLR specification. In my AST i need to do this
I have lexer/parser (Generated from an ANTLR grammar file) which (for performance reasons) I
i have the following hibernate mapping: <class name=Domain.Roomreservation, Core table=Reservationroom> <id name=ID unsaved-value=undefined> <generator

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.