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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:09:17+00:00 2026-06-07T04:09:17+00:00

I have some issues with parser writing with Spirit::Qi 2.4. I have a series

  • 0

I have some issues with parser writing with Spirit::Qi 2.4.
I have a series of key-value pairs to parse in following format <key name>=<value>.

Key name can be [a-zA-Z0-9] and is always followed by = sign with no white-space between key name and = sign. Key name is also always preceded by at least one space.

Value can be almost any C expression (spaces are possible as well), with the exception of the expressions containing = char and code blocks { }.

At the end of the sequence of the key value pairs there’s a { sign.

I struggle a lot with writing parser for this expression. Since the key name always is preceded by at least one space and followed by = and contains no spaces I defined it as

  KeyName %= [+char_("a-zA-Z0-9_") >> lit("=")] ;

Value can be almost anything, but it can not contain = nor { chars, so I defined it as:

  Value %=  +(char_ - char_("{=")) ;

I thought about using look-ahead’s like this to catch the value:

ValueExpression 
    %= ( 
      Value  
      >> *space 
      >> &(KeyName | lit("{"))
    )
    ;

But it won’t work, for some reason (seems like the ValueExpression greedily goes up to the = sign and “doesn’t know” what to do from there). I have limited knowledge of LL parsers, so I’m not really sure what’s cooking here. Is there any other way I could tackle this kind of sequence?

Here’s example series:

EXP1=FunctionCall(A, B, C) TEST="Example String" \
AnotherArg=__FILENAME__ - 'BlahBlah' EXP2= a+ b+* {

Additional info: since this is a part of a much larger grammar I can’t really solve this problem any other way than by a Spirit.Qi parser (like splitting by ‘=’ and doing some custom parsing or something similar).

Edit:

I’ve created minimum working example here: http://ideone.com/kgYD8
(compiled under VS 2012 with boost 1.50, but should be fine on older setups as well).

  • 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-07T04:09:18+00:00Added an answer on June 7, 2026 at 4:09 am

    I’d suggest you have a look at the article Parsing a List of Key-Value Pairs Using Spirit.Qi.

    I’ve greatly simplified your code, while

    • adding attribute handling
    • removing phoenix semantic actions
    • debugging of rules

    Here it is, without further ado:

    #define BOOST_SPIRIT_DEBUG
    
    #include <boost/fusion/adapted.hpp>
    #include <boost/spirit/include/qi.hpp>
    #include <map>
    
    namespace qi = boost::spirit::qi;
    namespace fusion = boost::fusion;
    
    typedef std::map<std::string, std::string> data_t;
    
    template <typename It, typename Skipper>
    struct grammar : qi::grammar<It, data_t(), Skipper>
    {
        grammar() : grammar::base_type(Sequence)
        {
            using namespace qi;
    
            KeyName  = +char_("a-zA-Z0-9_") >> '=';
            Value    = qi::no_skip [+(~char_("={") - KeyName)];
            Sequence = +(KeyName > Value);
    
            BOOST_SPIRIT_DEBUG_NODE(KeyName);
            BOOST_SPIRIT_DEBUG_NODE(Value);
            BOOST_SPIRIT_DEBUG_NODE(Sequence);
        }
      private:
        qi::rule<It, data_t(), Skipper>      Sequence;
        qi::rule<It, std::string()>          KeyName; // no skipper, removes need for qi::lexeme
        qi::rule<It, std::string(), Skipper> Value;
    };
    
    template <typename Iterator>
    data_t parse (Iterator begin, Iterator end)
    {
        grammar<Iterator, qi::space_type> p;
    
        data_t data;
    
        if (qi::phrase_parse(begin, end, p, qi::space, data)) {
            std::cout << "parse ok\n";
            if (begin!=end) {
                std::cout << "remaining: " << std::string(begin,end) << '\n';
            }
        } else {
            std::cout << "failed: " << std::string(begin,end) << '\n';
        }
    
        return data;
    }
    
    int main ()
    {
        std::string test(" ARG=Test still in first ARG ARG2=Zombie cat EXP2=FunctionCall(A, B C) {" );
        auto data = parse(test.begin(), test.end());
    
        for (auto& e : data)
            std::cout << e.first << "=" << e.second << '\n';
    }
    

    Output will be:

    parse ok
    remaining: {
    ARG=Test still in first ARG 
    ARG2=Zombie cat 
    EXP2=FunctionCall(A, B C) 
    

    If you really wanted ‘{‘ to be part of the last value, change this line:

    Value    = qi::no_skip [+(char_ - KeyName)];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I finally finished building my Wavefront OBJ parser, but still have some issues rendering
I have some issues in setting the classpath for the a project for which
I have some issues with a PHP file that is not working properly. The
I have some issues with my EJB 3 timer where it is launched more
I'm have some issues adding a class to 2 navigation links on a theme.
I seems to have some issues in order to compile gproc from uwiger. I
Im starting with CoCos2d development and I think I have some issues with nodespace,
Building my first SL MVVM application (Silverlight4 RC) and have some issues i don't
I am integrating PayPal in my website But I have some issues in it.
I ran git pull origin and now I have some issues with merge my

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.