473,395 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

String Parser using BOOST.Spirit

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
13 8411
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

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

"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: sbobrows | last post by:
{Whilst I think much of this is OT for this newsgroup, I think the issue of understanding diagnostics just about gets under the door. -mod} Hi, I'm a C++ newbie trying to use the Boost regex...
1
by: Ingo Nolden | last post by:
Hi, I am using spirit 1.31 I have been trying the following example from the spirit docs. I tried it with int and double neither works: vector<int> v; rule<> r = list_p(int_p, ch_p(',')); ...
1
by: Alf P. Steinbach | last post by:
Microsoft Visual C++ warning C4267 (the number is not a typo) can occur e.g. when compiling Boost Spirit, or when doing std::size_t x = 0; std::cout << x; when compiled with 64-bit...
3
by: Paul van Hagen | last post by:
Hello, I've been doing some research as to ways to optimise our parsing and evaluation of mathematical expressions and came across the spirit template library as part of the boost library. It...
4
by: Marcin Kalicinski | last post by:
Hi All, Does anybody has experience compiling XML parser written with boost::spirit on gcc? The parser is based on http://spirit.sourceforge.net/repository/applications/xml.zip samples. What...
2
by: Rahul | last post by:
Hey Guys I have a development environment, in which the whole SQL syntax is stored in the Database. So the syntax in the databse column could be "where BirthDate = '12/31/2005' and ID =...
3
by: Chris Jones | last post by:
Hi, I've experimenting with using boost::pool_allocator with std::vector and gcc (4.1)., and I am having problems with segmentation violations. Below I give a simple example of one way I am...
3
by: =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki | last post by:
How do it's by next of boost.spirit library it are not discrimitation a letter size that a small letters and big letters it are not discrimitation? I'm know how write s small parser by next of...
4
by: Man4ish | last post by:
namespace ve/////////////////ve.h { struct VertexProperties { std::size_t index; boost::default_color_type color; }; }...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.