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

The Archive Base Latest Questions

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

I am trying to parse and capture this string {ABC,HIJ}:{10,15,20} into struct indicator_rec .

  • 0

I am trying to parse and capture this string "{ABC,HIJ}:{10,15,20}" into struct indicator_rec.

The parse is successful, however, only the first set {ABC,HIJ} is captured into the std::vector<std::string> field.

The second part, {10,15,20} is parsed correctly, but fails to make it into the second field std::vector<unsigned>.

What am I doing wrong?

Output:

<equation>
  <try>{ABC,HIJ}:{10,15,20}</try>
  <indicator>
    <try>{ABC,HIJ}:{10,15,20}</try>
    <fail/>
  </indicator>
  <indicators>
    <try>{ABC,HIJ}:{10,15,20}</try>
    <indicator>
      <try>ABC,HIJ}:{10,15,20}</try>
      <success>,HIJ}:{10,15,20}</success>
      <attributes>[[A, B, C]]</attributes>
    </indicator>
    <indicator>
      <try>HIJ}:{10,15,20}</try>
      <success>}:{10,15,20}</success>
      <attributes>[[H, I, J]]</attributes>
    </indicator>
    <success>:{10,15,20}</success>
    <attributes>[[[A, B, C], [H, I, J]]]</attributes>
  </indicators>
  <parameters>
    <try>{10,15,20}</try>
    <parameter>
      <try>10,15,20}</try>
      <success>,15,20}</success>
      <attributes>[]</attributes>
    </parameter>
    <parameter>
      <try>15,20}</try>
      <success>,20}</success>
      <attributes>[]</attributes>
    </parameter>
    <parameter>
      <try>20}</try>
      <success>}</success>
      <attributes>[]</attributes>
    </parameter>
    <success></success>
    <attributes>[[]]</attributes>
  </parameters>
  <success></success>
  <attributes>[[[[A, B, C], [H, I, J]], []]]</attributes>
</equation>

Code:

#define BOOST_SPIRIT_DEBUG
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
#include <boost/spirit/include/classic_symbols.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>

#include <boost/algorithm/string.hpp>
#include <boost/regex.hpp>   // std::regex not fully implemented in stdc++ yet

#include <string>
#include <map>
#include <utility>
#include <functional>

// -----------------------------------------------------------------------------
namespace client
{
    namespace qi      = boost::spirit::qi;
    namespace ascii   = boost::spirit::ascii;
  namespace spirit  = boost::spirit;
    namespace phoenix = boost::phoenix;

    // ---------------------------------------------------------------------------
    struct indicator_rec
    {
    public:
        indicator_rec() { }

        std::vector<std::string>  m_indicators;
        std::vector<unsigned>     m_parameters;
    };
}

BOOST_FUSION_ADAPT_STRUCT(
    client::indicator_rec,
    (std::vector<std::string>,  m_indicators)
    (std::vector<unsigned>,     m_parameters)
)

namespace client
{
    // ---------------------------------------------------------------------------
    template <typename Iterator>
    struct system_parser : 
        qi::grammar<Iterator, ascii::space_type, indicator_rec()>
    {
            system_parser() : 
                system_parser::base_type(equation)
            {
                    using qi::double_;
                    using qi::_val;
                    using qi::_1;
                    using boost::spirit::ascii::string;

                    equation %=
                            (indicator | indicators)                      
                            >> ':'
                            >> (parameters | parameter)
                            ;

                    indicator %= (string("ABC")|string("HIJ"))
                            ;

                    indicators %= '{' >> (indicator % ',') >> '}'
                            ;

                    parameter %= qi::uint_
                            ;

                    parameters %= '{' >> (parameter % ',') >> '}'
                            ;

                    BOOST_SPIRIT_DEBUG_NODE(equation);
                    BOOST_SPIRIT_DEBUG_NODE(parameter);
                    BOOST_SPIRIT_DEBUG_NODE(parameters);
                    BOOST_SPIRIT_DEBUG_NODE(indicator);
                    BOOST_SPIRIT_DEBUG_NODE(indicators);
            }

            qi::rule<Iterator, ascii::space_type, indicator_rec()>
                equation;

            qi::rule<Iterator, ascii::space_type, std::vector<std::string>()> 
                indicators;

            qi::rule<Iterator, ascii::space_type, std::string()> 
                designator, indicator;

            qi::rule<Iterator, ascii::space_type, unsigned> 
                parameter;

            qi::rule<Iterator, ascii::space_type, std::vector<unsigned>()> 
                parameters;
    };

