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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:37:03+00:00 2026-05-29T06:37:03+00:00

I have a simple grammar consisting of mixed variables ( $(name) ) and variable-value

  • 0

I have a simple grammar consisting of mixed variables ($(name)) and variable-value pairs ($(name:value)). I have a hand-coded recursive parser, but am interested in using it as an exercise to learn Spirit, which I’ll need for more complex grammars eventually(/soon).

Anyway, the set of possible forms I’m working with (simplified from the full grammar) is:

$(variable)     // Uses simple look-up, recursion and inline replace
$(name:value)   // Inserts a new variable into the local lookup table

My current rules look something like:

typedef std::map<std::string, std::string> dictionary;

template <typename Iterator>
bool parse_vars(Iterator first, Iterator last, dictionary & vars, std::string & output)
{
    using qi::phrase_parse;
    using qi::_1;
    using ascii::char_;
    using ascii::string;
    using ascii::space;
    using phoenix::insert;

    dictionary statevars;

    typedef qi::rule<Iterator, std::string()> string_rule;
    typedef qi::rule<Iterator, std::pair<std::string, std::string>()> pair_rule;

    string_rule state = string >> ':' >> string; // Error 3
    pair_rule variable = 
    (
        char_('$') >> '(' >> 
        (
            state[insert(phoenix::ref(statevars), _1)] |
            string[output += vars[_1]] // Error 1, will eventually need to recurse
        ) >> ')'
    ); // Error 2

    bool result = phrase_parse
    (
        first, last, 
        (
            variable % ','
        ), 
        space
    );

    return r;
}

If it wasn’t obvious, I have no idea how Spirit works and the docs have everything but actual explanations, so this is about an hour of throwing examples together.

The parts I particularly question are the leading char_('$') in the variable rule, but removing this causes a shift operator error (the compiler interprets '$' >> '(' as a right-shift).

When compiling, I get errors related to the state rule, particularly creating the pair, and the lookup:

  1. error C2679: binary ‘[‘ : no operator found which takes a right-hand operand of type ‘const boost::spirit::_1_type’ (or there is no acceptable conversion)
  2. error C2512: ‘boost::spirit::qi::rule::rule’ : no appropriate default constructor available

Changing the lookup (vars[_1]) to a simple += gives:

3. error C2665: ‘boost::spirit::char_class::classify::is’ : none of the 15 overloads could convert all the argument types

Error 1 seems to relate to the type (attribute?) of the _1 placeholder, but that should be a string, and is when used for printing or concatenation to the output string. 2 appears to be noise caused by 1.

Error 3, digging down the stack of template errors, seems to relate to not being able to turn the state rule into a pair, which seems odd as it almost exactly matches one of the rules from this example.

How can I modify the variable rule to properly handle both input forms?

  • 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-29T06:37:04+00:00Added an answer on May 29, 2026 at 6:37 am

    A few things to note:

    1. To adapt std::pair (so you can use it with maps) you should include (at least)

      #include <boost/fusion/adapted/std_pair.hpp>
      
    2. It looks like you are trying to create a symbol table. You could use qi::symbols for that

    3. avoid mixing output generation with parsing, it complicates matters unduly

    I haven’t ‘fixed’ all the above (due to lack of context), but I’d happy to help out with any other questions arising from those.

    Here is a fixed code version staying pretty close to the OP. Edit have tested it too now, output below:

    #include <boost/spirit/include/qi.hpp>
    #include <boost/spirit/include/phoenix.hpp>
    #include <boost/fusion/adapted/std_pair.hpp>
    #include <map>
    
    namespace qi    = boost::spirit::qi;
    namespace phx   = boost::phoenix;
    
    typedef std::map<std::string, std::string> dictionary;
    
    template <typename Iterator, typename Skipper = qi::space_type>
        struct parser : qi::grammar<Iterator, Skipper>
    {
        parser(dictionary& statevars, std::string& output) : parser::base_type(start)
        {
            using namespace qi;
            using phx::insert; 
    
            with_initializer = +~char_(":)") >> ':' >> *~char_(")");
    
            simple           = +~char_(")");
    
            variable         = 
                "$(" >> (
                       with_initializer  [ insert(phx::ref(statevars), qi::_1) ] 
                     | simple            [ phx::ref(output) += phx::ref(statevars)[_1] ]
                 ) >> ')';
    
            start = variable % ',';
    
            BOOST_SPIRIT_DEBUG_NODE(start);
            BOOST_SPIRIT_DEBUG_NODE(variable);
            BOOST_SPIRIT_DEBUG_NODE(simple);
            BOOST_SPIRIT_DEBUG_NODE(with_initializer);
        }
    
      private:
        qi::rule<Iterator, std::pair<std::string, std::string>(), Skipper> with_initializer;
        qi::rule<Iterator, std::string(), Skipper> simple;
        qi::rule<Iterator, Skipper> variable;
        qi::rule<Iterator, Skipper> start;
    };
    
    template <typename Iterator>
    bool parse_vars(Iterator &first, Iterator last, dictionary & vars, std::string & output)
    {
        parser<Iterator> p(vars, output);
        return qi::phrase_parse(first, last, p, qi::space);
    }
    
    int main()
    {
        const std::string input = "$(name:default),$(var),$(name)";
        std::string::const_iterator f(input.begin());
        std::string::const_iterator l(input.end());
    
        std::string output;
        dictionary table;
    
        if (!parse_vars(f,l,table,output))
            std::cerr << "oops\n";
        if (f!=l)
            std::cerr << "Unparsed: '" << std::string(f,l) << "'\n";
        std::cout << "Output:   '" << output << "'\n";
    }
    

    Output:

    Output:   'default'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ANTLR3 simple grammar parser that takes short lines of text and
I have this simple grammar for a C# like syntax. I can't figure out
I have a simple ANTLR grammar, which I have stripped down to its bare
I have simple form. <form target=_blank action=somescript.php method=Post id=simpleForm> <input type=hidden name=url value=http://...> <input
I have a simple grammar: grammar sample; options { output = AST; } assignment
This is a very simple task in every language I have ever used, but
I have this grammar to match simple logical predicates in ANTLR. exp : or
I have three types of questions: Vocab (represented by question:answer pairs) Grammar (represented by
I have a question regarding Antlr, I am building a simple parser with it
I want to make a simple JSP parser by using Treetop. Now, 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.