EzDevInfo.com

pycparser

Complete C99 parser in pure Python

Can't run pycparser: Needs ply.yacc?

I downloaded pycparser and ran python setup.py install, but whenever I try to run anything, I get:

...
    from .c_parser import CParser
  File "C:\Program Files\Python 3.2\lib\site-packages\pycparser\c_parser.py", line 11, in <module>
    import ply.yacc
ImportError: No module named ply.yacc

What's wrong? I'm pretty sure I followed the Readme exactly...


Source: (StackOverflow)

Extracting input parameters and its Identifier type While parsing a c file using PycParser

Note: Ones who are familiar with pycparser would understand the problem much better.

I am using pycparser v2.10 and I am trying to extract all the functions that have been defined in the C file as well extract its input parameter name and identifier type While parsing that C file (Using pycparser).

Code sample

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)

Here in the visit_FuncDef I am printing the Function name and then in the for loop, it's parameters.

The problem is that I am able to get the name of the input parameter passed to the function using the params.name but not able to get its Identifier type using params.type in the for loop.

Can somebody tell me how can I extract the identifier of the parameter?

Btw, the output is like this:

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>

Here as you can see, Instead of getting the Identifier type, I am getting the object type in each line. e.g <pycparser.c_ast.PtrDecl object at 0x00000000024EFC88>

sample hash.c file which i am using as a test file (Anyways it's all there in 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);
}

Source: (StackOverflow)

Advertisements

how does pycparser reads the header files listed in includes in C code files?

I am trying to parse a C file using pycparser. I am curious to know that while pre-processing the C file does the pycparser reads only those library files which are provided in fake lib folder(if you provide the path of fake lib in cpp_args) or it also reads from the location mentioned in include statements, such as-
in line below

#include<folder1/folder2/xyz.h>  

where will the pycparser search for xyz.h will it only be in FAKE LIB folder?


Source: (StackOverflow)

Is there a way to add a keyword to pycparser?

Does anybody know if there is a way to add a new keyword to pycparser? I need to parse C code for the compiler which is based on C99 but is slightly different. There are a few keywords that are not part of C99.

Any help is appreciated


Source: (StackOverflow)

Cannot install cryptography 0.9.3 on OSX 10.8

I'm new to python and django. I'm struggling to install our company's development package locally since the latest release of django. Here's the message stack:

Installing django.
django: There's no directory named after our project. Probably you want to run 'bin/django startproject project'
Getting distribution for 'cryptography>=0.7'.

Installed /private/var/folders/hf/y3d63zk55893w65jr6dxpk5m0000gn/T/easy_install-bi2ZfW/cryptography-0.9.3/.eggs/cffi-1.1.2-py2.7-macosx-10.8-x86_64.egg
Searching for ipaddress
Reading https://pypi.python.org/simple/ipaddress/
Best match: ipaddress 1.0.14
Downloading https://pypi.python.org/packages/source/i/ipaddress/ipaddress-1.0.14.tar.gz#md5=e2f2f6593b2b8a7e8abba0fbdf33f046
Processing ipaddress-1.0.14.tar.gz
Writing /var/folders/hf/y3d63zk55893w65jr6dxpk5m0000gn/T/easy_install-bi2ZfW/cryptography-0.9.3/temp/easy_install-Q3JRP9/ipaddress-1.0.14/setup.cfg
Running ipaddress-1.0.14/setup.py -q bdist_egg --dist-dir /var/folders/hf/y3d63zk55893w65jr6dxpk5m0000gn/T/easy_install-bi2ZfW/cryptography-0.9.3/temp/easy_install-Q3JRP9/ipaddress-1.0.14/egg-dist-tmp-QxA7aQ
zip_safe flag not set; analyzing archive contents...
Copying ipaddress-1.0.14-py2.7.egg to /private/var/folders/hf/y3d63zk55893w65jr6dxpk5m0000gn/T/easy_install-bi2ZfW/cryptography-0.9.3/.eggs

