|
Boost Users : |
Subject: Re: [Boost-users] [Spirit]Kleene star with arguments havingoptional attributes.
From: Torri, Stephen CIV NSWCDD, W15 (stephen.torri_at_[hidden])
Date: 2010-11-24 08:59:13
>From: boost-users-bounces_at_[hidden] on behalf of TONGARI
>Sent: Wed 11/24/2010 7:49 AM
>To: boost-users_at_[hidden]
>Subject: Re: [Boost-users] [Spirit]Kleene star with arguments havingoptional attributes.
>
>>2010/11/24, Paul Graphov <graphov_at_[hidden]>:
>> Is it possible to make such a parser without using semantic actions
>> like [bind(&string::append, res, _1)] or something like that, which seem
>> to be less elegant solution.
>
>The following works as expected:
>
>std::vector<boost::optional<char> > vec;
>bool r = qi::parse(begin, end, *('p' | qi::char_), vec);
>
>Why std::string does not? I have no idea, either.
If you can in the future providing a stand alone example describing a problem is very helpful in giving you a good answer. What I see from the simple grammar is that you are looking for a sequence of characters. The 'qi::char_' will parse any single character (see http://www.boost.org/doc/libs/1_45_0/libs/spirit/doc/html/spirit/qi/reference/char/char.html). The 'p' is a qi literal which is not meant to be parsed into the output string. I found in a simple example I used below that the 'p' character was placed in the output. I think the | operator used the qi::char_ to parse the 'p' character. Here is my example which places the parsed input string into a std::string.
What is happening beneath the code is that the sequence of zero or more characters is that is being placed into a std::vector<char> which happens to be automatically promoted into a std::string. Since I did not have the original email does this solve your problem?
Stephen
-------------------
#include <boost/spirit/home/qi.hpp>
#include <iostream>
int main (int, char**)
{
std::string input = "apapa";
std::string::const_iterator begin = input.begin();
std::string::const_iterator end = input.end();
std::string output;
bool r = boost::spirit::qi::parse ( begin,
end,
*(boost::spirit::qi::lit('p')|boost::spirit::qi::char_),
output );
if ( r )
{
std::cout << "Success: found - " << output << std::endl;
}
else
{
std::cout << "Failed to parse string" << std::endl;
}
return 0;
}
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