简体   繁体   中英

Python C Header File Parsing and Reverse Initialization

I am interested in parsing C header files (only structures and variable declarations) using Python in a recursive manner.

Here is an example of what I am looking for. Suppose the following:

typedef struct
{
   double value[3];
} vector3;

typedef struct
{
       unsigned int variable_a[4][2];
       vector3 variable_b[5];
} my_example;

Also, suppose there is a file that contains initialization values such as:

ANCHOR_STRUCT(my_example) = 
{
    // variable_a
    { {1,2}, {3, 4}, {5,6} ,{7,8}   },

    // variable_b
    { {1.0,2.0,3.0}, {4.0,5.0,6.0}, {7.0,8.0,9.0}, {10.0,11.0,12.0}, {13.0,14.0,15.0}  }
}

I would like to be able to parse both of these files and be able to generate a report such as:

OUTPUT:
my_example.variable_a[0][0]   = 1
my_example.variable_a[0][1]   = 2
my_example.variable_a[1][0]   = 3
my_example.variable_a[1][1]   = 4
my_example.variable_a[2][0]   = 5
my_example.variable_a[2][1]   = 6
my_example.variable_a[3][0]   = 7
my_example.variable_a[3][1]   = 8

my_example.variable_b[0].value[0] = 1
my_example.variable_b[0].value[1] = 2
my_example.variable_b[0].value[2] = 3
my_example.variable_b[1].value[0] = 4
my_example.variable_b[1].value[1] = 5
my_example.variable_b[1].value[2] = 6
my_example.variable_b[2].value[0] = 7
my_example.variable_b[2].value[1] = 8
my_example.variable_b[2].value[2] = 9
my_example.variable_b[3].value[0] = 10
my_example.variable_b[3].value[1] = 11
my_example.variable_b[3].value[2] = 12
my_example.variable_b[4].value[0] = 13
my_example.variable_b[4].value[1] = 14
my_example.variable_b[4].value[2] = 15

I would like to be able to report this without running the code (only through parsing). Is there a Python tool that exist that would parses and prints this information. I'd also like to print out the data type.

It seems it is a bit complicated to parse the "{" and "," and "}" in the intiailization file and be able to match this with the structure's variables and children. Matching the values with the correct code name seems difficult because the order is very important. I also assume recursion is needed for parent/children/grandchildren variables.

Thanks, Ned

Unless you restrict yourself to simple data types, this is going to get very complicated. For example, do you want to handle arbitrary data types such as nested classes?

You say you don't want to run the c-sources, but what you are trying to do here is build your own c-interpreter! Are you sure you want to reinvent the wheel? If yes...

The first thing you need to be able to do, is parse the file. You can can use a parser+lexicographic analyzer such as PLY . Once you have the parse tree, you can analyze what your variables are and what their intended values are.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM