Some while ago I wrote a simple C to XML serializer that reads C structs and unions from a header file a writes them to a XML file. You can read that post here.
I’ve improved it a little bit and released it in GitHub. You can check it out here.
This program is in its first stages and currently it only converts the most widely used C-structs constructs to XML. However, it could potentially serialize other C constructs and almost any C code since it uses an almost full-compatible C grammar.
The version I released in GitHub adds the support for structs/unions defined inside a macro.
I’ll try to explain how this feature works:
Suppose you have a file, test3.h, with a macro that defines the following struct:
#define def_struct_data(prefix,number,code_number)
struct prefix##driver_data
{
task_status_t tsk_sts;
struct buttons_platform_data pdata[number];
struct prefix##an_event event;
struct prefix##buttons_settings
{
uint8_t thresholds[3];
uint8_t pressed_threshold;
struct prefix##buttons_mask hash_table[code_number];
}__attribute__((packed)) settings;
uint8_t raw[((number) / 8) + 1];
uint8_t raw_mem[((number) / 8) + 1];
}__attribute__((packed));
As you probably know, the syntax of a C macro is not valid for a C compiler, that’s one of the reasons for using the C pre-compiler: it converts the macro to something that the compiler understands.
In the SC2XML case, applies the same condition. Therefore, you have to create a very simple C file that includes the file that you want to serialize, in this case test3.h, along with the use of the macro and execute sc2xml with that file:
Assume that the name of your simple C file is test3.stub.h. It should looks like this:
#include "test3.h"
def_struct_data(prefix,number,code_number)
def_struct_data is the name of your macro.
Execute sc2xml:
$ sc2xml test3.stub.h
sc2xml 0.0.2
INFO: *** Parsing file data/test3.gen.h ***
The final generated XML file, test3.h.xml, will look like this:
prefixdriver_data
tsk_sts
pdata
event
prefixbuttons_settings
thresholds
pressed_threshold
hash_table
__attribute__ ( ( packed ) )
settings
raw
raw_mem
__attribute__ ( ( packed ) )
Now you can see that the XML file shows exactly the struct defined in the macro def_struct_data but with nice XML tags!
I hope this project could be useful, specially if you want a non-trivial example that defines a parser and grammar with lex and yacc.