我正在尝试使用pycparser解析c文件,并找到我使用https://github.com/eliben/pycparser/blob/master/examples/explore_ast.py this link生成的最后一个开关语句。然后使用n= len(ast.ext),我已经找到了从ast生成的exts的长度。现在,我必须从最后一次尝试执行的if re.findall(r'( switch (\s*'),ast.ext) )中找到switch语句,并匹配正则表达式以查找switch case,但它没有发生。由于我是pycparser的新手,对此一无所知,该如何操作呢?
发布于 2021-02-16 02:39:45
你不能在pycparser ASTs上运行regexp匹配!
pycparser存储库中有多个示例应该会对您有所帮助:explore_ast.py,您已经看到它允许您使用它并探索它的节点。
dump_ast.py展示了如何转储整个AST,并查看您的代码有哪些节点。
最后,func_calls.py演示了如何遍历AST以查找特定类型的节点:
class FuncCallVisitor(c_ast.NodeVisitor):
def __init__(self, funcname):
self.funcname = funcname
def visit_FuncCall(self, node):
if node.name.name == self.funcname:
print('%s called at %s' % (self.funcname, node.name.coord))
# Visit args in case they contain more func calls.
if node.args:
self.visit(node.args)在本例中是FuncCall节点,但您需要切换节点,因此您将创建一个名为visit_Switch的方法,访问者将找到所有Switch节点。
https://stackoverflow.com/questions/66204748
复制相似问题