News for hackers. Stuff that matters.
Re:Gosh Darn Function Pointers by ratboy666 on Monday January 15, 2018 @04:05PM (#16153435)
Whatever happened to 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>
Re:Gosh Darn Function Pointers by dee-see on Monday January 15, 2018 @04:26PM (#16153587)
It's also available online https://cdecl.org/
Re:Gosh Darn Function Pointers by ufo on Monday January 15, 2018 @05:31PM (#16154042)
While we are on the topic of cdecl, does anyone know where I can find a succinct algorithm for producing valid C declarations from a structured description of the type? Basically, what the examples in the parent post are doing (and NOT the other way around, which is parsing valid C declarations).

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...

Re:Gosh Darn Function Pointers by imglorp on Monday January 15, 2018 @05:54PM (#16154181)
The algorithm is the "spiral rule".

http://c-faq.com/decl/spiral.anderson.html

Re:Gosh Darn Function Pointers by ufo on Monday January 15, 2018 @06:03PM (#16154267)
Thanks, but I actually want the other way around. Instead of parsing an existing C declaration, I want to produce one when given a structured tree representing the C datatype.

I found that to be trickier than it sounds.

Re:Gosh Darn Function Pointers by webkike on Monday January 15, 2018 @05:36PM (#16154078)
There's no "algorithm" in that it's the same way you traverse over any tree to produce output, a la compilers.
Re:Gosh Darn Function Pointers by ufo on Monday January 15, 2018 @05:59PM (#16154211)
Sure, but I already spent a whole day trying to write said tree traversal, and failed miserably. Eventually I had to give up and find another way to solve my original problem, which annoys me to this day.

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.

Re:Gosh Darn Function Pointers by webkike on Monday January 15, 2018 @07:38PM (#16154852)
It entirely depends on how you structure your data, so asking for help in this regard is essentially asking someone to do it for you.
Re:Gosh Darn Function Pointers by ufo on Monday January 15, 2018 @07:54PM (#16154941)
I thought that I was making myself very clear that I was asking for someone to help me, either by pointing me to the solution or just outright stating it (I suspect that it shouldn't be very long) :)

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?

Re:Gosh Darn Function Pointers by webkike on Tuesday January 16, 2018 @01:48AM (#16156302)
I understand, but there's a difference between doing work for you and helping you get there. Imagine a sexpression representing C data types the following two ways:

(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

Re:Gosh Darn Function Pointers by ufo on Tuesday January 16, 2018 @08:03AM (#16157832)
Come on, that is a trivial difference that doesn't change anything in the grand scheme of things. At this point needlessly restricting the input format would just serve to make it harder for other people to help me find the solution I need.

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.

Re:Gosh Darn Function Pointers by teddyh on Monday January 15, 2018 @05:34PM (#16154065)
Nothing happened to it.

  apt install cdecl