Spirit2: Phoenix statements as parser semantics

View: New views
2 Messages — Rating Filter:   Alert me  

Spirit2: Phoenix statements as parser semantics

by Mathias.Born :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


I have a question regarding statements as parser semantics.
The following is a self contained example. I'm using
boost_trunk from svn 23/10/2009.

-----------------------------------------------------------------
#include <tchar.h>

#define _SECURE_SCL 1
#define _SCL_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#include <vector>
#include <string>
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/support.hpp>
#include <boost/spirit/include/phoenix.hpp>


using namespace boost::spirit;
using namespace boost;
using namespace std;

namespace ph = phoenix;
namespace pha = phoenix::arg_names;
namespace phl = boost::phoenix::local_names;
using phoenix::for_each;
using phoenix::begin;
using phoenix::end;

template <typename Iterator>
struct test1_parser : qi::grammar<Iterator, string(), ascii::space_type>
{
        test1_parser(): test1_parser::base_type(start)
        {
            using qi::_1;
            using ascii::char_;
            using ascii::blank;

            item_ch        %= char_ - '=' - eol;
            env_ref        %= "$(" >> +(item_ch - ')') >> ')';
            no_env_ref    = *(item_ch - env_ref);
            item_str    = item_ch[_val = _1];    // convert char to string
            item_part    %= env_ref | item_str;
            item        = +item_part[_val += _1];
//            item        = (+item_part)
//    (1)                 [_val = ph::accumulate(_1, ph::construct<std::string>(""))];
/*    (2)                 [
                             ph::for_each(_1,
                                ph::lambda
                                [
                                    _val += pha::_1
                                ]
                             )
                          ];
*/
            start        = item;
        }

        qi::rule<Iterator, string(), ascii::space_type> item_str, env_ref, no_env_ref, item_part, group_name, empty_line, line, groups, group, key_value, item, start;
        qi::rule<Iterator, char(), ascii::space_type> item_ch;
};


int _tmain(int argc, _TCHAR* argv[])
{
    return 0;
}

-----------------------------------------------------------------

It does compile using MS-VC++. What do I want:

"item = +item_part[_val += _1];"

is supposed to yield a single string instead of a vector<string> which
"+item_part" would produce. Hence the semantic action.

Even though this compiles, I wanted to play around with alternatives.
The second approach, labeled (1) and using
"item = (+item_part)" as rule also works. Very nice.

But why does (2) not compile?
I tried this test:
-------------------------
vector<string> v;
ph::for_each(pha::arg1,
        ph::lambda
        [
                cout << pha::arg1
        ]
) (v);
-------------------------
and it compiles.

Can I not use Phoenix statements as parser semantics?

Thank you and Best Regards,
Mathias



------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Spirit-general mailing list
Spirit-general@...
https://lists.sourceforge.net/lists/listinfo/spirit-general

Re: Spirit2: Phoenix statements as parser semantics

by Hartmut Kaiser :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> I have a question regarding statements as parser semantics.
> The following is a self contained example. I'm using
> boost_trunk from svn 23/10/2009.

I'm really busy with the preparations for the release of Spirit v2.1 so I
didn't investigate too closely. But from looking at your code it seems
currently not to be possible to use Phoenix lamdas in Spirit's semantic
actions, but I might be off and want to get back to that after the release.
Perhaps it's just a bug. I've put it on my list of things to revisit.

Regards Hartmut

-------------------
Meet me at BoostCon
http://boostcon.com




------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Spirit-general mailing list
Spirit-general@...
https://lists.sourceforge.net/lists/listinfo/spirit-general