 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
alliecat Guest
|
Posted: Thu Jan 20, 2005 12:20 pm Post subject: help with boost::spirit |
|
|
Hi Group,
I'm starting to learn to use boost::spirit but I am having a problem
understanding the behaviour of a simple example. Any suggestions and
corrections would be appreciated.
Just as a test, I'd like to parse this string:
"SET albert fred 123"
with the "SET" being identified as a keyword, and the "123" identified
as an integer, and "albert" and "fred" identified as names.
To my surprise, the output from the code below is:
SET
NAME='albert fred 123'
rather than (what *I* thought I'd get)
SET
NAME='albert'
NAME='fred'
INT=123
Any suggestions?
Thanks,
Stuart Allie
Sample code:
------------
#include <iostream>
#include <string>
#include <boost/spirit/core.hpp>
#include <boost/spirit/utility.hpp>
using namespace boost::spirit;
void do_set(const char* first, const char* last) {
std::cout << "SETn";
};
void do_int(int val) {
std::cout << "INT=" << val << "n";
};
void do_name(const char* first, const char* last) {
std::string s(first, last);
std::cout << "NAME='" << s << "'n";
};
int main (int argc, char** argv) {
parse( "SET albert fred 123",
*( str_p("SET")[&do_set]
| Quote: | (alpha_p >> *(alnum_p))[&do_name]
int_p[&do_int]
) |
, space_p
);
return 0;
};
----------------
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Carl Barron Guest
|
Posted: Fri Jan 21, 2005 10:17 am Post subject: Re: help with boost::spirit |
|
|
In article <1106183680.357405.165990 (AT) c13g2000cwb (DOT) googlegroups.com>,
alliecat <stuart.allie (AT) hydro (DOT) com.au> wrote:
| Quote: | Hi Group,
I'm starting to learn to use boost::spirit but I am having a problem
understanding the behaviour of a simple example. Any suggestions and
corrections would be appreciated.
Just as a test, I'd like to parse this string:
"SET albert fred 123"
with the "SET" being identified as a keyword, and the "123" identified
as an integer, and "albert" and "fred" identified as names.
To my surprise, the output from the code below is:
SET
NAME='albert fred 123'
rather than (what *I* thought I'd get)
SET
NAME='albert'
NAME='fred'
INT=123
Any suggestions?
Thanks,
Stuart Allie
Sample code:
------------
#include <iostream
#include
#include
#include
using namespace boost::spirit;
void do_set(const char* first, const char* last) {
std::cout << "SETn";
};
void do_int(int val) {
std::cout << "INT=" << val << "n";
};
void do_name(const char* first, const char* last) {
std::string s(first, last);
std::cout << "NAME='" << s << "'n";
};
int main (int argc, char** argv) {
parse( "SET albert fred 123",
*( str_p("SET")[&do_set]
| (alpha_p >> *(alnum_p))[&do_name]
| int_p[&do_int]
)
, space_p
);
return 0;
};
I reworte main()'s parse call to |
parse( "SET albert fred 123"
*( str_p("SET")[&do_set]
| Quote: | (alpha_p >> *(alnum_p))[&do_name]
int_p[&do_int]
space_p ));
|
and it works. See 'scanners and parsers' in the spirit docs for
details.
Logically I don't see the problem with the original code and why it
thinks a space is an alphanumeric char, but this works.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Guest Guest
|
Posted: Sat Jan 22, 2005 5:07 am Post subject: Re: help with boost::spirit |
|
|
Stuart,
You should use boost::spirit::lexeme_d to turn off whitespace skipping
for your "name" parser.
lexeme_d[alpha_p >> *alnum_p][&do_name]
By the way, you don't need the semi-colons at the end of functions.
Regards, Thomas
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|