1

注意:熟悉 pycparser 的人会更好地理解这个问题。

我正在使用pycparser v2.10,我正在尝试提取已在 C 文件中定义的所有函数,并在解析该 C 文件时提取其输入参数名称和标识符类型(使用 pycparser)。

代码示例

import sys
sys.path.extend(['.', '..'])
CPPPATH = '../utils/cpp.exe' if sys.platform == 'win32' else 'cpp'
from pycparser import c_parser, c_ast, parse_file

class FunctionParameter(c_ast.NodeVisitor):

    def visit_FuncDef(self, node):
        #node.decl.type.args.params
        print "Function name is", node.decl.name, "at", node.decl.coord
        print "    It's parameters name  and type is (are)"
        for params in (node.decl.type.args.params):
            print "        ", params.name, params.type


def func_parameter(filename):
    ast = parse_file(filename, use_cpp=True, cpp_path=CPPPATH, cpp_args=r'-I../utils/fake_libc/include')

    vf = FunctionParameter()
    vf.visit(ast)

if __name__ == '__main__':
    if len(sys.argv) > 1:
        filename = sys.argv[1]
    else:
        filename = 'c_files/hash.c'
    func_parameter(filename)

在 visit_FuncDef 中,我正在打印函数名称,然后在 for 循环中,它是参数。

问题是我能够使用for 循环获取传递给函数的输入参数的名称,params.name但无法获取其标识符类型。params.type

有人可以告诉我如何提取参数的标识符吗?

顺便说一句,输出是这样的:

Function name is hash_func at c_files/hash.c:32
    It's parameters name  and type is (are)
         str <pycparser.c_ast.PtrDecl object at 0x00000000024EFC88>
         table_size <pycparser.c_ast.TypeDecl object at 0x00000000024EFEF0>
Function name is HashCreate at c_files/hash.c:44
    It's parameters name  and type is (are)
         hash <pycparser.c_ast.PtrDecl object at 0x00000000024FABE0>
         table_size <pycparser.c_ast.TypeDecl object at 0x00000000024FAE48>
Function name is HashInsert at c_files/hash.c:77
    It's parameters name  and type is (are)
         hash <pycparser.c_ast.PtrDecl object at 0x00000000024F99E8>
         entry <pycparser.c_ast.PtrDecl object at 0x00000000024F9BE0>
Function name is HashFind at c_files/hash.c:100
    It's parameters name  and type is (are)
         hash <pycparser.c_ast.PtrDecl object at 0x00000000028C4160>
         key <pycparser.c_ast.PtrDecl object at 0x00000000028C4358>
Function name is HashRemove at c_files/hash.c:117
    It's parameters name  and type is (are)
         hash <pycparser.c_ast.PtrDecl object at 0x00000000028C5780>
         key <pycparser.c_ast.PtrDecl object at 0x00000000028C5978>
Function name is HashPrint at c_files/hash.c:149
    It's parameters name  and type is (are)
         hash <pycparser.c_ast.PtrDecl object at 0x00000000028E9438>
         PrintFunc <pycparser.c_ast.PtrDecl object at 0x00000000028E9668>
Function name is HashDestroy at c_files/hash.c:170
    It's parameters name  and type is (are)
         hash <pycparser.c_ast.PtrDecl object at 0x00000000028EF240>

如您所见,我没有获取标识符类型,而是获取每一行中的对象类型。例如<pycparser.c_ast.PtrDecl object at 0x00000000024EFC88>

我用作测试文件的示例 hash.c 文件(无论如何它都在 pycparser 中):

/*
** C implementation of a hash table ADT
*/
typedef enum tagReturnCode {SUCCESS, FAIL} ReturnCode;


typedef struct tagEntry
{
    char* key;
    char* value;
} Entry;



typedef struct tagNode
{
    Entry* entry;

    struct tagNode* next;
} Node;


typedef struct tagHash
{
    unsigned int table_size;

    Node** heads; 

} Hash;


static unsigned int hash_func(const char* str, unsigned int table_size)
{
    unsigned int hash_value;
    unsigned int a = 127;

    for (hash_value = 0; *str != 0; ++str)
        hash_value = (a*hash_value + *str) % table_size;

    return hash_value;
}


