By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,369 Members | 1,631 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,369 IT Pros & Developers. It's quick & easy.

String Parser using BOOST.Spirit

P: n/a
hi,

i have problem implemting a string parser that parser comman delimited
string:
"str1,str2,str3"
INTO:
1. str1
2. str2
3. str3
*also strings are of any string (no specific string/keyword)

I have this code below, what it does so far is to parse specific
string: "x1", "x2", "y1", "y2"
but what i need is a parser for any kind of string (comma delimited)

bool
parse_string(char const* str, vector<string>&v)
(
return parse(str,
(
(str_p("x1"[push_back_a(v)]) |
str_p("x2")[push_back_a(v)] )
*(',' >> (str_p("y1")[push_back_a(v)] |

str_p("y2")[push_back_a(v)] ) )
),
space_p).full;
)

example use: INPUT: "x1,y2,y1,y2"
OUTPUT:
x1
y2
y1
y2

what i need is:
example: INPUT: "this,is,a,test"
OUTPUT:
this
is
a
test

ps. i need to implement this using BOOST.Spirit
--
Thx

Apr 2 '06 #1
Share this Question
Share on Google+
13 Replies


P: n/a
krbyxtrm wrote:
hi,

i have problem implemting a string parser that parser comman delimited
string:
"str1,str2,str3"
INTO:
1. str1
2. str2
3. str3
*also strings are of any string (no specific string/keyword)


Your better off joining the boost.spirit mailing list at:
http://lists.sourceforge.net/lists/l...spirit-general

Also I'd be surprised if there weren't an example of doing exactly that in
your spirit installation. Something like rule<> comma_delimited =
(*(anychar_p-','))[push_back_a(a)] % ',';

Jeff Flinn
Apr 3 '06 #2

P: n/a

Also I'd be surprised if there weren't an example of doing exactly
that in your spirit installation. Something like rule<>
comma_delimited = (*(anychar_p-','))[push_back_a(a)] % ',';


OT, but: What is that % operator used for?
Apr 3 '06 #3

P: n/a
Gernot Frisch wrote:
Also I'd be surprised if there weren't an example of doing exactly
that in your spirit installation. Something like rule<>
comma_delimited = (*(anychar_p-','))[push_back_a(a)] % ',';


OT, but: What is that % operator used for?


From the docs at http://www.boost.org/libs/spirit/doc/operators.html,

a % b ; Match a list of one or more repetitions of a separated by
occurrences of b. This is the same as a >> *(b >> a). Note that a must not
also match b.

Jeff
Apr 3 '06 #4

P: n/a
> Gernot Frisch wrote:
OT, but: What is that % operator used for?


Awesome expression metatemplate techniques are never off-topic!

(And could we all learn to stand up to the chronic naggers, instead of
cringing each time a thread strays off The C++ Standard?)

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 3 '06 #5

P: n/a
Phlip wrote:
Gernot Frisch wrote:

OT, but: What is that % operator used for?


Awesome expression metatemplate techniques are never off-topic!


I don't consider them awesome. Actually, it's the worst case of operator
abuse I've ever seen.

Apr 3 '06 #6

P: n/a
Rolf Magnus wrote:
I don't consider them awesome. Actually, it's the worst case of operator
abuse I've ever seen.


Can you implement an efficient and appealing parser generator directly in
C++ without abusing the occassional operator?

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 3 '06 #7

P: n/a
Phlip wrote:
Rolf Magnus wrote:
I don't consider them awesome. Actually, it's the worst case of operator
abuse I've ever seen.


Can you implement an efficient and appealing parser generator directly in
C++ without abusing the occassional operator?


No, and I wouldn't want to do that either.
Apr 3 '06 #8

P: n/a
Hello people,

I have this parser that parses string that represents a function,
CONSIDER:

bool
parse_info<> x_parser3(char const* str, FunctionSpec& spec)
{
// some rules...
rettype = lexeme_d[(alpha_p)
*(alnum_p | space_p | ch_p('*'))][&do_ret];
// assign function name when match...
functionName = lexeme_d[(alpha_p | ch_p('_')) *(alnum_p | ch_p('_'))][assign_a(spec.m_Name)];

// other rules....
....
return parse(str,top);
}

for the functionName rule it was easy, just assign the name.
but for the rettype rule its much harder since i should not be assigned
directly,
since the FunctionSpec struct look like this:

strcut FunctionSpec {
std::string m_Name;
enum eVarType {
VAR_VOID, VAR_BOOL, VAR_INT...
}
eVarType m_RetType;
}

How can i assign value for 'm_RetType' ? since [do_ret] callback on
rettype rule is like this: do_ret[char const*,char const*], is there a
way around here?

