cdecl is a tool for decoding C type declarations. It was first first described in K&R2 and since then is part of most Unix/Linux distributions.

Well, since my pycparser project is nearing completion, I've decided to implement cdecl on top of it. As pycparser is a complete parser and produces ASTs from ANSI C, I figured this shouldn't be too complicated.

Indeed, it turned out to be quite simple. The core of the implementation is a 30-line function that traverses the AST recursively and spits out the explanation.

Here are a couple of sample results:

char *(*(**foo [][8])())[];

=> foo is a array of array[8] of pointer to pointer to function() returning pointer to array of pointer to char

static const char* const cptr[2];

=> cptr is a static array[2] of const pointer to const char

The cdecl is a simple and fun way to experiment with pycparser. It teaches how to explore the AST created by pycparser an traverse it correctly.


comments powered by Disqus