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

  • Home
  • SEARCH
  • 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 8968211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:25:08+00:00 2026-06-15T17:25:08+00:00

I am using boost::spirit for constructing a parser for a simple script language. In

  • 0

I am using boost::spirit for constructing a parser for a simple script language. In a rule, I want to use a call to a fabric method to create a new object which is the return value of this rule.

The rule is

qi::rule<Iterator, NodeHandle(), Skipper>      value;

and defined as

value = ( '%' >> +(qi::alpha) >> '%')
        [phoenix::bind(&CreateVarNode, qi::_val, qi::_1)];

Actually, these “NodeHandles” are constructed via these kind of methods

NodeHandle CreateVarNode(std::wstring var)
{
    Node::ptr node(new VarTerminal(var));
    return NodeHandle(node);        
}

The problem is, that spirit does not like these kind of constructs for rules. Can you help me on how to implement a rule which uses this fabric method to return this object?

Thank you!

Cheers,
Chris

  • 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-15T17:25:09+00:00Added an answer on June 15, 2026 at 5:25 pm

    Assuming the factory functions are actually namespace-level functions:

    namespace Factories
    {
        NodeHandle CreateVarNode(std::wstring var)
        {
            Node::ptr node(new VarTerminal(var));
            return NodeHandle(node);        
        }
    }
    

    This should work nicely:

    value = ( '%' >> +(qi::alpha) >> '%')
            [qi::_val = phoenix::bind(&Factories::CreateVarNode, qi::_1)];
    

    This should work unaltered if it was actually a static member of a class named Factories. You could also implement a bit of convenience and write it like:

    value = ( '%' >> +(qi::alpha) >> '%') [ createvar ];
    

    This requires a bit of plumbing to use Phoenix functions. Full sample:

    #define BOOST_SPIRIT_USE_PHOENIX_V3
    
    #include <boost/spirit/include/qi.hpp>
    namespace qi  = boost::spirit::qi;
    namespace phx = boost::phoenix;
    
    struct VarNode    { VarNode    (std::string        ){ /*todo*/ }  } ;
    struct LitNode    { LitNode    (double             ){ /*todo*/ }  } ;
    struct Assignment { Assignment (std::string, double){ /*todo*/ }  } ;
    
    struct Node { typedef Node* ptr; };
    struct NodeHandle { /*todo*/ };
    
    template <typename ProductNode>
    struct NodeFactory
    {
        template<typename... T> struct result { typedef NodeHandle type; };
    
        template<typename... T>
            NodeHandle operator()(T&&... a) const
            {
                Node::ptr node(new ProductNode(std::forward<T>(a)...));
                return NodeHandle(node);
            }
    };
    
    int main ()
    {
        static const phx::function<NodeFactory<VarNode> >    createvar;
        static const phx::function<NodeFactory<LitNode> >    createliteral;
        static const phx::function<NodeFactory<Assignment> > createassign;
    
        qi::rule<std::string::const_iterator, NodeHandle()> value 
            = ( '%' >> +(qi::alpha) >> '%') [createvar];
    
        const std::string input("%a%");
        auto f=begin(input), l=end(input);
        assert(qi::parse(f, l, value));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using boost::spirit to write a parser, lexer here is what i want to
I am using Boost Spirit parser, and as the parser is parsing, semantic actions
I am using boost build in my project and now i want to use
I am trying to use Boost.Spirit (V. 2.5) library to create a mini-calculator. Features
Recently, I try to use the boost::spirit::qi binary endian parser to parse some binary
I want to parse a file into an AST using Boost Spirit. The root
I'm new to Boost Spirit and trying to write JSON parser using Boost Spirit
I'm trying to write a parser using Boost::Spirit, and I have the parser written
Using boost.spirit I try to parse simple command line of the form command:param1 param2...
I'm trying to compile a simple grammar using Boost.Spirit. I'm using g++ 4.7.0 and

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.