Click here to Skip to main content
13,352,408 members (59,938 online)
Click here to Skip to main content
Add your own
alternative version


18 bookmarked
Posted 14 Jul 2007

Porting Code Through XML

, 14 Jul 2007
Rate this:
Please Sign up or sign in to vote.
Framework for porting code between different languauges and platforms through a standard XML schema.


This article aims to present a framework for porting source code from one language or platform to another, using XML as the intermediate format in the conversion process.


The concept of this article came to my mind last weekend or so while I was thinking about some programming and design related things. I don't know about you, the reader, but I have ported some amount of code from one language to another for work and personal projects. In most cases, I either ported the code due to design/usability issues (something lacking in source language/platform) or to support a larger target audience (who use a particular language or platform, usually due to convenience or their skill set).

Porting code, depending on what you are porting it to, can be trivial or complex. But even in the trivial case, it can still be tedious and time consuming to do. And porting to more than one target language or platform will multiply that factor some more. This can be alleviated with some form of automation or tools that handle this.

I don't know if there is any good solution for this out there, but this article describes a possible solution. Due to the complexity of this solution, no code or real examples are presented. This article only describes a theoretical solution that is yet to be implemented.

The framework for porting code

The process of porting source code consists of two steps:

  1. Port source code (of the source language/platform) to the defined XML schema.
  2. Translate the intermediate XML to the target language/platform.

Therefore, the framework consists of three parts:

  1. Source language/platform code parser that converts the code to XML.
  2. An XML schema that defines things like general programming language constructs such as loops, branches, variables, etc.
  3. An XML parser that converts the XML generated by step 1 to the target language/platform.

The XML schema would be like a subset of XML, like SVG, XSL, etc., something yet to be defined (and maybe even standardized).

Ideally, to make the conversion effort simple and standardized for users/developers, the following are suggested when using this framework:

  • Parser for the source language/platform should be written in the source language or built on the source platform. This will make it easier for the user to tweak the parser, when needed.
  • Parser for the target language/platform should be written in XSLT. Since the intermediate format is XML, XSLT provides the easiest language and platform agnostic way of parsing it. Additionally, with an XSL stylesheet template, you can easily derive customized stylesheets for each target language/platform by replacing the placeholder sections with target code and adding/removing some XSLT code snippets as needed.

Ideally, if this framework did exist (with defined XML schema and pre-built parsers), I would like it to cover much of the general programming and scripting languages out there like C, C++, C#, Java, JavaScript, Perl, PHP, Python, VBScript, Visual Basic, Visual Basic .NET, etc.

Though I am thinking of using this framework mostly to port between different languages, I believe it is also possible to extend the framework to support porting between platforms like Windows and Linux/Unix as well as porting from x86 to embedded platforms, etc.

Disclaimer: No framework is perfect. I would not expect this framework to provide flawless conversion between languages or platforms, but it would at least simplify the porting of 50-75 percent of the code since most code is usually of the general programming construct type as mentioned earlier in the article. The parts that aren't well ported can then be custom ported as needed.

Theoretical example of framework in action

Assume you have the following code snippet in VBScript:

For i = 1 To 100
    WScript.Echo "Code ran " & i & " times."

Run the code through the source parser and you should get some XML like the following:

<loop type="for">
<condition><var id="i"/><compare type="lessthan"/>100</condition>
<output>Code ran <var id="i"/> times.</output>

Run the code through the target parser (for C++) and you should get something like the following:

for(int i = 1; i < 100; i++)
    cout << "Code ran " << i << " times." << endLine;

Disclaimer: This isn't a perfect example, code-wise, but is given just to illustrate the idea. The XML in the example probably is not the optimal schema, I just came up with it real quick. A real schema would take time to design and fine-tune.

Intentions of the framework

The framework is intended to be a platform and language independent solution that only relies on XML and XSLT. The language/platform choice for the source code parser and processing the XML with XSLT would be up to the user.


That is all, any questions or comments? I would really love to work on such a framework along with the code parsers, but I don't have the time for it right now. So, I thought I would post my idea out for others to see and try.

If someone does pick up work on such a framework, please inform me as I would like to assist with such a project.

If such a framework is already in progress or exists, again, please inform me. I may delete this article, or this article might just provide another alternative implementation to such a framework.


  • July 14th 2007 - Initial post of the article.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