Apr 3 '06 #9

P: n/a
krbyxtrm wrote:
I have this parser that parses string that represents a function,


Your post has two little problems:

- it's a new question, so it gets a new Subject
- the best place for Boost questions are its mailing lists

Just so you understand how topicality works, _I_ don't mind reading Boost
traffic here, because it's very important, and I'm not on that mailing
list. But _you_ will get the best answer on the narrowest possible
technical forum. There are too many people here who don't know enough about
Boost to qualify.

Try the Boost.Spirit mailing list for this one.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 3 '06 #10

P: n/a

"Rolf Magnus" <ra******@t-online.de> schrieb im Newsbeitrag
news:e0*************@news.t-online.com...
Phlip wrote:
Gernot Frisch wrote:

OT, but: What is that % operator used for?


Awesome expression metatemplate techniques are never off-topic!


I don't consider them awesome. Actually, it's the worst case of
operator
abuse I've ever seen.


I've used spirit since a very early version. Luckily I didn't choose
Lex/Yacc back then! If you get into the syntax of spirit, it's the
fastmost-awesome-incredibly-rapid way of programming complex parsers.
Apr 4 '06 #11

P: n/a
Do you have an idea on how to let the rules work with something like
this:

void do_ret()
{
//something to do here.
}
x_paser3(char const* str, FunctionSpec& spec/* struct */)
{
rule<> rettype = (<some rule...>)[<some_thing_should_be_here>]
return pase(....)
}

rettype here returns the matched string in the actual code i made:
"void","void *","int","unsigned int" etc.

in my current code, i just push this inside a vector... but as i said
earlier, the FunctionSpec member m_Rettype should be assigned with
defined type
defined by enum or any enumerated list...

info: FunctionSpec struct is structured representation of the function
string...

overall i think i need to used a class with operator(), as a
replacement for the <some_thing_should_be_here> area in the above code.
with that will i pass to that class is the FunctionSpec struct....

Ayon kay Gernot Frisch:
"Rolf Magnus" <ra******@t-online.de> schrieb im Newsbeitrag
news:e0*************@news.t-online.com...
Phlip wrote:
Gernot Frisch wrote:

> OT, but: What is that % operator used for?

Awesome expression metatemplate techniques are never off-topic!


I don't consider them awesome. Actually, it's the worst case of
operator
abuse I've ever seen.


I've used spirit since a very early version. Luckily I didn't choose
Lex/Yacc back then! If you get into the syntax of spirit, it's the
fastmost-awesome-incredibly-rapid way of programming complex parsers.


Apr 4 '06 #12

P: n/a
ps. problem is that i can't make the operator() work>
i tried operator() (char const*,char const*)...

Ayon kay krbyxtrm:
Do you have an idea on how to let the rules work with something like
this:

void do_ret()
{
//something to do here.
}
x_paser3(char const* str, FunctionSpec& spec/* struct */)
{
rule<> rettype = (<some rule...>)[<some_thing_should_be_here>]
return pase(....)
}

rettype here returns the matched string in the actual code i made:
"void","void *","int","unsigned int" etc.

in my current code, i just push this inside a vector... but as i said
earlier, the FunctionSpec member m_Rettype should be assigned with
defined type
defined by enum or any enumerated list...

info: FunctionSpec struct is structured representation of the function
string...

overall i think i need to used a class with operator(), as a
replacement for the <some_thing_should_be_here> area in the above code.
with that will i pass to that class is the FunctionSpec struct....

Ayon kay Gernot Frisch:
"Rolf Magnus" <ra******@t-online.de> schrieb im Newsbeitrag
news:e0*************@news.t-online.com...
Phlip wrote:

>> Gernot Frisch wrote:
>
>>> OT, but: What is that % operator used for?
>
> Awesome expression metatemplate techniques are never off-topic!

I don't consider them awesome. Actually, it's the worst case of
operator
abuse I've ever seen.


I've used spirit since a very early version. Luckily I didn't choose
Lex/Yacc back then! If you get into the syntax of spirit, it's the
fastmost-awesome-incredibly-rapid way of programming complex parsers.


Apr 4 '06 #13

P: n/a
krbyxtrm wrote:
ps. problem is that i can't make the operator() work>
i tried operator() (char const*,char const*)...


Now your taking advantage of the politeness of the responders here. You've
been directed twice to the boost mailing list, which if you read the link
provided in an earlier email, states how you can also view the mail list
using a news reader. Not only will you get more informed responses as philip
stated, but it your posting will help others in similar situations.

Jeff Flinn
Apr 4 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.