problems with boost::spirit

G

Goran

Hi all,

I wanna get boost::spirit to tell me which position in my string is
liable that parsing fails. Here's a fully compiling demonstration:

#--------------------------------------------------------------------
#include <iostream>
#include <string>
#include <boost/spirit.hpp>

using namespace std;
using namespace boost::spirit;

int main()
{
string s = "#x";
rule<> r = chlit<>('#') >> chlit<>('X') >> *space_p;

parse_info<> p = parse(s.c_str(),r);
if(p.full)
cout << "File is OK:\n";
else
cout << "Error at pos " << p.length << ".\n";

return 0;
}
#--------------------------------------------------------------------

With this I get:

Error at pos 4294967295.

But if "string s = "#Xx" I get:

Error at pos 2.

This is what I want. But why do I get 4294967295 which is 2^32-1.

Thanks for any comments,
Goran.
 
A

Alf P. Steinbach

* Goran:
Hi all,

I wanna get boost::spirit to tell me which position in my string is
liable that parsing fails. Here's a fully compiling demonstration:

#--------------------------------------------------------------------
#include <iostream>
#include <string>
#include <boost/spirit.hpp>

using namespace std;
using namespace boost::spirit;

int main()
{
string s = "#x";
rule<> r = chlit<>('#') >> chlit<>('X') >> *space_p;

parse_info<> p = parse(s.c_str(),r);
if(p.full)
cout << "File is OK:\n";
else
cout << "Error at pos " << p.length << ".\n";

return 0;
}
#--------------------------------------------------------------------

With this I get:

Error at pos 4294967295.

But if "string s = "#Xx" I get:

Error at pos 2.

This is what I want. But why do I get 4294967295 which is 2^32-1.

Thanks for any comments,

With 32-bit two's complement int 2^32-1 is the representation of -1, which is
the usual value of EOF.

But don't follow on that, it's an implementation detail.

Docs say:

<quote>
parse_info
stop Points to the final parse position (i.e The parser recognized and
processed the input up to this point)
hit True if parsing is successful. This may be full: the parser consumed all
the input, or partial: the parser consumed only a portion of the input.
full True when we have a full match (i.e The parser consumed all the input).
length The number of characters consumed by the parser. This is valid only if
we have a successful match (either partial or full).
</quote>

I.e. you should use 'p.stop-s.c_str()' instead of 'p.length'; and ideally you
shouldn't call c_str() twice (AFAIK formally it has no obligation to return the
same pointer) but store that pointer from the first call.

By the way, at least with my old Boost, version something something, with g++
it's necessary to use option '-W-no-long-long' to add 'long long' support,
making the compiler non-standard just to compile Boost code, in particular this.


Cheers & hth.,

- Alf

PS: It really helps to read the documentation. ;-)
 
G

Goran

* Goran:
















With 32-bit two's complement int 2^32-1 is the representation of -1, which is
the usual value of EOF.

But don't follow on that, it's an implementation detail.

Docs say:

<quote>
parse_info
stop    Points to the final parse position (i.e The parser recognized and
         processed the input up to this point)
hit     True if parsing is successful. This may be full: the parser consumed all
         the input, or partial: the parser consumed only a portion of the input.
full    True when we have a full match (i.e The parser consumed all the input).
length  The number of characters consumed by the parser. This is valid only if
         we have a successful match (either partial or full).
</quote>

I.e. you should use 'p.stop-s.c_str()' instead of 'p.length'; and ideally you
shouldn't call c_str() twice (AFAIK formally it has no obligation to return the
same pointer) but store that pointer from the first call.

By the way, at least with my old Boost, version something something, with g++
it's necessary to use option '-W-no-long-long' to add 'long long' support,
making the compiler non-standard just to compile Boost code, in particular this.

Cheers & hth.,

- Alf

PS: It really helps to read the documentation. ;-)

Thanks, this solved my problem.
 

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,772
Messages
2,569,593
Members
45,108
Latest member
AlbertEste
Top