{"id":121,"date":"2011-03-26T23:53:36","date_gmt":"2011-03-26T22:53:36","guid":{"rendered":"http:\/\/paguilar.org\/?p=121"},"modified":"2011-03-26T23:53:36","modified_gmt":"2011-03-26T22:53:36","slug":"a-simple-c-to-xml-serializer","status":"publish","type":"post","link":"https:\/\/paguilar.org\/?p=121","title":{"rendered":"A simple C to XML serializer"},"content":{"rendered":"<p>Some days ago I was asked for writing a program that could read any given C struct or union from a header file and serialize it to XML. <\/p>\n<p>I decided to write a program, called <strong>SC2XML<\/strong>: Simple C to XML serializer, flexible enough for adding later new C constructs like enums and typedefs. <\/p>\n<p>In order to serialize any valid C struct\/union I needed a C grammar and something that could scan and parse the file following that grammar. The best tools for doing this are <strong>lex<\/strong> and <strong>yacc<\/strong>. In Linux, <a href=\"http:\/\/flex.sourceforge.net\/\">Flex<\/a> and <a href=\"http:\/\/www.gnu.org\/software\/bison\/\">Bison<\/a> are the Open Source implementations of these old but quite useful Unix tools.<br \/>\nI strongly recommend these tools if you need to create a compiler, an interpreter or even a shell (you can have a look at a project called <a href=\"http:\/\/sourceforge.net\/projects\/tarasca\/\">tarasca<\/a> that I wrote some years ago as a proof of concept of a Cisco-like shell).<\/p>\n<p>Since writing an ANSI C grammar is not a easy task, I downloaded the <a href=\"http:\/\/www.lysator.liu.se\/c\/ANSI-C-grammar-l.html\">lex<\/a> and <a href=\"http:\/\/www.lysator.liu.se\/c\/ANSI-C-grammar-y.html\">yacc<\/a> implementations of the ANSI C grammar written by Jutta Degener and modified it because it misses a few things like user-defined data types.<\/p>\n<p>The first version of this program could potentially serialize other C constructs and almost any C code since it uses an almost full-compatible C grammar (see the header comment of scanner.l and parser.y for details).<\/p>\n<p>The sc2xml package needs, as requirements, the following two standard libraries:<\/p>\n<ul>\n<li>glib-2<\/li>\n<li>libxml2<\/li>\n<\/ul>\n<p>The package already provides the files generated by Flex and Bison called <em>lex.yy.c<\/em>, <em>parser.c<\/em> and <em>parser.tab.h<\/em>. These files are created from <em>scanner.l<\/em> and <em>parser.y<\/em>. Thus, if you modified the last two files, you will also need Flex and Bison.<\/p>\n<p>You can configure, compile and install sc2xml using the traditional way:<\/p>\n<pre lang=\"bash\">\n.\/configure\nmake\nsudo make install\n<\/pre>\n<p>This version, as it is normal with a first alpha release, has some limitations and bugs. At them moment, it doesn&#8217;t support some constructs like<\/p>\n<ul>\n<li>Anonymous structs\/unions<\/li>\n<li>Multi-line pre-processor directives and macros <\/li>\n<\/ul>\n<p>You can have a look at the <em>README<\/em> for more details about the current limitations and bugs.<\/p>\n<p>If I have the time or there is the interest for expanding this program, I would post it in sourceforge or gitorious. Meanwhile, you can download the source code from here: <a href=\"https:\/\/www.paguilar.org\/projects\/sc2xml-0.0.1.tar.gz\">sc2xml-0.0.1.tar.gz<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some days ago I was asked for writing a program that could read any given C struct or union from a header file and serialize it to XML. I decided to write a program, called SC2XML: Simple C to XML serializer, flexible enough for adding later new C constructs like enums and typedefs. In order\u2026 <span class=\"read-more\"><a href=\"https:\/\/paguilar.org\/?p=121\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-121","post","type-post","status-publish","format-standard","hentry","category-compiling"],"_links":{"self":[{"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/posts\/121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/paguilar.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=121"}],"version-history":[{"count":0,"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/posts\/121\/revisions"}],"wp:attachment":[{"href":"https:\/\/paguilar.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/paguilar.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/paguilar.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}