C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

help with boost::spirit

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
alliecat
Guest





PostPosted: Thu Jan 20, 2005 12:20 pm    Post subject: help with boost::spirit Reply with 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 <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





PostPosted: Fri Jan 21, 2005 10:17 am    Post subject: Re: help with boost::spirit Reply with quote



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





PostPosted: Sat Jan 22, 2005 5:07 am    Post subject: Re: help with boost::spirit Reply with quote



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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.