ReturnCode HashCreate(Hash** hash, unsigned int table_size)
{
    unsigned int i;

    if (table_size < 1)
        return FAIL;

    //
    // Allocate space for the Hash
    //
    if (((*hash) = malloc(sizeof(**hash))) == NULL)
        return FAIL;

    //
    // Allocate space for the array of list heads
    //
    if (((*hash)->heads = malloc(table_size*sizeof(*((*hash)->heads)))) == NULL)
        return FAIL;

    //
    // Initialize Hash info
    //
    for (i = 0; i < table_size; ++i)
    {
        (*hash)->heads[i] = NULL;
    }

    (*hash)->table_size = table_size;

    return SUCCESS;
}


ReturnCode HashInsert(Hash* hash, const Entry* entry)
{
    unsigned int index = hash_func(entry->key, hash->table_size);
    Node* temp = hash->heads[index];

    HashRemove(hash, entry->key);

    if ((hash->heads[index] = malloc(sizeof(Node))) == NULL)
        return FAIL;

    hash->heads[index]->entry = malloc(sizeof(Entry));
    hash->heads[index]->entry->key = malloc(strlen(entry->key)+1);
    hash->heads[index]->entry->value = malloc(strlen(entry->value)+1);
    strcpy(hash->heads[index]->entry->key, entry->key);
    strcpy(hash->heads[index]->entry->value, entry->value);

    hash->heads[index]->next = temp;

    return SUCCESS;
}



const Entry* HashFind(const Hash* hash, const char* key)
{
    unsigned int index = hash_func(key, hash->table_size);
    Node* temp = hash->heads[index];

    while (temp != NULL)
    {
        if (!strcmp(key, temp->entry->key))
            return temp->entry;

        temp = temp->next;
    }

    return NULL;
}


ReturnCode HashRemove(Hash* hash, const char* key)
{
    unsigned int index = hash_func(key, hash->table_size);
    Node* temp1 = hash->heads[index];
    Node* temp2 = temp1;

    while (temp1 != NULL)
    {
        if (!strcmp(key, temp1->entry->key))
        {
            if (temp1 == hash->heads[index])
                hash->heads[index] = hash->heads[index]->next;
            else
                temp2->next = temp1->next;

            free(temp1->entry->key);
            free(temp1->entry->value);
            free(temp1->entry);
            free(temp1);
            temp1 = NULL;

            return SUCCESS;
        }

        temp2 = temp1;
        temp1 = temp1->next;
    }

    return FAIL;
}


void HashPrint(Hash* hash, void (*PrintFunc)(char*, char*))
{
    unsigned int i;

    if (hash == NULL || hash->heads == NULL)
        return;

    for (i = 0; i < hash->table_size; ++i)
    {
        Node* temp = hash->heads[i];

        while (temp != NULL)
        {
            PrintFunc(temp->entry->key, temp->entry->value);
            temp = temp->next;
        }
    }
}



void HashDestroy(Hash* hash)
{
    unsigned int i;

    if (hash == NULL)
        return;

    for (i = 0; i < hash->table_size; ++i)
    {
        Node* temp = hash->heads[i];

        while (temp != NULL)
        {
            Node* temp2 = temp;

            free(temp->entry->key);
            free(temp->entry->value);
            free(temp->entry);

            temp = temp->next;

            free(temp2);
        }
    }    

    free(hash->heads);
    hash->heads = NULL;

    free(hash);
}
4

2 回答 2

3

是什么让您认为您没有提取类型?

Function name is HashCreate at c_files/hash.c:44
    It's parameters name  and type is (are)
         hash <pycparser.c_ast.PtrDecl object at 0x00000000024FABE0>
         table_size <pycparser.c_ast.TypeDecl object at 0x00000000024FAE48>

名字是table_size,类型是 in TypeDecl。没有提供简单的类型名称 - 您必须重新构建它们。有关如何将“decl”解开为其文本表示的示例,请参阅cdecl 示例

于 2014-02-19T05:47:26.120 回答
0

要获得 AST 中标识符的确切类型——就像电影《盗梦空间》中的 Leo——“需要更深入”。8]

