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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:59:00+00:00 2026-06-15T04:59:00+00:00

I have a chunk of Spirit code which correctly parses std::string input = RED.MAGIC(

  • 0

I have a chunk of Spirit code which correctly parses std::string input = "RED.MAGIC( 1, 2, 3 )[9].GREEN" into a simple std::vector<std::string>, by using std::vector<std::string> as the primary attribute.

I would like to replace the std::vector<std::string> into a struct my_rec which contains a std::vector<std::string>, but continue using the auto generator, if possible.

When I compile with -DUSE_MY_REC, I get a wall of impenetrable compile errors.

sample compile and run

/tmp$ g++ -g -std=c++11 sandbox.cpp -o sandbox && ./sandbox 
Finished.
MATCHED
/tmp$ g++ -DUSE_MY_REC -g -std=c++11 sandbox.cpp -o sandbox && ./sandbox
WALL OF COMPILE ERRORS --------------------------------------------

sandbox.cpp

// #define BOOST_SPIRIT_DEBUG
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/fusion/include/adapt_struct.hpp>

#include <string>
#include <iostream>

namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;

#ifdef USE_MY_REC
struct my_rec
{
    std::vector<std::string>    m_pieces;
};

BOOST_FUSION_ADAPT_STRUCT(
  my_rec,
  (std::vector<std::string>,  m_pieces)
)

typedef struct my_rec            MY_TYPE;
#else
typedef std::vector<std::string> MY_TYPE;
#endif

template <typename ITERATOR>
struct my_parser : 
    qi::grammar<ITERATOR, MY_TYPE(), ascii::space_type>
{
    my_parser() : 
        my_parser::base_type( start )
    {
        start %= ( color | fun_call ) % '.'
                ;

        color %=
                qi::string( "RED" )
                | qi::string( "GREEN" )
                | qi::string( "BLUE" )
                ;

        fun_call %= 
                qi::string( "MAGIC" )
                >> '('
                >> +qi::char_("0-9") % ','
                >> ')'
                >> '['
                >> +qi::char_("0-9")
                >> ']'
                ;
    }
    qi::rule<ITERATOR, MY_TYPE(),     ascii::space_type> start, fun_call;
    qi::rule<ITERATOR, std::string(), ascii::space_type> color;
};

int
main( int argc, char* argv[] )
{
    namespace qi    = boost::spirit::qi;
    namespace ascii = boost::spirit::ascii;

    MY_TYPE                  v;
    std::string              str = "RED.MAGIC( 1, 2, 3 )[9].GREEN";
    std::vector<std::string> exp = {{ "RED", "MAGIC", "1", "2", "3", "9", "GREEN" }};
    auto                     it  = str.begin(), end = str.end();
    my_parser<decltype(it)>  g;

    if( qi::phrase_parse( it, end, g, ascii::space, v ) && it==end ) 
    {
        std::cout << "Finished." << std::endl;
#ifndef USE_MY_REC
        if ( !std::equal( v.begin(), v.end(), exp.begin() ))
        {
            std::cout << "MISMATCH" << std::endl;
            for( const auto& x : v )
                std::cout << x << std::endl;
        } else {
            std::cout << "MATCHED" << std::endl;
        }
#endif
    } else
        std::cout << "Error." << std::endl;

    return 0;
}
  • 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-15T04:59:01+00:00Added an answer on June 15, 2026 at 4:59 am

    As shown in the question linked in the comments in order to assign to an adapted struct with a single member you need to use qi::eps in a sequence. Also you need to change the attribute of your intermediate fun_call rule to std::vector<std::string>().

    // #define BOOST_SPIRIT_DEBUG
    #include <boost/config/warning_disable.hpp>
    #include <boost/spirit/include/qi.hpp>
    #include <boost/spirit/include/phoenix_fusion.hpp>
    #include <boost/fusion/include/adapt_struct.hpp>
    
    #include <string>
    #include <iostream>
    
    namespace qi = boost::spirit::qi;
    namespace ascii = boost::spirit::ascii;
    
    
    struct my_rec
    {
        std::vector<std::string>    m_pieces;
    };
    
    BOOST_FUSION_ADAPT_STRUCT(
      my_rec,
      (std::vector<std::string>,  m_pieces)
    )
    
    typedef struct my_rec            MY_TYPE;
    
    
    template <typename ITERATOR>
    struct my_parser : 
        qi::grammar<ITERATOR, MY_TYPE(), ascii::space_type>
    {
        my_parser() : 
            my_parser::base_type( start )
        {
            start %= qi::eps >>( color | fun_call ) % '.' //add qi::eps to make the adaptation of the single member struct work
                    ;
    
            color %=
                    qi::string( "RED" )
                    | qi::string( "GREEN" )
                    | qi::string( "BLUE" )
                    ;
    
            fun_call %= 
                    qi::string( "MAGIC" )
                    >> '('
                    >> +qi::char_("0-9") % ','
                    >> ')'
                    >> '['
                    >> +qi::char_("0-9")
                    >> ']'
                    ;
        }
        qi::rule<ITERATOR, MY_TYPE(),     ascii::space_type> start;
        qi::rule<ITERATOR, std::vector<std::string>(), ascii::space_type> fun_call; //changed this rule's attribute to vector<string>
        qi::rule<ITERATOR, std::string(), ascii::space_type> color;
    };
    
    int
    main( int argc, char* argv[] )
    {
        namespace qi    = boost::spirit::qi;
        namespace ascii = boost::spirit::ascii;
    
        MY_TYPE                  v;
        std::string              str = "RED.MAGIC( 1, 2, 3 )[9].GREEN";
        std::vector<std::string> exp;
        exp.push_back("RED");
        exp.push_back("MAGIC");
        exp.push_back("1");
        exp.push_back("2");
        exp.push_back("3");
        exp.push_back("9");
        exp.push_back("GREEN");
        auto                     it  = str.begin(), end = str.end();
        my_parser<decltype(it)>  g;
    
        if( qi::phrase_parse( it, end, g, ascii::space, v ) && it==end ) 
        {
            std::cout << "Finished." << std::endl;
    
            if ( !std::equal( v.m_pieces.begin(), v.m_pieces.end(), exp.begin() ))
            {
                std::cout << "MISMATCH" << std::endl;
                for( const auto& x : v.m_pieces )
                    std::cout << x << std::endl;
            } else {
                std::cout << "MATCHED" << std::endl;
            }
    
        } else
            std::cout << "Error." << std::endl;
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this chunk of code which I found and implemented according to http://www.activexperts.com/activmonitor/windowsmanagement/scripts/networking/windowsfirewall/
I have this chunk of code which adds up all the players hurt on
I have a chunk of java code which hard codes a hibernate disjunction query
I have this chunk of code, which is displayed on a user's journal page.
I have this chunk of code: noObjs = 0 Dim oName As String Dim
I have this chunk of code which is placed in accelerometer: didAccelerate which changes
I have a chunk of code where sometimes I need to create a new
I have a chunk of code that defines div s and some of the
Ok so i have this chunk of code <p id=number_of_stations class=text left><?php $_GET['count'] !=
So i have a chunk of code like this: public List<DynamicBusinessObject> GetSearchResultList(Search search, List<CategoryAttribute>

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.