5 ms·
There's no "algorithm" in that it's the same way you traverse over any tree to produce output, a la compilers.
by webkike 9y ago
There's no "algorithm" in that it's the same way you traverse over any tree to produce output, a la compilers.
- ufo 9y agoSure, 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.
- webkike 9y agoIt entirely depends on how you structure your data, so asking for help in this regard is essentially asking someone to do it for you.
- ufo 9y agoI 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?
- webkike 9y agoI 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 https://en.wikipedia.org/wiki/Tree_traversal
- ufo 9y agoCome 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.