Creating a translator using CBS

Here is how to create a translator from a programming language to funcons in the PLanCompS SVN repository. Most of it is generated from specifications in the CBS meta-notation.

Platform requirements

The creation and use of Spoofax-based editors in Eclipse should be platform-independent. The process described below has been tested on the following platform:

Required files and folders

As a running example, we explain how to create a translator for a language called IMP. Language names should be short, start with a capital letter, and may consist of letters, digits and hyphens.

The name of the folder containing the CBS specification of IMP and the IMP translator could be simply IMP. Below, we use IMP-Lang instead, to avoid confusion with sub-folders whose name has to match the language name IMP.

  1. Create a folder CBS/Languages/IMP-Lang/IMP-cbsin the PLanCompS SVN repository. Then:

  2. Create a minimal Spoofax editor project:

    In IMP-Editor/editor/IMP.esv, insert , fct after extensions: imp.

    The file syntax/Common.sdf3 is not used by CBS-generated editors, and may be deleted.

    Create an empty folder IMP-Editor/cbs.

    Share the project on SVN as CBS/Languages/IMP-Lang/IMP-Editor. Note that the subdirectories "include" and "utils" do not need to be shared on SVN.

    The remaining steps all concern files located in this IMP-Editor project.


  3. Add the following svn:externals property to the empty folder IMP-Editor/cbs, to create links from cbs/IMP to CBS/Languages/IMP-Lang/IMP-cbs/IMP, and from cbs/Funcons to CBS/Funcons:

     IMP svn://svn.plancomps.org/resources/CBS/Languages/IMP-Lang/IMP-cbs/IMP
     Funcons svn://svn.plancomps.org/resources/CBS/Funcons
    

    Commit the changes to the IMP-Editor project, then update the project from SVN. The IMP and Funcons links in cbs should now give access to their target folders.

Generating SDF and Stratego from CBS

Note: Eclipse will initially flag various errors and warnings in the generated files. The errors should all disappear when the IMP-Editor project has been successfully built; some warnings (e.g., about overloaded constructors in SDF3 files) will remain, but can be ignored.

  1. cbs/IMP/IMP-Start.cbs: Open this file in the CBS editor.

    Check that all references to names in all the CBS files have been successfully resolved to their definitions. If not, click the Reset and reanalyze button. If that doesn't work, select the IMP-Editor project and use the pop-up menu items Unload analysis followed by Reload analysis.

    Click the Generation button.

    The generation of all the required components should be reported in a console window.

  2. Edit syntax/IMP.sdf3 to have the following contents:

     module IMP
    
     imports
         IMP/IMP-Start
    
     context-free start-symbols
    
       Start
    
     context-free syntax
    
       Start = L-start
    

    This is also the file where one may add or import SDF3 disambiguation rules, based on copies of the productions generated in syntax/cbs-gen/IMP/.

Building the project

Testing the generated editor