    template <typename Iterator>
    bool parse_system( Iterator first, Iterator last, client::indicator_rec& rec )
    {
        system_parser<Iterator> parser;
        bool r = qi::phrase_parse( first, last, parser, ascii::space, rec );
        if (first != last) // fail if we did not get a full match
            return false;
        return r;
    }

    template <typename Iterator>
    bool parse_universe(Iterator first, Iterator last, std::vector<std::string>& v)
    {
        bool r = qi::phrase_parse(first, last,
                              //  Begin grammar -------------------------------------
                              (
                                                        (+(qi::alpha|qi::char_( "_" ))) >> ':' >> '{' >>
                                                            (+~qi::char_(",}")) % ','
                                                            >> '}'
                              )
                              ,
                              //  End grammar ---------------------------------------
                              ascii::space, v);

        if (first != last) // fail if we did not get a full match
            return false;
        return r;
    }
}

main( int argc, char* argv[] )
{
    std::string calculator( "{ABC,HIJ}:{10,15,20}" );
    client::indicator_rec rec;
    client::parse_system( calculator.begin(), calculator.end(), rec );
}
  • 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:13:49+00:00Added an answer on June 15, 2026 at 4:13 am

    This question from yesterday it’s about the same. The signature of every rule needs to use this function declarator syntax. The type before the parenthesis is the attribute “returned” from the rule (its synthesized attribute) and the types inside are the inherited attributes (you can find a simple example using them here).

    So if you are not using these inherited attributes, you must use rule_attribute_type() in your rule declaration. Ideally a failure to do so would result in a compiler error but, apparently due to the heterogeneous nature of the rule’s template parameters, unfortunately it’s not what happens.

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

Sidebar

Related Questions

Given the following string, I'd like to parse into a list of first names
I'm trying to parse a text file using parser combinators. I want to capture
Trying to parse some XML but apparently this is too much for a lazy
Trying to parse an SQL string and pull out the parameters. Ex: select *
Im trying to parse the string located in /proc/stat in a linux filesystem using
I have an input string I'm trying to parse. It might look like either
So I'm trying to a parse a file that has text in this format:
I have no idea why this is hanging. I'm trying to capture output from
I'm trying to parse this HTML block: <div class=v120WrapperInner><a href=/redirect?q=http%3A%2F%2Fwww.google.com%2Faclk%3Fsa%3DL%26ai%3DCKJh--O7tSsCVIKeyoQTwiYmRA5SnrIsB1szYhg2d2J_EAhABIJ7rxQ4oA1CLk676B2DJntmGyKOQGcgBAaoEFk_Qyu5ipY7edN5ETLuchKUCHbY4SA#0%26num%3D1%26sig%3DAGiWqtwtAf8NslosN7AuHb7qC7RviHVg7A%26q%3Dhttp%3A%2F%2Fwww.youtube.com%2Fwatch%253Fv%253D91sYT_8CN8Q%2526feature%253Dpyv%2526ad%253D3409309746%2526kw%253Dsusan%25252#0boyle&amp;adtype=pyv&amp;event=ad&amp;usg=bR7ErKA_3szWtQMGe2lt1dpxzHc= title=The Valley Downs Chicago><img
I'm trying to parse a Twitter atom feed in PHP but am running into

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.