如何使用的libclang Python绑定跳过遍历AST的子树

Python014

如何使用的libclang Python绑定跳过遍历AST的子树,第1张

的只是特定的节点。 它只是服务的宗旨,如何让刚特定节点 example.cpp文件:int i

char var[10]

double tmp

int add (int a, int b)

{

int r

r=a+b

return (r)

}

例如Python代码:import sys

from clang.cindex import *

index = Index.create()

tu = index.parse('example.cpp')

root_node = tu.cursor

#for further working with children nodes i tend to save them in a seperate list

#wanted nodes in extra list "result"

wanted_nodes = ['var', 'tmp']

result = []

node_list= []

for i in node.get_children():

node_list.append(i)

for i in node_list:

if i.spelling in wanted_nodes:

result.append(i)

#now result contains the two nodes "var" and "add"

#print the name

for i in result:

print i.spelling

#print the type

for i in result:

print i.type.kind

######OUTPUT#######

>>>var

>>>add

>>>TypeKind.CONSTANTARRAY

>>>TypeKind.DOUBLE

如果您希望每个数组的另外的U型得到它通过:result[1].type.element_type.kind

#######OUTPUT######

>>>TypeKind.CHAR_S

因为MODUL cindex.py是那么它不应该是很难找到如何获取你需要的。

2.

我不认为一个get_next_sibling在Python API,AST中的每个节点都知道所有的子,所以,跳绳无趣的子树可以很容易地通过简单地跳过他们在遍历父的子来完成。从礼Bendersky的有关libclang的Python API的优秀的博客文章的例子:

def find_typerefs(node, typename):

""" Find all references to the type named 'typename'

"""

if node.kind.is_reference():

ref_node = clang.cindex.Cursor_ref(node)

if ref_node.spelling == typename:

print 'Found %s [line=%s, col=%s]' % (

typename, node.location.line, node.location.column)

# Recurse for children of this node,

# skipping all nodes not beginning with "a"

for c in node.get_children():

if c.spelling.startswith ("a"):

find_typerefs(c, typename)

3.

在铛-C而言,enumsCXChildVisitResult有3个值 CodeGo.net,CXChildVisit_Continue跳过探望,所以游客要下一个兄弟。这样应该是在python了。

1、用pip安装第三方库时总是提示:You are using pip version 8.0.2, however version 8.1.0 is available.

2、pip install --upgrade pip也没用

3、就算安装好了第三方库 比如说pliiow安装了 也没有用 from PIL import。。。时也会提示没有这个model

求解决方法

pip install Pillow

就行

You are using pip version 8.0.2, however version 8.1.0 is available.

这个说你的pip不是最新版本,没有关系。

Python 2.7.10 (default, Oct 23 2015, 18:05:06)

[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

>>>from PIL import Image

>>>

上一篇: Termux02-安装Python

在termux上安装lxml经常失败,显示 #include <libxml/xmlversion.h>、#include <iconv.h>错误等。

主要原因是安装过程中需要从C语言编译lxml,因而需要相关库的文件。

经实验,通过如下步骤可以成功安装

1、首先安装Clang、python-dev等

2、然后安装相关库文件

3、这时可以尝试安装lxml

4、如果还是出现错误,可以尝试安装Cython

然后再进行第3步。

5、经实际测试,通过以上几个步骤可以成功安装lxml