: fred@dejah sql $; cdecl
Type `help' or `?' for help
cdecl> declare x as pointer to function returning int
int (*x)()
cdecl> declare x as pointer to function returning pointer to function returning int
int (*(*x)())()
cdecl> : fred@dejah sql $; cdecl
Type `help' or `?' for help
cdecl> declare x as pointer to function returning int
int (*x)()
cdecl> declare x as pointer to function returning pointer to function returning int
int (*(*x)())()
cdecl>I tried looking at cdecl's source code but everything is implemented as a big yacc grammar, which obscures the control flow of the algorithm. While this is necessary for the parsing feature, I suspect that for producing declarations it might be possible to use a simple recursive algorithm instead...
If anyone can help me find a way to convert a tree representing a C data type into a string with a syntactically valid C declaration for it I would be very grateful. I suspect that it should be possible to do this via a clean recursive algorithm, but the only implementation of this I could find so far was cdecl's LALR-based one.
I don't think it would depend very much on how the data is structured though. How many different ways are there to represent C types using tagged unions?
(pointer-to (integer))
(integer (pointed-to))
We could very easily print a decl for the second example by printing the two atoms seen in order:
int *
But if we want to print the first correctly, we will have to prepend to a string in order to correctly display the decl. That is, we will have to change our traversal order: https://en.wikipedia.org/wiki/Tree_traversal
And it is fine if you don't want to help me with this but sheesh, I am not trying to get someone else to do my homework here. I am trying to share a fun (and hopefully short) problem I couldnt solve myself yet.
I found that to be trickier than it sounds.
apt install cdecl