Parsing question

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

Parsing question

Josue Andrade Gomes-2
Hi,

I need to parse the following information:

Name: John Smith Age: 36 cabd23e7cf

and extract:

   "John Smith"
   "36"
   "cabd23e7cf"

The last information is a hex encoded signature.
I've tried many ideas but none of them worked.
Unfortunately I do not have the option of change the format.

Any ideas?

Regards,
Josue Gomes


-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download
it for free - -and be entered to win a 42" plasma tv or your very own
Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: Parsing question

bill barry
A hex encoded signature?

[After some thought I removed the line of code I was just going to simply give you]

Is this classwork?
Reply | Threaded
Open this post in threaded view
|

Re: Parsing question

Josue Andrade Gomes-2
On 9/21/05, bill barry <[hidden email]> wrote:
> A hex encoded signature?
>
> Is this classwork?
>

How hex encoded signatures and classworks relate is a mistery to me. :)

> [After some thought I removed the line of code I was just going to simply
> give you]
>

Thanks anyway.

My greatest problem is to parse the name part. The others are simple.
My last shot (without success) was:

str_p("Name:") >> space_p >>
(
  lexeme_d[nonspace_r >> *(anychar_p - space_p) >> space_p]
)

Again, any ideas?


-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download
it for free - -and be entered to win a 42" plasma tv or your very own
Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: Parsing question

Josue Andrade Gomes-2
On 9/22/05, Josue Gomes <[hidden email]> wrote:

> str_p("Name:") >> space_p >>
> (
>  lexeme_d[nonspace_r >> *(anychar_p - space_p) >> space_p]
> )
>

oops!

str_p("Name:") >> space_p >>
(
 lexeme_d[!space_p >> *(anychar_p - space_p) >> space_p]
)


-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download
it for free - -and be entered to win a 42" plasma tv or your very own
Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: Parsing question

Joel de Guzman-2
In reply to this post by Josue Andrade Gomes-2
Josue Gomes wrote:

> Hi,
>
> I need to parse the following information:
>
> Name: John Smith Age: 36 cabd23e7cf
>
> and extract:
>
>    "John Smith"
>    "36"
>    "cabd23e7cf"
>
> The last information is a hex encoded signature.
> I've tried many ideas but none of them worked.
> Unfortunately I do not have the option of change the format.

Have you tried the uint_parser parser in hex mode?
(i.e. hex_p)

http://tinyurl.com/922x9

Regards,
--
Joel de Guzman
http://www.boost-consulting.com
http://spirit.sf.net



-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download
it for free - -and be entered to win a 42" plasma tv or your very own
Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: Re: Parsing question

Josue Andrade Gomes-2
On 9/22/05, Joel de Guzman <[hidden email]> wrote:

> Josue Gomes wrote:
> > Hi,
> >
> > I need to parse the following information:
> >
> > Name: John Smith Age: 36 cabd23e7cf
> >
> > and extract:
> >
> >    "John Smith"
> >    "36"
> >    "cabd23e7cf"
> >
> > The last information is a hex encoded signature.
> > I've tried many ideas but none of them worked.
> > Unfortunately I do not have the option of change the format.
>
> Have you tried the uint_parser parser in hex mode?
> (i.e. hex_p)
>

Yes. The signature thing was not the problem. The problem is on how to get the
name.


-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download
it for free - -and be entered to win a 42" plasma tv or your very own
Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Parsing an unsigned char

Vale Group
Is it possible to parse unsigned chars in Spirit?  I have to read a file which
contains, in addition to run-of-the-mill alphanumeric characters and strings,
some single 8-bit bytes in the range of 0x00 to 0xFF.  In my program I need to
use the integer representation of those characters.

To read single-byte characters I now have:
unsigned char tempA;
rule_t oneByte = anychar_p[assign_a(tempA)];
That fixes the problem with the sign bit.

After tempA is loaded with a byte from the file, I put it into the necessary
form with:
unsigned intgrA = tempA;

