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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:14:35+00:00 2026-05-26T08:14:35+00:00

Let’s say I’m reading a line from a file: {Parent{{ChildA}{ChildB}}} More complex example: {Parent{{ChildA{ChildC}{ChildD}}{ChildB{ChildE}{ChildF}}}}

  • 0

Let’s say I’m reading a line from a file:

{Parent{{ChildA}{ChildB}}}

More complex example:

{Parent{{ChildA{ChildC}{ChildD}}{ChildB{ChildE}{ChildF}}}}

Which is the grammar used to construct a tree.

Any name inside {} brackets is a node, and if within that bracket there are other nodes (brackets), those nodes are children.

I’m able to parse the first specific example using a counter, but only to find the text names of the nodes. How can I parse this so that I can determine what nodes are children of one another? I can’t seem to wrap my mind around the code I would use. I have a feeling I would use recursion.

Any help or advice would be appreciated.

C++ is preferred.

Thank you very much.

  • 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-05-26T08:14:36+00:00Added an answer on May 26, 2026 at 8:14 am

    Spoiling the fun with an answer you can’t use anyway if it’s homework:

    A minimal implementation with Boost Spirit Qi:

    #include <boost/spirit/include/qi.hpp>
    namespace qi = boost::spirit::qi;
    
    typedef boost::make_recursive_variant<
        std::vector<boost::recursive_variant_>, 
        std::string>::type ast_t;
    
    void dump(const ast_t&);
    
    // adhoc parser rule:
    static const qi::rule<std::string::iterator, ast_t()> node = 
        '{' >> *node >> '}' | +~qi::char_("{}");
    
    int main()
    {
         std::string input = "{Parent{{ChildA{ChildC}{ChildD}}{ChildB{ChildE}{ChildF}}}}";
         std::string::iterator f(input.begin()), l(input.end());
    
         ast_t tree;
         if (qi::parse(f, l, node, tree))
             dump(tree);
         else
             std::cerr << "Unparsed: " << std::string(f, l) << std::endl;
    }
    

    The implementation of dump is regrettably almost the equivalent amount of code 🙂


    It will print:

    {
        Parent
        {
            {
                ChildA
                {
                    ChildC
                }
                {
                    ChildD
                }
            }
            {
                ChildB
                {
                    ChildE
                }
                {
                    ChildF
                }
            }
        }
    }
    


    Here is the definition of dump(const ast_t&):

    struct dump_visitor : boost::static_visitor<>
    {
        dump_visitor(int indent=0) : _indent(indent) {}
    
        void operator()(const std::string& s) const { print(s); }
    
        template <class V>
            void operator()(const V& vec) const
        {
            print("{");
            for(typename V::const_iterator it=vec.begin(); it!=vec.end(); it++)
                boost::apply_visitor(dump_visitor(_indent+1), *it);
            print("}");
        }
    
      private:
        template <typename T> void print(const T& v) const 
          { std::cout << std::string(_indent*4, ' ') << v << std::endl; }
        int _indent;
    };
    
    void dump(const ast_t& tree)
    {
        boost::apply_visitor(dump_visitor(), tree);
    }
    

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

Sidebar

Related Questions

Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>
Let's take a common example how a haskell function can be called from a
Let's say I have a javascript array with a bunch of elements (anywhere from
Let's say I wanted all nodes whose parent(s) matched some certain condition. Is there
Let's say for example, I have the following javascript function that returns a boolean:
Let's say for example i have URL containing the following percent encoded character :
Let's say I have to pick a number from 0-10. The number I pick
Let's say you create a wizard in an HTML form. One button goes back,
Let's say I'm building a data access layer for an application. Typically I have

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.