|
Boost Users : |
Subject: Re: [Boost-users] [spirit][qi] Iteration / string iterator
From: alfC (alfredo.correa_at_[hidden])
Date: 2010-10-03 02:00:07
On Oct 1, 7:35 pm, OvermindDL1 <overmind..._at_[hidden]> wrote:
> On Fri, Oct 1, 2010 at 2:34 PM, alfC <alfredo.cor..._at_[hidden]> wrote:
> > can anybody explain to me why this example from the Spirit.Qi manual
> > works
>
> >http://www.boost.org/doc/libs/1_44_0/libs/spirit/example/qi/complex_n...
>
> > and when I try to use the string iterators directly I get a horrific
> > compiler error? Something to do with string iterators being pointers
> > or something?
>
> > std::string s = "(2, 3)"; // < line added to example
> > bool r = phrase_parse(s.begin(), s.end(), //< only line
> > changed in the example was (first, last)
>
> You are using GCC, phrase_parse's first parameter must be non-const.
right in the spot. Thank you. That worked.
I have another question in general on Spirit.Qi. I read the manual
many times and I still can't figure out how to read 'words' from the
parser. In the following example I want to parse the name of something
but I can not use the semantic assignment on std::string. ? The
pattern is matched if I disable the assignment line but it doesn't
compile with the line "[ref(name) = _1]". Again this is boost 1.43
with gcc 4.4.
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
using boost::spirit::qi::double_;
using boost::spirit::qi::int_;
using boost::spirit::qi::lexeme;
using boost::spirit::qi::char_;
using boost::spirit::qi::_1;
using boost::spirit::qi::phrase_parse;
using boost::spirit::ascii::space;
using boost::phoenix::ref;
int main(){
std::string test_line = " atom -1.4275 -1.84343 -1.9255 1
center_atom 12";
std::string::iterator first = test_line.begin(), last =
test_line.end();
double x=9999., y=9999., z=9999.; int type=-1;
std::string name ;
int index=-1;
bool parsed = phrase_parse(first, last,
(
"atom" >> double_[ref(x) = _1] >> double_[ref(y) = _1] >>
double_[ref(z) = _1] >> int_[ref(type) = _1] >>
(+char_-' ')
[ref(name) = _1] // < problem in this line, it
doesn't compile
>> int_[ref(index) = _1]
),
space
);
if(not parsed) std::clog << "can not interpret line, values read
can be incomplete" << std::endl;
std::clog <<"just read: x = " << x << " y = " << y << " z = " << z
<< " type = " << type << " name = " << name << " index = " << index <<
std::endl;
return 0;
}
Thank you,
Alfredo
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