[英]Parsing struct within struct in C using pycparser?
I have this example c file I want to parse: 我有要解析的示例c文件:
StrcutWithinStruct.c
// simple struct within a struct example
struct A {
int a;
};
struct B {
A a;
int b;
};
I'm running pcyparser to parse it, with the following code 我正在运行pcyparser来解析它,并带有以下代码
exploreStruct.py
#parse StructWithinStruct
from pycparser import parse_file
ast = parse_file(filename='..\StructWithinStruct.c')
ast.show()
As a result, I got the following: 结果,我得到以下信息:
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
So, is pycparser can handle struct within struct, or not? 那么,pycparser是否可以在struct中处理struct? I thought this is some basic requirement, so I'm pretty sure that the problem lying in my configuration somewhere... 我认为这是一些基本要求,所以我很确定问题出在我的配置中……
One more thing: I know that pcypareser author, @ Eli Bendersky , says that one should use Clang to parse C++ , but I will like to know if there's another option nowadays to parse C++ (preferably over Python), and is user-friendly. 还有一件事:我知道pcypareser作者@ Eli Bendersky表示应该使用Clang解析C ++ ,但是我想知道当今是否还有另一种解析C ++的选项(最好是通过Python解析),并且对用户友好。
Thanks. 谢谢。
Your struct
declarations are not closed with a semicolon: 您的struct
声明不会以分号关闭:
Additionally A
itself is not a type name in C. In C++ A
alone would suffice, but in C you need to add the struct
keyword. 另外, A
本身不是C中的类型名称。在C ++中A
仅A
就足够了,但是在C中,您需要添加struct
关键字。
struct A {
int a;
};
struct B {
struct A a;
int b;
};
Or, you can declare a synonym with a typedef
keyword: 或者,您可以使用typedef
关键字声明同义词:
struct A {
int a;
};
typedef struct A A;
or, shorter: 或更短:
typedef struct A {
int a;
} A;
From that point the declaration 从那时起,宣言
A a;
should compile properly. 应该正确编译。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.