United States United States
Practitioner of C#, .NET, C++, C, ASP, VBScript, Windows Scripting Host, JScript, Perl, Perlscript, ADO.

Interested in automation, systems & application integration, web services & applications, and mobile computing.

You may also be interested in...


Comments and Discussions

GeneralPart of the reason for the CLR/MSIL... Pin
reinux15-Jul-07 20:17
memberreinux15-Jul-07 20:17 
GeneralRe: Part of the reason for the CLR/MSIL... Pin
daluu16-Jul-07 11:53
memberdaluu16-Jul-07 11:53 
GeneralRe: Part of the reason for the CLR/MSIL... [modified] Pin
reinux16-Jul-07 17:56
memberreinux16-Jul-07 17:56 
GeneralRe: Part of the reason for the CLR/MSIL... Pin
daluu17-Jul-07 8:27
memberdaluu17-Jul-07 8:27 
GeneralCode Generation Pin
ByteGhost15-Jul-07 16:31
memberByteGhost15-Jul-07 16:31 
GeneralRe: Code Generation Pin
daluu15-Jul-07 19:32
memberdaluu15-Jul-07 19:32 
GeneralRe: Code Generation Pin
ByteGhost16-Jul-07 1:53
memberByteGhost16-Jul-07 1:53 
GeneralA good idea but... Pin
Craig G. Wilson15-Jul-07 13:18
memberCraig G. Wilson15-Jul-07 13:18 
GeneralRe: A good idea but... Pin
daluu15-Jul-07 19:26
memberdaluu15-Jul-07 19:26 
Thanks for the feedback. You have valid points.

Here is the reasoning behind my approach:

Building an AST is also a lot of work, unless one is well acquainted in data structures and compiler theory. Xml provides an easier alternative to port to.

This approach was idealized for "write once, port to many". Xml provides for a standardized representation of code (if you define a standard first of course). Sorry, I'm just a novice in compiler theory and data structures but an AST may or may not be the same for each language. So if it's different, that's more work in the porting (to multiple languages/platforms) effort. With the use of an AST, you may have to define a standard format as well to simplify the porting effort.

An AST is simpler to manage in memory (at least for novices) but you can read it from and write it to disk as well. However, when it is on disk, it is rather hard to decipher since it is a data structure and not plain text like xml. So to decipher the file (before parsing with target parser), you need to use/write a parser or you need to have intimate knowledge of the AST. With xml, anyone familiar with it, and with a copy of the xml schema, can interpret some of the logic without need of a parser. So even in intermediate form, the code is still comprehensible.

And then regarding parsing to the target, as I suggested in the article, XSLT should be used to convert xml to the target code. The reason for this is (1) XSLT can be run from most languages and platforms (often through some library), so you won't have a dependence on any language/platform (you can run the Xml with XSLT through the source language/platform to generate code for the target); and (2) with one standard xml format, most of the XSLT lexing/parsing code will be the same, so you need only customize the XSLT sections that define what to do when you encounter a specific Xml tag/node and define anything else that is specific only to the target platform. The end result - this saves you from having to write multiple target parsers, if you know XSLT.

Overall, if this framework was established with some pre-built source parsers and target XSL stylesheets, porting code would be much easier for developers.

By the way, one last thing about your comment:

Craig G. Wilson wrote:
I have not looked at your implementation, but I assume you are lexing/parsing the source into and abstract syntax tree (AST) and then visiting the AST to create the xml.

I didn't really think about it that way or that deep. I was just thinking read in code, output to xml, based on the xml schema that defines things like loops, branches, etc. I was thinking more about the big picture of the framework. Oh, and this is just a theoretical idea, there is no actual implementation (from me) to date.

"A good scientist is a person with original ideas. A good engineer is a person who makes a design that works with as few original ideas as possible." - Freeman Dyson

GeneralRe: A good idea but... Pin
Craig G. Wilson16-Jul-07 3:30
memberCraig G. Wilson16-Jul-07 3:30 
QuestionOn Error GoTo Pin
Thomas Lykke Petersen14-Jul-07 23:58
memberThomas Lykke Petersen14-Jul-07 23:58 
AnswerRe: On Error GoTo Pin
daluu15-Jul-07 19:37
memberdaluu15-Jul-07 19:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 14 Jul 2007
Article Copyright 2007 by daluu
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid