Jump to: navigation, search

Grammar2Ecore

How to use Grammar to Ecore Generator

This article describes the steps involved in the generation of .genmodel, .ecore and Java from a grammar file using Gymnast, Emfatic and Grammar2Ecore frameworks.

Initial conditions

  • Eclipse plug-in project with a sample grammar file (in this example, SVG.ast) exists as shown in Figure 1.
  • JavaCC or Antlr can be used as Parser Generators.
  • srcGymnast (in Figure 1) is the source folder to which Gymnast runtime core plug-in is added manually. Please refer "Bugzilla entry 233805"
Grammar2Ecore1.jpg
Figure 1 Project structure
  • Dependent Plug-ins are added in the MANIFEST.MF file. Hence right clicking on the grammar file shows options like 'Generate .genmodel and Java', 'Generate .genmodel' etc. Refer Figure 2.
Grammar2Ecore2.jpg
Figure 2 Context menu for grammar file

Grammar to Java conversion

This section describes the steps involved in the conversion of grammar file to Java code, along with the generation of .genmodel and .ecore. Problems like compilation errors, missing files etc., may occur at some of the steps in the grammar to Java conversion process. These problems are documented in Grmr2JavaReport, along with the possible workarounds/solutions.

Step 1: Generation of lexer files

After checking for the well-formedness of the grammar, JavaCC and Antlr lexer files can be auto generated by clicking 'Generate JavaCC lexer file' and 'Generate Antlrv2 lexer file' from the context menu of the grammar file. This autogeneration of lexer files is one of the enhancements made to Gymnast. In this example, svgLexer.jj and svgLexer.g are JavaCC and Antlr lexer files respectively. Refer Figure 3.

Grammar2Ecore3.jpg
Figure 3 Autogenerated lexer files

Step 2: Generation of .ecore and .genmodel

Ecore and Genmodel can be generated from the grammar, using the context menu links 'Generate .ecore' and 'Generate .genmodel'. Refer Figure 4. This step is optional as both the .ecore and .genmodel files will be autogenerated in the step 4.

Grammar2Ecore4.jpg
Figure 4 Ecore and Genmodel generation

Step 3: Generation of AST and Parser

Generate AST and Parser packages using Gymnast from the grammar, using the context menu link 'Generate AST'. The project structure is as shown in Figure 5.

Grammar2Ecore5.jpg
Figure 5 AST and Parser packages

Step 4: Generation of Java

Generate the Java code from the grammar file by clicking 'Generate .genmodel and Java' from the grammar file context menu. The project structure is as shown in Figure 6.

File:Grammar2Ecore6.jpg
Figure 6 Generated Java code

Conclusion

This page described the steps to be followed to generate Java from a grammar file (.ast) using Gymnast, Emfatic and Grammar2Ecore frameworks.

Organization