Boost logo

Boost Users :

Subject: Re: [Boost-users] Spirit question
From: Hartmut Kaiser (hartmut.kaiser_at_[hidden])
Date: 2010-03-17 17:46:30


> Beg pardon, wrong question.
>
> Would you give me an example of a separate string id parser (like
> ident?) used in a comma-separated-list parser? In short, I don't
> know how to resolve the discrepancy in attributes.

Do you mean something like this?

#include <boost/spirit/home/qi.hpp>

namespace ecgp {

  namespace qi = boost::spirit::qi;
  namespace ascii = boost::spirit::ascii;

  template <typename Iterator>
  struct ident
    : qi::grammar<Iterator, std::string(), ascii::space_type>
  {

    ident(): ident::base_type(start) {
      using namespace qi;
      start %= lexeme[('_' | alpha) >> *(digit | alpha | '_' | '-')];
    }
    qi::rule<Iterator, std::string(), ascii::space_type> start;
  };

  template <typename Iterator>
  struct idlist
    : qi::grammar<Iterator, std::vector<std::string>(), ascii::space_type>
  {

    ecgp::ident<Iterator> id;
    idlist(): idlist::base_type(start) {
      using namespace qi;
      start %= id % ','; // don't use a sequence here
    }

    qi::rule<Iterator, std::vector<std::string>(), ascii::space_type> start;
  };
}

namespace qi = boost::spirit::qi;

int main()
{
    ecgp::idlist<std::string::const_iterator> g;
    std::string input("abc,def");
    std::string::const_iterator begin = input.begin();
    std::string::const_iterator end = input.end();

    std::vector<std::string> v;
    qi::phrase_parse(begin, end, g, qi::ascii::space, v);
    BOOST_ASSERT(v.size() == 2 && v[0] == "abc" && v[1] == "def");
    return 0;
}

Regards Hartmut

---------------
Meet me at BoostCon
www.boostcon.com


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net