Getting Compiling Error

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Getting Compiling Error

lu feng
Hi, All,
 I am newer to the spirit package.
 When I tried to write a small parser, it gives me a lot of compiling error messages.
 The problem is that I want to pass a reference of vector<unsigned> as inherited attribute.
 The reference is obtained from value element of map<string, vector<unsigned> >.
 Could anyone help me for it?
Thanks,
Feng Lu

 Here are the codes:

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>

#include <iostream>
#include <string>
#include <vector>
#include <map>

namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
namespace phoenix = boost::phoenix;
using qi::lexeme;
using ascii::char_;
using ascii::alnum;
using qi::uint_;
using qi::_val;
using qi::_1;
using qi::_r1;
using phoenix::ref;
using phoenix::push_back;

std::map<std::string, std::vector<unsigned> > result;
template <typename It>
struct nmap : qi::grammar<It, ascii::space_type> {
    nmap() : nmap::base_type(start) {
        start = smap % ';';
        smap = id[_val = _1] >> ':'>> nlist(ref(result)[_val]);
        id %= lexeme[+alnum];
        nlist = uint_[push_back(_r1, _1)] %  ',';
    };
    qi::rule<It, ascii::space_type> start;
    qi::rule<It, std::string(), ascii::space_type> smap;
    qi::rule<It, std::string(), ascii::space_type> id;
    qi::rule<It, void(std::vector<unsigned>&), ascii::space_type> nlist;
};

int main()
{
    std::string str = "A:1,2,3; B:4,5;A 7,8";
    nmap<std::string::iterator> parser;
    bool ret = qi::phrase_parse(str.begin(), str.end(), parser, ascii::space);
    return 0;
}


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

_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: Getting Compiling Error

Mario Lang
lu feng <[hidden email]> writes:

>  I am newer to the spirit package.
>  When I tried to write a small parser, it gives me a lot of compiling error messages.
>  The problem is that I want to pass a reference of vector<unsigned> as inherited attribute.
>  The reference is obtained from value element of map<string, vector<unsigned> >.
>  Could anyone help me for it?

There are of course several ways to do what you are trying to achieve.
However, I personally would favour a semantic action free grammar.
The version below does not use a global variable to push into,
but a post-processing step to make the AST into what you supposedly
want.
Not ideal if you have a lot of input, but much cleaner and easier to
work with.  In general, trying to massage data into a specific data
structure is possible with phoenix and semantic actions, but it tends to
complicate the grammar significantly.  It is often easier to adapt the
initial AST to what you're actually getting, and massage it later on in
your program separately.  At least that is my experience.

#include <boost/config/warning_disable.hpp>
#include <boost/fusion/include/std_pair.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>

#include <iostream>
#include <string>
#include <vector>
#include <map>

namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
using qi::lexeme;
using ascii::char_;
using ascii::alnum;
using qi::uint_;

template <typename It>
struct nmap : qi::grammar<It, std::vector<std::pair<std::string, std::vector<unsigned> > >(), ascii::space_type> {
    nmap() : nmap::base_type(start) {
        start = smap % ';';
        smap = id >> ':' >> nlist;
        id %= lexeme[+alnum];
        nlist = uint_ % ',';
    };
    qi::rule<It, std::vector<std::pair<std::string, std::vector<unsigned> > >(), ascii::space_type> start;
    qi::rule<It, std::pair<std::string, std::vector<unsigned> >(), ascii::space_type> smap;
    qi::rule<It, std::string(), ascii::space_type> id;
    qi::rule<It, std::vector<unsigned>(), ascii::space_type> nlist;
};

int main()
{
    std::string str = "A:1,2,3; B:4,5;A 7,8";
    nmap<std::string::iterator> parser;
    std::vector<std::pair<std::string, std::vector<unsigned> > > results;
    bool ret = qi::phrase_parse(str.begin(), str.end(), parser, ascii::space, results);
    if (ret) {
        std::map<std::string, std::vector<unsigned> > result;
        for (std::vector<std::pair<std::string, std::vector<unsigned> > >::const_iterator i = results.begin();
             i != results.end(); ++i) {
            std::vector<unsigned> &v = result[i->first];
            v.insert(v.end(), i->second.begin(), i->second.end());
        }
    }
    return 0;
}

--
CYa,
  ⡍⠁⠗⠊⠕

------------------------------------------------------------------------------
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general