Skip to main content
Jump to: navigation, search

Gymnast

Revision as of 16:41, 2 November 2007 by Miguel.garcia.tuhh.de (Talk | contribs) (POJO-based CSTs)

How to use Gymnast Generator

Gymnast is a framework for jumpstarting text editors for custom Domain Specific Languages, written by Chris Daly, and summarized in these slides.

Gymnast is being distributed along with Emfatic, as their developer communities overlap (at least, so far).

Overview

Gymnast comprises two main parts (Runtime and Generator), each of which can in turn be divided into Core and User Interface. This section is all about using the Gymnast Generator to:

  1. create .ast files
  2. configure options in that file
  3. manually add a lexer file, and
  4. manually add the parser's .jar to the (Build path or Plugin dependencies)

so that POJO-style CST classes can be generated and compiled. More details are discussed in the technical report Generation of Eclipse-based IDEs for Custom DSLs



Let's say we want to reproduce the steps to generate POJO CST classes for Emfatic. An Eclipse project should be created and an .ast and a lexer file added, to look as shown in Figure 1(a) (more on that lexer file later).


GymnastHowToUseGymnast1.PNG
Figure 1(a) Before Gymnast Generator


After the generator is run (by rightclicking on emfatic.ast and choosing "Generate AST") the result looks as in Figure 1(b).


GymnastHowToUseGymnast2.PNG
Figure 1(b) After Gymnast Generator


Not shown here is the added plugin dependency to org.antlr, which can be installed as described next.

What if you target ANTLR instead of JavaCC

In that case, the code generated for the parsing infrastructure will depend on the ANTLR parser generator framework. The easiest way to install a plugin with ANTLR is to get it from the update site on SourceForge by following these steps: <p/>

  1. In Eclipse, go to Help -> Software Updates -> Find and Install....
  2. Select Search for new features to install and click on Next.
  3. Press the New Remote Site... button and enter the following values:
  4. Now check the new ANTLR checkbox and click on Next.
  5. Select version 3.0.5 of the feature org.antlr.ui and click on Next.
  6. Accept the licensing terms and click on Next and Finish.
  7. Click on Install when you see a pop-up message warning about unsigned features.
  8. When prompted, click on Yes to restart the workbench.

Choosing target parser, and providing the parser-dependent lexer file

The choice of target parser is made with the option parserGenerator = ". . ." (Figure 2). Unlike ANTLR, JavaCC requires no runtime library: all the code it needs gets generated.


The lexer file (emfaticLexer.g in the ANTLR example) is actually constant for all .asts we care to write. It can be copied from org.eclipse.gymnast.generator.core.parser (the file ending in .g is the ANTLR one, ending in .jj the JavaCC one) The only thing that needs updating are its filename (to match the name of the DSL in question) and, in the case of ANTLR, the first line in the lexer file (because such heading also contains the name of the DSL).


GymnastantlrOption.PNG
Figure 2 How to choose a parser

Exploring the generated artifacts

The generated grammar files (for the ANTLR example the one highlighted in Figure 3) are not supposed to be messed up by the user, however in order to explore them the following Eclipse plugins prove useful:

GymnastGeneratedGrammarFile.PNG
Figure 3 Generated artifacts


POJO-based CSTs

The generated POJO-style CST classes:

  • exhibit useful patterns for later processing (visiting in particular)
  • are independent from the chosen parser generator


The input-output relationship and the resulting code patterns are described by Gymnast's author, Chris J Daly, in an Eclipse Tech Panel Exchange presentation, available here.


The parser-generator-independent CST classes generated by Gymnast Generator implement org.eclipse.gymnast.runtime.core.ast.ASTNode (Figure 4(a)).

GymnastASTNode.PNG
Figure 4(a) ASTNode


A sample POJO-style CST class is depicted in Figure 4(b), corresponding to the following grammar production.

 
sequence classDecl : (abstractModifier)?
                     classKind name=ID
                     (typeParamsInfo=typeParamsInfo)?
                     ("extends" superTypes=commaListBoundExceptWild)?
                     (COLON instClassName=boundExceptWildcard)?
                     LCURLY classMemberDecls RCURLY ;


GymnastClassDecl.PNG
Figure 4(b) ClassDecl

Organization

Initial committers

Back to the top