Predefined Actions

The framework has two predefined semantic action functors. Experience shows that these functors are so often used that they were included as part of the core framework to spare the user from having to reinvent the same functionality over and over again.


Assign the value passed by the parser to the variable v.

Example usage:

    int i;
    std::string s;
    r = int_p[assign(i)] >> (+alpha_p)[assign(s)];

Given an input 123456 "Hello", assign(i) will extract the number 123456 and assign it to i, then, assign(s) will extract the string "Hello" and assign it to s. Technically, the expression assign(v) is a template function that generates a semantic action. The semantic action generated is polymorphic and should work with any type as long as it is compatible with the arguments received from the parser. It might not be obvious, but a string can accept the iterator first and last arguments that are passed into a generic semantic action (see above). In fact, any STL container that has an assign(first, last) member function can be used.

For reference and to aid users in writing their own semantic action functors, here's the implementation of the assign(v) action. We include it here since it is short and simple enough to understand.

The assign_actor class

    template <typename T>
    class assign_actor

        assign_actor(T& ref_)
        : ref(ref_) {}

        template <typename T2>
        void operator()(T2 const& val) const
        { ref = val; }

        template <typename IteratorT>
        operator()(IteratorT const& first, IteratorT const& last) const
        { ref.assign(first, last); }


        T& ref;

The assign function

    template <typename T>
    inline assign_actor<T> const
    assign(T& t)
        return assign_actor<T>(t);


Append the value passed by the parser to the container c.

Example usage:

    std::vector<int> v;
    r = int_p[append(v)] >> *(',' >> int_p[append(v)]);

The code above can parse a comma separated list of integers and stuff the numbers in the vector v. If it isn't obvious already, append(c) appends the parsed value (the argument passed into the semantic action by the parser) into the container c, which must have member functions insert(where, value) and end(). To cut the story short, STL containers are perfect candidates for append(c) to work on. Like assign(v), append(c) may also take in the iterator pairs. In which case, the container must have two member functions: insert(where, first, last) and end(); e.g. std::vector<std::string>.