parsing C function declarations to generate code to serialize the formal arguments

"Moshe Pfeffer" <einatbg@zahav.net.il>
14 Dec 2006 14:06:33 -0500

          From comp.compilers

Related articles
parsing C function declarations to generate code to serialize the form einatbg@zahav.net.il (Moshe Pfeffer) (2006-12-14)
Re: parsing C function declarations to generate code to serialize the eliotm@pacbell.net (Eliot Miranda) (2006-12-14)
Re: parsing C function declarations to generate code to serialize the DrDiettrich1@aol.com (Hans-Peter Diettrich) (2006-12-14)
Re: parsing C function declarations to generate code to serialize the alex_mcd@btopenworld.com (Alex McDonald) (2006-12-15)
Re: parsing C function declarations to generate code to serialize the idbaxter@semdesigns.com (Ira Baxter) (2006-12-24)
| List of all articles for this month |

From: "Moshe Pfeffer" <einatbg@zahav.net.il>
Newsgroups: comp.compilers
Date: 14 Dec 2006 14:06:33 -0500
Organization: SMILE INTERNET ZAHAV
Keywords: parse, C question, comment

I am looking for an elegant method to do the following. Are there
tools available to help? Have you done something like this that I may
also try?


I wish to take a C header file consisting of a list of function
declarations, parse the function declaration to obtain a
representation of the formal parameters from which to generate C
code. Each argument must be processable as a tree, since it may be a
struct, or pointer to a struct, with further structs/pointer to
structs as it's members, or member's members, etc. If it is a simple
type, the tree has 1 node, and depth 1. If it is a pointer to a simple
type, the tree has 2 nodes and depth 2 (one for the pointer and one
for the data pointed to).


The result:


For each function F(i) in the H file there will be N(F(i)) trees
generated (one for each formal parameter + return type). Let's call
that a cluster. One cluster of trees represents a full specification
of one function's parameters + it's return type.


The forest thus consists of M clusters, where M is the number of
functions declared in the H file.


Note: there will be no cycles in any of the trees, as non of the
argument types reference themselves or parts of themselves (as you
would find, say, in a linked-list struct).


Once the forest is represented, I then need to generate code for each
cluster (i.e. each function).


My intention is to traverse the tree to generate C code to serialize the
arguments into a character buffer, and to deserialize a character buffer
into a set of local variables which reconstructs the "tree" formed by the
actual parameters of the function at run time.
========================================================
Here is a more algorithmic description of what the generated code must
do, and the algorithm that the code generator will use
(approximately).


It goes something like this, loosely speaking:


File: myfile.h contains one function declaration:
sometype somefunc(arguments);


From this will be generated 2 C functions:


// same signature as somefunc
sometype somefunc_caller(arguments) {
                start with empty buffer
                with each argument
                        serialize the argument tree into the buffer, argument by
argument,
                        member by member.


                call somefunc_receiver(buffer) to process and alter the buffer
                retrieve the alterered arguments from the buffer
                return
}


void somefunc_receiver(buffer) {
                local variables defined corresponding to the signature somefunc.
                one local variable defined for each formal parameter.
                one local variable defined for each pointer's contents
                one local variable defined for the return value of the call to
somefunc.


                /* the following line is a shorthand way of representing several
lines of code
                      for each independent argument or member of an argument list */


                with each argument of the parsed function declaration
                          deserialize the argument from the buffer. if it's a struct
(pointer) tree,
                          store the (pointed to) data in the appropriate local variable
and point the
                          pointer to it (if non-null).


                call somefunc, using the deserialized local variables as arguments


                empty the buffer
                with each argument
                        serialize the argument tree into the buffer
                serialize the return value of somefunc
                return;
}




The algorithms the code generator will use to process the formal parameter
representation goes something like this:


serialize(thing, buffer)
                        case: simple type
                                    generate code to add it to the buffer
                        case: pointer
                                    generate code to add the pointer to the buffer
                                    if the pointer is non-null, recurse:
                                                serialize(*thing, buffer)
                        case: struct
                                    for each data member of thing, recurse:
                                                serialize(data member, buffer)


deserialize(thing, buffer)
                        case: simple type
                                    generate code to extract it from the buffer into it's
place in the local
                                                "copy" of the argument tree
                        case: pointer
                                    generate code to extract the pointer from the buffer into
a
                                            variable whose name matches the name


                                    if the extracted pointer is non-null
                                                deserialize(*thing, buffer)
                        case: struct
                                    for each data member of thing
                                                deserialize(data member, buffer)


Thanks in advance,
Moshe
[Generally speaking, you can't parse a C header file with anything less
than a full C parser. If you have particular applications in mind, you
might be able to cheat and get by with less. -John]


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.