Installed /private/var/folders/hf/y3d63zk55893w65jr6dxpk5m0000gn/T/easy_install-bi2ZfW/cryptography-0.9.3/.eggs/ipaddress-1.0.14-py2.7.egg
Searching for enum34
Reading https://pypi.python.org/simple/enum34/
Best match: enum34 1.0.4
Downloading https://pypi.python.org/packages/source/e/enum34/enum34-1.0.4.zip#md5=9843e97527f3126c851df7afeb0524b3
Processing enum34-1.0.4.zip
Writing /var/folders/hf/y3d63zk55893w65jr6dxpk5m0000gn/T/easy_install-bi2ZfW/cryptography-0.9.3/temp/easy_install-GiulwR/enum34-1.0.4/setup.cfg
Running enum34-1.0.4/setup.py -q bdist_egg --dist-dir /var/folders/hf/y3d63zk55893w65jr6dxpk5m0000gn/T/easy_install-bi2ZfW/cryptography-0.9.3/temp/easy_install-GiulwR/enum34-1.0.4/egg-dist-tmp-NSgjuy
zip_safe flag not set; analyzing archive contents...
Copying enum34-1.0.4-py2.7.egg to /private/var/folders/hf/y3d63zk55893w65jr6dxpk5m0000gn/T/easy_install-bi2ZfW/cryptography-0.9.3/.eggs

Installed /private/var/folders/hf/y3d63zk55893w65jr6dxpk5m0000gn/T/easy_install-bi2ZfW/cryptography-0.9.3/.eggs/enum34-1.0.4-py2.7.egg
Searching for six>=1.4.1
Reading https://pypi.python.org/simple/six/
Best match: six 1.9.0
Downloading https://pypi.python.org/packages/source/s/six/six-1.9.0.tar.gz#md5=476881ef4012262dfc8adc645ee786c4
Processing six-1.9.0.tar.gz
Writing /var/folders/hf/y3d63zk55893w65jr6dxpk5m0000gn/T/easy_install-bi2ZfW/cryptography-0.9.3/temp/easy_install-xWVhoY/six-1.9.0/setup.cfg
Running six-1.9.0/setup.py -q bdist_egg --dist-dir /var/folders/hf/y3d63zk55893w65jr6dxpk5m0000gn/T/easy_install-bi2ZfW/cryptography-0.9.3/temp/easy_install-xWVhoY/six-1.9.0/egg-dist-tmp-WO8SGv
no previously-included directories found matching 'documentation/_build'
zip_safe flag not set; analyzing archive contents...
six: module references __path__
creating /private/var/folders/hf/y3d63zk55893w65jr6dxpk5m0000gn/T/easy_install-bi2ZfW/cryptography-0.9.3/.eggs/six-1.9.0-py2.7.egg
Extracting six-1.9.0-py2.7.egg to /private/var/folders/hf/y3d63zk55893w65jr6dxpk5m0000gn/T/easy_install-bi2ZfW/cryptography-0.9.3/.eggs

Installed /private/var/folders/hf/y3d63zk55893w65jr6dxpk5m0000gn/T/easy_install-bi2ZfW/cryptography-0.9.3/.eggs/six-1.9.0-py2.7.egg
Searching for pyasn1
Reading https://pypi.python.org/simple/pyasn1/
Best match: pyasn1 0.1.8
Downloading https://pypi.python.org/packages/2.7/p/pyasn1/pyasn1-0.1.8-py2.7.egg#md5=59f23a2692b9b4bc7901d166eba69167
Processing pyasn1-0.1.8-py2.7.egg
Moving pyasn1-0.1.8-py2.7.egg to /private/var/folders/hf/y3d63zk55893w65jr6dxpk5m0000gn/T/easy_install-bi2ZfW/cryptography-0.9.3/.eggs

Installed /private/var/folders/hf/y3d63zk55893w65jr6dxpk5m0000gn/T/easy_install-bi2ZfW/cryptography-0.9.3/.eggs/pyasn1-0.1.8-py2.7.egg
Searching for idna
Reading https://pypi.python.org/simple/idna/
Best match: idna 2.0
Downloading https://pypi.python.org/packages/source/i/idna/idna-2.0.tar.gz#md5=bd17a9d15e755375f48a62c13b25b801
Processing idna-2.0.tar.gz
Writing /var/folders/hf/y3d63zk55893w65jr6dxpk5m0000gn/T/easy_install-bi2ZfW/cryptography-0.9.3/temp/easy_install-ne4LbF/idna-2.0/setup.cfg
Running idna-2.0/setup.py -q bdist_egg --dist-dir /var/folders/hf/y3d63zk55893w65jr6dxpk5m0000gn/T/easy_install-bi2ZfW/cryptography-0.9.3/temp/easy_install-ne4LbF/idna-2.0/egg-dist-tmp-8YLfu7
warning: no previously-included files matching '*.pyc' found under directory 'tools'
warning: no previously-included files matching '*.pyc' found under directory 'tests'
zip_safe flag not set; analyzing archive contents...
Copying idna-2.0-py2.7.egg to /private/var/folders/hf/y3d63zk55893w65jr6dxpk5m0000gn/T/easy_install-bi2ZfW/cryptography-0.9.3/.eggs

