How to parse a simple C struct?

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

How to parse a simple C struct?

PretzelX
Hi,

I'd like to know how to use spirit to parse some simple C code
- initialization of  simple struct.  

Say for example, the file looks like:

/**************BEGIN*******************/
#include "mystruct.h"
#define HDR "Header"
#define FTR "Footer"

MyStruct myStruct =
{
        HDR,
        {1, 2, 3, 'A'},
        {
                "Detail1", // how to ignore this comment when parsing?
                "Detail2",
                "Detail3",
                {99, 2222, 11, 'F'},
                {0, 1, 0, 'A'},
                {33, 0000, 44, 'X'},
        },
        FTR
};
/*****************END*******************/


I'd like to parse this file and extract the fields so that I can generate an XML
file containing the relevant information from the initalization of this
structure.

Say for example I want to parse and extract data into the following:
int a1;        // 1 from {1, 2, 3, 'A'},
int a2;        // 2 from {1, 2, 3, 'A'},
int a3;        // 3 from {1, 2, 3, 'A'},
char a4;       // A from {1, 2, 3, 'A'},
char b1[100];  // "Detail1"
char b2[100];  // "Detail2"

I must admit I'm a little overwhelmed by all the examples and think that I
should be able to get away with something simple since the file contains a known
format/structure (I'm completely new to Spirit).  The only variations would be
whitespace and possible some comments (as is seen ont the line with "Detail1".

I was originally going to use fscanf, but would really like to learn how to use
Spirit, to properly parse.  

Thanks very much for your help.
PretzelX.





-------------------------------------------------------
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: How to parse a simple C struct?

Carl Barron

On Sep 15, 2005, at 11:46 PM, PretzelX wrote:

> Hi,
>
> I'd like to know how to use spirit to parse some simple C code
> - initialization of  simple struct.
>
> Say for example, the file looks like:
>
> /**************BEGIN*******************/
> #include "mystruct.h"
> #define HDR "Header"
> #define FTR "Footer"
>
> MyStruct myStruct =
> {
> HDR,
> {1, 2, 3, 'A'},
> {
> "Detail1", // how to ignore this comment when parsing?
> "Detail2",
> "Detail3",
> {99, 2222, 11, 'F'},
> {0, 1, 0, 'A'},
> {33, 0000, 44, 'X'},
> },
> FTR
> };
> /*****************END*******************/
>
>
> I'd like to parse this file and extract the fields so that I can
> generate an XML
> file containing the relevant information from the initalization of this
> structure.
>
> Say for example I want to parse and extract data into the following:
> int a1;        // 1 from {1, 2, 3, 'A'},
> int a2;        // 2 from {1, 2, 3, 'A'},
> int a3;        // 3 from {1, 2, 3, 'A'},
> char a4;       // A from {1, 2, 3, 'A'},
> char b1[100];  // "Detail1"
> char b2[100];  // "Detail2"
>
> I must admit I'm a little overwhelmed by all the examples and think
> that I
> should be able to get away with something simple since the file
> contains a known
> format/structure (I'm completely new to Spirit).  The only variations
> would be
> whitespace and possible some comments (as is seen ont the line with
> "Detail1".
>
> I was originally going to use fscanf, but would really like to learn
> how to use
> Spirit, to properly parse.
>
    if you want to ignore eveything from "//" to end of tine  from the
data your
parser sees then a skipper like  comment_p("//") | space_p will ignore
C++ style comments and white space from data seen by your parser.

if you need a parser to generate the results [after comment removal] I
would need
a more complete description of what this is parsing.
   Write it first in ebnf then conversion to spirit is probably simple.




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