这是您的 visit_FuncDef 函数的扩展,用于演示如何从给定点到达 AST 的不同项目:

def visit_FuncDef(self, node):
    #node.decl.type.args.params
    print "Function name is", node.decl.name, "at", node.decl.coord
    print "    It's parameters name  and type is (are)"
    for params in (node.decl.type.args.params): ###FuncDef/Decl/FuncDecl/ParamList
        # Assign parameter name
        pname = params.name ###ParamList/Decl

        # Parameter is a pointer type of some kind
        if type(params.type) is c_ast.PtrDecl:
            # Parameter is a pointer to a pointer type - double indirection
            if type(params.type.type) is c_ast.PtrDecl:
                ptype = params.type.type.type.type.names ###Decl/PtrDecl/PtrDecl/TypeDecl/IdentifierType
            # There is no double indirection
            else:
                # Parameter is a pointer to a function type
                if type(params.type.type) is c_ast.FuncDecl:
                    pname = str(params.type.type.type.type.names) + ' (*' ###Decl/PtrDecl/TypeDecl/IdentifierType
                    pname = pname + params.type.type.type.declname + ')' ###Decl/PtrDecl/FuncDecl/TypeDecl
                    ptype = ''
                    for subparams in params.type.type.args.params: ###Decl/PtrDecl/FuncDecl/ParamList
                        ptype = ptype + str(subparams.type.type.type.names) ###Typename/PtrDecl/TypeDecl/IdentifierType
                # Parameter is a pointer type - single indirection
                else:
                    ptype = params.type.type.type.names ###Decl/PtrDecl/TypeDecl/IdentifierType

        # Parameter is a variable
        elif type(params.type.type) is c_ast.IdentifierType:
            ptype = params.type.type.names

        print "        ", pname, ptype

通过评论,我试图解释代码正在寻找哪种类型的参数。我用三重井号标记了 AST 树中的实际位置。

作为一个例子,这里是函数 HashPrint() 的 AST 树的一部分,它包含一个指向函数的指针作为参数:

  FuncDef: 
    Decl: HashPrint, [], [], []
      FuncDecl: 
        ParamList: 
          Decl: hash, [], [], []
            PtrDecl: []
              TypeDecl: hash, []
                IdentifierType: ['Hash']
          Decl: PrintFunc, [], [], []
            PtrDecl: []
              FuncDecl: 
                ParamList: 
                  Typename: None, []
                    PtrDecl: []
                      TypeDecl: None, []
                        IdentifierType: ['char']
                  Typename: None, []
                    PtrDecl: []
                      TypeDecl: None, []
                        IdentifierType: ['char']
                TypeDecl: PrintFunc, []
                  IdentifierType: ['void']
        TypeDecl: HashPrint, []
          IdentifierType: ['void']
    Compound: 

最后是函数的输出:

Function name is hash_func at c_files/hash.c:32
    It's parameters name  and type is (are)
         str ['char']
         table_size ['unsigned', 'int']
Function name is HashCreate at c_files/hash.c:44
    It's parameters name  and type is (are)
         hash ['Hash']
         table_size ['unsigned', 'int']
Function name is HashInsert at c_files/hash.c:77
    It's parameters name  and type is (are)
         hash ['Hash']
         entry ['Entry']
Function name is HashFind at c_files/hash.c:100
    It's parameters name  and type is (are)
         hash ['Hash']
         key ['char']
Function name is HashRemove at c_files/hash.c:117
    It's parameters name  and type is (are)
         hash ['Hash']
         key ['char']
Function name is HashPrint at c_files/hash.c:149
    It's parameters name  and type is (are)
         hash ['Hash']
         ['void'] (*PrintFunc) ['char']['char']
Function name is HashDestroy at c_files/hash.c:170
    It's parameters name  and type is (are)
         hash ['Hash']

这特别适用于示例文件 hash.c。我只是想让您深入了解如何从一个点访问 AST 的特定部分。

最佳做法是将 AST 保存到文件中:

file = open('ast.txt', 'w')
ast.show(buf=file)
file.close()

然后将 AST 与 _c_ast.cfg 进行比较,以查看每个节点具有什么样的属性,以便您可以在树中“更深入”。

于 2016-03-07T14:40:16.463 回答