SmaCC Transformation Toolkit

The SmaCC Transformation Toolkit can be used to create scripts that transform large codebases. It has been used on several projects to convert programs written in one language into another. The basic process is to write a SmaCC Transformation Toolkit script file that performs the transformation, run the transformed code to validate the result, and then iterate by refining the script and rerunning the transformation.

The transformation toolkit takes a script and a set of files to transform. It processes each file individually by first parsing the file into an AST and then running the script on the nodes in that AST. By default, the nodes are processed top down, but this can be changed by the script. Once the script has processed the AST, the resulting edits are saved and the next file is processed. Unlike tools that modify the AST and then regenerate source code from the modified tree, the SmaCC transformation toolkit uses edit operations to transform the original source directly.

As an example, consider a script that converts a simple language defined by GtSmaCCTransformationExampleParser SmaCCGLRParser << #GtSmaCCTransformationExampleParser slots: {}; tag: 'Transformations'; package: 'GT4SmaCC-Examples' into JavaScript.

We can use this script to transform a program written in our simple language into JavaScript:

This is the result of applying the script above to the simple program:

The Transforming Delphi code into C# (at syntax level) page contains a simple script that converts basic Delphi syntax into C#. It is not intended to be a complete Delphi-to-C# conversion, but it does show how parts of Delphi syntax can be transformed into C#.