Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用pycparser在C的struct中解析struct?_Python_C++_C_Parsing_Clang - Fatal编程技术网

Python 使用pycparser在C的struct中解析struct?

Python 使用pycparser在C的struct中解析struct?,python,c++,c,parsing,clang,Python,C++,C,Parsing,Clang,我有一个要解析的示例c文件: StrcutWithinStruct.c // simple struct within a struct example struct A { int a; }; struct B { A a; int b; }; 我正在运行以下代码来解析它 exploreStruct.py #parse StructWithinStruct from pycparser import parse_file ast = parse_file(filename='..

我有一个要解析的示例c文件:

StrcutWithinStruct.c
// simple struct within a struct example

struct A {
 int a;
};

struct B {
 A a;
 int b;
};
我正在运行以下代码来解析它

exploreStruct.py
#parse StructWithinStruct

from pycparser import parse_file
ast = parse_file(filename='..\StructWithinStruct.c')
ast.show()
因此,我得到了以下结果:

Tracback (most recent call last):
  File "exploreStruct.py", line 3, in <module>
   ast = parse_file(filename='...\StructWithinStruct.c')
  File "D:\...\pycparser\__init__.py", line 93, in parse_file
   return parser.parse(text,filename)
  File "D:\...\pycparser\c_parser.py", line 146, in parse
   debug=debug_level)
  File "D:\...\pycparser\yacc.py", line 331, in parse
   return self.parseropt_notrack(input, lexer, debug, tracking, tokenfunc)
  File "D:\...\pycparser\yacc.py", line 1181, in parseropt_notrack
   tok=call_errorfunc(self.errorfunc, errtoken, self)
  File "D:\...\pycparser\yacc.py", line 193, in call_errorfunc
   r=errorfunc(token)
  File "D:\...\pycparser\c_parser.py", line 1699, in p_error
   column=self.clex.find_tok_column(p)))
  File "D:\...\pycparser\plyparser.py", line 55, in _parse_error
   raise ParseError("%s: %s % (coord, msg))
pycparser.plyparser.ParserError: D:...\StructWithinStruct.c:7:2: Before A
Tracback(最近一次通话最后一次):
文件“exploreStruct.py”,第3行,在
ast=parse_文件(文件名='…\structWithInstruction.c')
文件“D:\…\pycparser\ \ uu init\ uuu.py”,第93行,在parse\ u文件中
返回parser.parse(文本、文件名)
文件“D:\…\pycparser\c\u parser.py”,第146行,在parse中
调试=调试级别)
解析中第331行的文件“D:\…\pycparser\yacc.py”
返回self.parseropt\u notrack(输入、lexer、调试、跟踪、tokenfunc)
文件“D:\…\pycparser\yacc.py”,第1181行,位于parseropt\u notrack中
tok=call_errorfunc(self.errorfunc,errtoken,self)
文件“D:\…\pycparser\yacc.py”,第193行,在call\u errorfunc中
r=errorfunc(令牌)
文件“D:\…\pycparser\c\u parser.py”,第1699行,p\U错误
column=self.clex.find_tok_column(p)))
文件“D:\…\pycparser\plyparser.py”,第55行,在\u parse\u错误中
引发解析错误(“%s:%s%(协调,消息))
pycparser.plyparser.ParserError:D:…\structWithInstruction.c:7:2:在
那么,pycparser是否可以在struct中处理struct? 我认为这是一些基本要求,所以我很确定我的配置中存在的问题

<强>另外一件事:我知道PcPaPraseRead,@,说应该这样,但是我想知道现在还有其他选项来解析C++(最好是在Python上),并且用户友好。


谢谢。

您的
struct
声明没有用分号结尾:

< >代码> A<代码>本身不是C++中的C.类型名称>单独> A/COD>就足够了,但是在C中需要添加<代码> Stutt

struct A {
 int a;
};

struct B {
 struct A a;
 int b;
};
或者,您可以声明与
typedef
关键字的同义词:

struct A {
 int a;
};

typedef struct A A;
或简称:

typedef struct A {
 int a;
} A;
从那时起,宣言

A a;

应该正确编译。/P>这里的分号是一个键入。在原始代码中存在。1,在C到C++之间的差异,我猜这就是为什么我问解析C++的原因……我正在用C++工作,但是不知道在你把结构声明为类型名的方式上有什么实际的不同……@ BakItzik在C结构类型中的应用。但是,在C++中,结构> <代码>等价于<代码>类< /代码>,只是默认的初始继承类型<代码>私有< /CUL> >用<代码>公共< /C> >代替。因此,用C++中的<代码>结构> <代码>关键字声明的类型成为G。全局可用类型名的方式与声明为

class
的方式相同。