String Parser using BOOST.Spirit

K

krbyxtrm

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
 
J

Jeff Flinn

krbyxtrm said:
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/listinfo/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
 
G

Gernot Frisch

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?
 
J

Jeff Flinn

Gernot said:
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
 
P

Phlip

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?)
 
R

Rolf Magnus

Phlip said:
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.
 
P

Phlip

Rolf said:
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?
 
R

Rolf Magnus

Phlip said:
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.
 
K

krbyxtrm

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?
 
P

Phlip

krbyxtrm said:
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.
 
G

Gernot Frisch

Rolf Magnus said:
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.
 
K

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:
 
K

krbyxtrm

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:
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.
 
J

Jeff Flinn

krbyxtrm said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top