Installed /private/var/folders/hf/y3d63zk55893w65jr6dxpk5m0000gn/T/easy_install-bi2ZfW/cryptography-0.9.3/.eggs/idna-2.0-py2.7.egg
Searching for pycparser
Reading https://pypi.python.org/simple/pycparser/
Best match: pycparser 2.14
Downloading https://pypi.python.org/packages/source/p/pycparser/pycparser-2.14.tar.gz#md5=a2bc8d28c923b4fe2b2c3b4b51a4f935
Processing pycparser-2.14.tar.gz
Writing /var/folders/hf/y3d63zk55893w65jr6dxpk5m0000gn/T/easy_install-bi2ZfW/cryptography-0.9.3/temp/easy_install-M45io9/pycparser-2.14/setup.cfg
Running pycparser-2.14/setup.py -q bdist_egg --dist-dir /var/folders/hf/y3d63zk55893w65jr6dxpk5m0000gn/T/easy_install-bi2ZfW/cryptography-0.9.3/temp/easy_install-M45io9/pycparser-2.14/egg-dist-tmp-TZWoS1
warning: no previously-included files matching 'yacctab.*' found under directory 'tests'
warning: no previously-included files matching 'lextab.*' found under directory 'tests'
warning: no previously-included files matching 'yacctab.*' found under directory 'examples'
warning: no previously-included files matching 'lextab.*' found under directory 'examples'
zip_safe flag not set; analyzing archive contents...
Copying pycparser-2.14-py2.7.egg to /private/var/folders/hf/y3d63zk55893w65jr6dxpk5m0000gn/T/easy_install-bi2ZfW/cryptography-0.9.3/.eggs

Installed /private/var/folders/hf/y3d63zk55893w65jr6dxpk5m0000gn/T/easy_install-bi2ZfW/cryptography-0.9.3/.eggs/pycparser-2.14-py2.7.egg
no previously-included directories found matching 'docs/_build'
warning: no previously-included files matching '*' found under directory 'vectors'
Undefined symbols for architecture x86_64:
  "_EC_GFp_nistp224_method", referenced from:
      __cffi_f_EC_GFp_nistp224_method in _Cryptography_cffi_a269d620xd5c405b7.o
  "_EC_GFp_nistp256_method", referenced from:
      __cffi_f_EC_GFp_nistp256_method in _Cryptography_cffi_a269d620xd5c405b7.o
  "_EC_GFp_nistp521_method", referenced from:
      __cffi_f_EC_GFp_nistp521_method in _Cryptography_cffi_a269d620xd5c405b7.o
  "_EC_curve_nid2nist", referenced from:
      __cffi_f_EC_curve_nid2nist in _Cryptography_cffi_a269d620xd5c405b7.o
  "_SSL_CTX_set_alpn_protos", referenced from:
      __cffi_f_SSL_CTX_set_alpn_protos in _Cryptography_cffi_a269d620xd5c405b7.o
  "_SSL_CTX_set_alpn_select_cb", referenced from:
      __cffi_f_SSL_CTX_set_alpn_select_cb in _Cryptography_cffi_a269d620xd5c405b7.o
  "_SSL_get0_alpn_selected", referenced from:
      __cffi_f_SSL_get0_alpn_selected in _Cryptography_cffi_a269d620xd5c405b7.o
  "_SSL_set_alpn_protos", referenced from:
      __cffi_f_SSL_set_alpn_protos in _Cryptography_cffi_a269d620xd5c405b7.o
  "_X509_VERIFY_PARAM_set1_email", referenced from:
      __cffi_f_X509_VERIFY_PARAM_set1_email in _Cryptography_cffi_a269d620xd5c405b7.o
  "_X509_VERIFY_PARAM_set1_host", referenced from:
      __cffi_f_X509_VERIFY_PARAM_set1_host in _Cryptography_cffi_a269d620xd5c405b7.o
  "_X509_VERIFY_PARAM_set1_ip", referenced from:
      __cffi_f_X509_VERIFY_PARAM_set1_ip in _Cryptography_cffi_a269d620xd5c405b7.o
  "_X509_VERIFY_PARAM_set1_ip_asc", referenced from:
      __cffi_f_X509_VERIFY_PARAM_set1_ip_asc in _Cryptography_cffi_a269d620xd5c405b7.o
  "_X509_VERIFY_PARAM_set_hostflags", referenced from:
      __cffi_f_X509_VERIFY_PARAM_set_hostflags in _Cryptography_cffi_a269d620xd5c405b7.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: Setup script exited with error: command '/usr/bin/clang' failed with exit status 1
