Boost logo

Boost Users :

Subject: Re: [Boost-users] [Spirit] Grammar rule problem
From: OvermindDL1 (overminddl1_at_[hidden])
Date: 2009-10-30 11:55:18


On Fri, Oct 30, 2009 at 9:30 AM, Dylan Klomparens
<dylan.klomparens_at_[hidden]> wrote:
> Hello,
>
> I'm writing a configuration file parser with boost::spirit, and have
> encountered some strange errors. The errors are:
>
> 1) error C2078: too many initializers (in file assign_actor.hpp, line 62)
>
> 2) error C2440: 'initializing' : cannot convert from 'const iterator_t ' to
> 'value_type' (in file assign_actor.hpp, line 62)
>
> The line that's causing the error is identical (in concept) to the one
> above. Why is it generating an error? The code I'm using is below. Rule
> "two" is generating the error.
>
> #include <iostream>
> #include <boost/spirit.hpp>
> using namespace std;
>
> int main()
> {
>     using namespace boost::spirit;
>     const int UnsignedCharacterMaximum = (numeric_limits<unsigned
> char>::max)();
>     const int UnsignedShortMaximum = (numeric_limits<unsigned
> short>::max)();
>     rule<> octet_p = limit_d(0, UnsignedCharacterMaximum)[int_p];
>     rule<> ip_address_p = octet_p >> ch_p('.') >> octet_p >> ch_p('.') >>
> octet_p >> ch_p('.') >> octet_p;
>     rule<> network_port_p = limit_d(0, UnsignedShortMaximum - 1)[int_p];
>
>     string s;
>     int i;
>     rule<> one = ip_address_p[assign_a(s)];
>     rule<> two = network_port_p[assign_a(i)]; // Commenting this line gets
> rid of the error.
>     // But this line should work. It is identical (in conecpt) to the
> previous line.
>
>     cout << parse("192.168.0.1", one).full << endl;
>
>     return 0;
> }

You should post Spirit questions to the Spirit list.

Based on your code you are using the old Spirit.Classic, I do not know
enough about it to say, however in Spirit.Qi (the latest version), I
would do it like this:

    uint_parser<unsigned char,1,3> uint8_;
    rule<string::iterator, string()> ip_address = uint8_ >> '.' >>
uint8_ >> '.' >> uint8_ >> '.' >> uint8_;
    rule<string::iterator, short()> network_port = ushort_;

    string testIP("192.168.0.1");
    string testPort("1024");
    string s;
    short i;
    if(parse(testIP.begin(),testIP.end(), ip_address, s))
        cout << "IP: " << s << endl;
    else
        cout << "IP could not be parsed" << endl;
    if(parse(testPort.begin(),testPort.end(), network_port, i))
        cout << "Port: " << i << endl;
    else
        cout << "Port could not be parsed" << endl;

s would then contain "192.168.0.1" and i would then contain the integer 1024.
Not sure my syntax is perfect, but close enough. As you can see
though, it is a lot easier to do in Spirit.Qi instead of
Spirit.Classic, and it will also run faster as well.

As stated though, I do not know Spirit.Classic's syntax that well, but
you may still get a response from someone that does. You will have a
lot better chance of getting a response if you post this to the Spirit
mailing list though.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net