The Spirit documentation has no index so I loaded all the /doc/*.html files in a
text editor and did a search on "unsigned char."  There are no appearances of
that phrase.

Rules using the predefined uint_parsers such as
rule_t oneByte = uint_p[assign_a(tempA)]; and
rule_t oneByte = hex_p[assign_a(tempA)]; do not help.  Other uint_parser
variations do not help.  If the byte which I read is an ASCII semicolon (0x3B,
59 decimal), for instance, the parse will fail.  The semicolon is not a valid
integer or hexadecimal character.  My roundabout method correctly gives me the
decimal 59.

Changing the type def for char_t from
typedef char char_t;
to
typedef unsigned char char_t; does not help.

Is there a way I can go directly from a byte or bytes in a file to the integer
representation of those bytes without the intermediate conversions?

--
Charles Brockman



-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download
it for free - -and be entered to win a 42" plasma tv or your very own
Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: Parsing question

Joel de Guzman-2
In reply to this post by Josue Andrade Gomes-2
Josue Gomes wrote:

> On 9/22/05, Joel de Guzman <[hidden email]> wrote:
>
>>Josue Gomes wrote:
>>
>>>Hi,
>>>
>>>I need to parse the following information:
>>>
>>>Name: John Smith Age: 36 cabd23e7cf
>>>
>>>and extract:
>>>
>>>   "John Smith"
>>>   "36"
>>>   "cabd23e7cf"
>>>
>>>The last information is a hex encoded signature.
>>>I've tried many ideas but none of them worked.
>>>Unfortunately I do not have the option of change the format.
>>
>>Have you tried the uint_parser parser in hex mode?
>>(i.e. hex_p)
>>
>
>
> Yes. The signature thing was not the problem. The problem is on how to get the
> name.

Sorry, you lost me. Please explain.

Regards,
--
Joel de Guzman
http://www.boost-consulting.com
http://spirit.sf.net



-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download
it for free - -and be entered to win a 42" plasma tv or your very own
Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: Parsing an unsigned char

Joel de Guzman-2
In reply to this post by Vale Group
Vale Group wrote:
> Is it possible to parse unsigned chars in Spirit?  I have to read a file which
> contains, in addition to run-of-the-mill alphanumeric characters and strings,
> some single 8-bit bytes in the range of 0x00 to 0xFF.  In my program I need to
> use the integer representation of those characters.

I'm confused by your post after this first paragraph.
You want to read in a single byte. That could be done
by anychar_p, as you noted. Now what? I don't understand
the rest of your messsage. Maybe an example of an input
you want to parse will clarify.

Cheers,
--
Joel de Guzman
http://www.boost-consulting.com
http://spirit.sf.net



-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download
it for free - -and be entered to win a 42" plasma tv or your very own
Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: Re: Parsing an unsigned char

Vale Group
Joel de Guzman wrote:
> You want to read in a single byte. That could be done
> by anychar_p, as you noted. Now what? I don't understand
> the rest of your messsage. Maybe an example of an input
> you want to parse will clarify.

For example, I have a short test file 15 bytes in length.  It would be reported
by the Spirit debugger as:
rule(test):        ";TEST CASE 123\0"

That first byte, a semicolon, actually represents the decimal integer 59.  The
semicolon is merely the ASCII representation.  Then I continue parsing the rest
of the file.  At the end I have two variables: an int to which the value 59 has
been assigned and a string holding "TEST CASE 123" and a null.

I can easily cascade an action onto the single byte parse which performs the
conversion from anychar to an unsigned char to an int.  Is there a more direct,
preferred method?
--
Chuck Brockman



-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download
it for free - -and be entered to win a 42" plasma tv or your very own
Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: Parsing an unsigned char

Joel de Guzman-2
Vale Group wrote:

> Joel de Guzman wrote:
>
>>You want to read in a single byte. That could be done
>>by anychar_p, as you noted. Now what? I don't understand
>>the rest of your messsage. Maybe an example of an input
>>you want to parse will clarify.
>
>
> For example, I have a short test file 15 bytes in length.  It would be reported
> by the Spirit debugger as:
> rule(test):        ";TEST CASE 123\0"
>
> That first byte, a semicolon, actually represents the decimal integer 59.  The
> semicolon is merely the ASCII representation.  Then I continue parsing the rest
> of the file.  At the end I have two variables: an int to which the value 59 has
> been assigned and a string holding "TEST CASE 123" and a null.
>
> I can easily cascade an action onto the single byte parse which performs the
> conversion from anychar to an unsigned char to an int.  Is there a more direct,
> preferred method?

Wouldn't an action taking in an unsigned char work? Example:

     void f(unsigned char);

...

    anychar_p[&f]

Cheers,
--
Joel de Guzman
http://www.boost-consulting.com
http://spirit.sf.net



-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download
it for free - -and be entered to win a 42" plasma tv or your very own
Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

RE: Re: Parsing an unsigned char

Alex Simpson
In reply to this post by Vale Group
> I can easily cascade an action onto the single byte parse which performs
the conversion
> from anychar to an unsigned char to an int.  Is there a more direct,
preferred method?

You could write a simple functor parser that had int for the result_t.

Alex



-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download
it for free - -and be entered to win a 42" plasma tv or your very own
Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: Re: Parsing question

bill barry
In reply to this post by Joel de Guzman-2

line
    =   "Name:"
    >>  ((lexeme_d[ +alpha_p ] - "Age") | (anychar_p - alpha_p))  [ add_name() ]
    >>  "Age:"
    >>  uint_p                                                    [ add_int_age() ]
    >>  hex_p                                                     [ add_hex_sig() ]
    ;

(this is a phrase level parser)
The only thing that I know the above line won't work correctly for is when the person has the work Age as a word in their name, like this:

Name: Age Wilson Age: 17 ccef53143


sorry, just that it looks like a question someone would write in a book.

Reply | Threaded
Open this post in threaded view
|

Re: Re: Parsing an unsigned char

Vale Group
In reply to this post by Joel de Guzman-2
Joel de Guzman wrote:
> Wouldn't an action taking in an unsigned char work?

Yes:

unsigned intgrA;

void f(unsigned char c) {
intgrA = c;
}

anychar_p[&f]


Then I decided to implement f as a member function and call it with a bind:
rule_t oneByte = anychar_p[boost::bind(&myClass::f, this, _1)];

Works great.  Thank you, Joel and Alex.

--
Chuck Brockman



-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download
it for free - -and be entered to win a 42" plasma tv or your very own
Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general