An error occurred when trying to install cryptography 0.9.3. Look above this message for any errors that were output by easy_install.
While:
  Installing django.
  Getting distribution for 'cryptography>=0.7'.
Error: Couldn't install: cryptography 0.9.3

I've run:

brew install openssl
brew install pkg-config libffi
env ARCHFLAGS="-arch x86_64" LDFLAGS="-L/usr/local/opt/openssl/lib" CFLAGS="-I/usr/local/opt/openssl/include"
pip install cffi

I noticed my machine already had an older version of openssl so I created a symlink to the new one:

openssl version
OpenSSL 1.0.1f 6 Jan 2014
mv /sw/bin/openssl /sw/bin/openssl_OLD
ln -s /usr/local/Cellar/openssl/1.0.2d_1/bin/openssl /sw/bin/openssl
openssl version
OpenSSL 1.0.2d 9 Jul 2015

I can also confirm I have a 64bit machine:

file /sw/lib/libffi.6.dylib
/sw/lib/libffi.6.dylib: Mach-O 64-bit dynamically linked shared library

Running the same script with a dependency of cryptography==0.7 results in the same error.

I'm running GCC 4.2

Nothing I've tried so far changes the error I get in django buildout :( When I run pip install cryptography it seems to install cleanly but this buildout script insists on installing a local copy.

Does anyone have any suggestions on what I should try next? I'm running out of ideas.


Source: (StackOverflow)

Including Fake-headers via pycparser in Windows?

We can support new typedefs , #includes and #defines , by preprocessing it with the help of pycparser parse_file . I also went through the documentation: README page and the blog post , but all the documentation was written for linux systems.I have no clue about linux systems and would love to get the documentation for windows.


Source: (StackOverflow)

how to make the Pycparser to understand my c function

I am using the pycparser for parsing my C code. When I run the script, pycparser could not understand the function and it throws an error like below.

File "C:\Python27\lib\site-packages\pycparser\__init__.py", line 93, in parse_file
return parser.parse(text, filename)
File "C:\Python27\lib\site-packages\pycparser\c_parser.py", line 124, in parse
return self.cparser.parse(text, lexer=self.clex, debug=debuglevel)
File "C:\Python27\lib\site-packages\pycparser\ply\yacc.py", line 265, in parse
return self.parseopt_notrack(input,lexer,debug,tracking,tokenfunc)
File "C:\Python27\lib\site-packages\pycparser\ply\yacc.py", line 1047, in parseopt_notrack
  tok = self.errorfunc(errtoken)
File "C:\Python27\lib\site-packages\pycparser\c_parser.py", line 1423, in p_error
  column=self.clex.find_tok_column(p)))
File "C:\Python27\lib\site-packages\pycparser\plyparser.py", line 54, in _parse_error
  raise ParseError("%s: %s" % (coord, msg))
