简体   繁体   English

Python C头文件解析和反向初始化

[英]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. 我有兴趣以递归方式使用Python解析C头文件(仅结构和变量声明)。

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. 是否存在可以解析和打印此信息的Python工具。 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. 在intiailization文件中解析“{”和“,”和“}”并且能够将其与结构的变量和子项匹配似乎有点复杂。 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! 你说你不想运行c-sources,但是你要做的就是建立自己的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 . 您可以使用解析器+词典分析器,如PLY Once you have the parse tree, you can analyze what your variables are and what their intended values are. 获得解析树后,您可以分析变量是什么以及它们的预期值是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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