eliben / pycparser

:snake: Complete C99 parser in pure Python
Other
3.21k stars 612 forks source link

BUG: pycparser treats empty declarations as a binary operation #529

Closed id-aaryan closed 5 months ago

id-aaryan commented 6 months ago

Let's take the following C code:

int my_function(struct_0 *var_0)
{
  ...
  isdn_net_phone *n;
  ...
  return FLAG;
}

In this case, PyCParser parser the file, and creates an AST that represents the line isdn_net_phone *n; as the following: BinaryOp(op='*', left=ID(name='isdn_net_phone' ), right=ID(name='n' ) )

I have a function that adds fake typedefs to the top of my file - I needed more than what is provided in using_gcc_E_libc.py. However, the main issue is that here the parser treats the line of code as a normal action, i.e., BinaryOp, whereas this line is merely an instantiation of the isdn_net_phone * object.

If you don't know how to fix it and want a spare set of eyes to help out, I'd be more than happy to take a look, but just wanted to know whether this is a known bug. And, also if I am using the tool incorrectly, please let me know.

Thanks

eliben commented 6 months ago

I'm almost certain you're hitting the issue described in https://eli.thegreenplace.net/2011/05/02/the-context-sensitivity-of-cs-grammar-revisited/ and then https://eli.thegreenplace.net/2015/on-parsing-c-type-declarations-and-fake-headers/

Please create a minimal reproducer if you think otherwise.