-: Python Code :-

cdecl.py

News

*Sat Jan 11, 2003*
Have discovered bgen (it's in the python Tools directory). Hopefully cdecl.py will be talking to bgen soon.

About

This one understands c declarations as found in header files.

The output is valid python code; the parse tree, in fact, along with the source and an explanation.

[simon@arrow]$ ./cdecl.py "int foo;"
#int foo;

Declaration(
  Declarator(
    Identifier('foo'),
    DeclarationSpecifiers(BasicType('int'))))
# explanation : foo is int.
#-----------------------------------------------#
[simon@arrow]$ ./cdecl.py "void (*signal(void(*)(int)))(int);"
#void (*signal(void(*)(int)))(int);

Declaration(
  Declarator(
    Identifier('signal'),
    Function(
      ParameterDeclaration(
        AbstractDeclarator(
          Pointer(),
          Function(
            ParameterDeclaration(
              AbstractDeclarator(TypeSpecifiers(BasicType('int'))))),
          TypeSpecifiers(BasicType('void'))))),
    Pointer(),
    Function(
      ParameterDeclaration(
        AbstractDeclarator(TypeSpecifiers(BasicType('int'))))),
    DeclarationSpecifiers(BasicType('void'))))
# explanation : signal is function (pointer to function (int), returning void), returning pointer to function (int), returning void.
#-----------------------------------------------#
He doesn't inspect (non-trivial) expressions, as found in array sizes, enums, initializers etc.
int wont_look_at_this_next_bit[sizeof(struct foo)*3];

There are also some hacks so that (gcc) standard include files can be parsed:

[simon@arrow]$ ./harvest.py /usr/include/stdio.h 
...

Future

This is part of a wrapper generator; something like swig, only written in python, with python customisable output.

Maybe someone will extend the grammar to understand all of ANSI-C.

Download

Hours of fun! Impress your friends!

cdecl-0.45.tar.gz - 22nd january

cdecl-0.41.tar.gz - some type manipulations

cdecl-0.38.tar.gz - explanations broken, but now produces c code from parse tree.

cdecl-0.29.tar.gz - explanations work, but has some other bugs that have since been cleaned out.



Copyright Simon Burton 2002, 2003.

Last update: Jan 14, 2003

vi improved