Need a tool to determine the type of variables in C code

I participate in a project developing a specific source-source compiler. At this point, I need to find the type of variables in the source code C. For example, if the code c[i]=j*f[k]+p; , I have to find the type of the variables c , i , j , f , k and p ( int* , float , and any other type defined in the source). Is there any tool for this? If there are several tools, I prefer a python based tool.

Thanks for coming.

+4
source share
2 answers

you can use pycparser to write your own parser, you can find more examples here

 from pycparser import c_parser parser = c_parser.CParser() text = 'int x; int y; float z;' ast = parser.parse(text, filename='<none>') ast.show() FileAST: Decl: x, [], [], [] TypeDecl: x, [] IdentifierType: ['int'] Decl: y, [], [], [] TypeDecl: y, [] IdentifierType: ['int'] Decl: z, [], [], [] TypeDecl: z, [] IdentifierType: ['float'] 
+4
source

you should use llvm-tools to create an AST from the source code and then analyze the AST. besides your task to write something like llvm-tools :)

here is an example of how to use llvm bindings for python to parse a c file:

http://www.mdevan.org/llvm-py/examples.html

+2
source

All Articles