如何解析语法正确的C文件(包含单个函数,但具有未定义的类型)?文件自动缩进(4个空格),使用this service在每个块关键字下面加括号,例如
if ( condition1 )
{
func1( int hi );
unktype foo;
do
{
if ( condition2 )
goto LABEL_1;
}
while ( condition3 );
}
else
{
float a = bar(baz, 0);
LABEL_1:
int foobar = (int)a;
}
第一行是原型,第二行是“{”。所有行都以\n结尾。最后一行只是“}\n” 有很多多对一的goto,而且标签经常超出他们的范围(糟糕,我知道:D) 我只关心结构信息,即块和语句类型。下面是我想要的(打印时,为了清晰起见,添加了缩进):
[If(condition = [condition1],
bodytrue = ["func1( int hi );",
"unktype foo;"
DoWhile(condition = [condition3],
body = [
SingleLineIf(condition = [condition2],
bodytrue =["goto LABEL_1;"],
bodyelse = []
)
]
)
]
bodyelse = ["float a = bar(baz, 0);",
"int foobar = (int)a;"
]
)]
使用条件1、条件2和条件3字符串。其他的构造也一样。
标签可以丢弃。我还需要包括与任何特殊语句无关的块,比如Block([...]).
由于未知类型,标准C语言Python解析器无法工作(例如pycparser给出语法错误)
Pyparsing包括a simple C parser as part of its examples,这里是一个解析器,它将处理您的示例代码,还有更多(包括对
for
语句的支持)。你知道吗这不是一个很好的C解析器。它将if、while和do条件作为嵌套括号中的字符串进行广泛的遍历。但它可能会让你开始提取你感兴趣的东西。你知道吗
印刷品:
相关问题 更多 >
编程相关推荐