ParseError: dsc.c:2592:1: before: {

The line number it shows is nothing but an function like this

void dsc (void)
{

Can anyone tell how to make the pycparser to undertand my function?

 static void dsc (void)
 {
 UINT8 j, i;
 static UINT16 jump;
 for (j = 0; j< 10; j++)
 {
 jump = dsc_jump

 for
  (i = 1; i < 10; i++)
 {
 if
 (
 ((jump & 0x50 != 0)
  )
 {
 jump  = dsc_jump
  }
  }

  }

Source: (StackOverflow)

Pycparser not working on preprocessed code

I need to use pycparser on preprocessed C code (the results produced by 'gcc -E'). However I am currently running into issue that I can't understand or solve.

I am using the provided samples year2.c and func_defs.py, which i modified to use a variety of preprocessors and fake libraries to no avail. Maybe some of you can look into this and see if you can reproduce/solve the issue. I will append all necessary code.

The errors were generated using year2.c (regular sample file) and year2.i ('gcc -E' output). There was no useable result for the latter while the former worked with both preprocessor/fakelib variants.

I have created a bitbucket repo with all relevant errors, the script used (albeit only its last variation) and the year2.c and year2.i files.

Error & Sample Repo

Thanks for your time.


Source: (StackOverflow)

Add line of code surrounding certain function calls

I don't know if it is possible, but I'd like to put some line of code around function calls from a certaint .c file. Say I have a file1.c in which I do something like:

#include "file2.h"
...
void fun(){
    <i want do add a line here>
    file2_fun();
    <..and here>
}

is it possible to add those lines? Can you give me some example using pycparser or a similar c parsing library for python?


Source: (StackOverflow)

Find all macro definition in c header file

I am trying to get list of all macro definitions in a c header file by using pycparser.

Can you please help me on this issue with an example if possible?

Thanks.


Source: (StackOverflow)

python pyparser fails at #pragma directive

The python C-parser pycparser fails at the following #pragma directive:

#pragma ghs section somestring="some_other_string"

generates error:

AssertionError: invalid #pragma directive

What's wrong with this #pragma?


Source: (StackOverflow)

Is there any way to avoid parse-errors in pycparser library of python

The below code triggers an error in .c file when pycparser tries to parse it. This might be because rf_wcdma_bup_flags_t is not a defined typedef, is there anyway to skip it?

rf_wcdma_bup_flags_t rf_wcdma_debug_flags = { 0 };

Source: (StackOverflow)

I have a sample.c file which has many .h files can i stop generating ast for those .h files is it possible in PycParser?

In pycparser , i am using the commandline option -I to include many directories , now it generating an AST for those .h files , which is inevitable , since at the end of the day I m passing a preprocessed output to the Cparser().So is there anyway to stop generating AST for .h files.


Source: (StackOverflow)

How to create a list of tuples (name, value) for each enum in a C file(s) with pycparser?

How to create a list of tuples (name, value) for each enum in a C file(s) with pycparser?

I'd like to cover both cases - w/

enum eOne { a=3, b=5, c=1};

or

typedef enum eOne { a=3, b=5, c=1} typeOne ;

or

typedef enum { a=3, b=5, c=1} typeOne ;

or

typedef enum eOne typeOne ;

and w/o explicitly assignments

enum eTwo {x,y,z};

or

typedef enum eTwo {x,y,z} typeTwo;

or

typedef enum {x,y,z} typeTwo;

or

typedef enum eTwo typeTwo;

Is it possible co cover the mixed case like

enum eMixed { d=4, e, f=77};

?

The expected result is

eOne = [(a,3), (b, 5), (c,1)]
eTwo = [(x,0), (y, 1), (z,2)]

or/and

typeOne = [(a,3), (b, 5), (c,1)]
typeTwo = [(x,0), (y, 1), (z,2)]

.


Source: (StackOverflow)

Ignore includes with #pycparser and define multiple Subgraphs in #pydot

I am new to stackoverflow, but I got a lot of help until now, thanks to the community for that.

I'm trying to create a software showing me caller depandencys for legacycode.

I'parsing a directory with c code with pycparcer, and for each file i want to create a subgraph with pydot.

Two questions:

  1. When parsing a c file, the parser references the #includes, an i get also functions in my AST, from the included files. How can i know, if the function is included, or originaly from this actual file/ or ignore the #includes??

  2. For each file i want to create a subgraph, an then add all functions in this file to this subgraph. I don't know how many subgraphs i have to create...

I have a set of files, where each file is a frozenset with the functions of this file somthing like this is pssible?

    for files in SetOfFiles:
        #how to create subgraph with name of files?

        for function in files:
            self.graph.add_node(pydot.Node(funktion))  #--> add node to subgraph "files"

I hope you got my challange... any ideas?

Thanks!

EDIT:

I solved the question about pydot, it was quiet easy... So I stay with my pycparser problem :(

    for files in ListOfFuncs:
        cluster_x = pydot.Cluster(files, label=files)

        for functions in files:
            cluster_x.add_node(pydot.Node(functions))

        graph.add_subgraph(cluster_x)          

Source: (StackOverflow)