This tutorial explains how to create Xtext grammars that can be used with BCerT.

Step 1 — Create an Xtext project

  1. Open Eclipse
  2. Go to File → New → Project
  3. Select Xtext Project
  4. Click Next

  5. Fill the following information:
    • Project name (e.g. org.xtext.families)
    • Language name (e.g. org.xtext.Families)
    • File extension (e.g. fam)
In the following steps we use two languages org.xtext.families (with extension .fam) and org.xtext.persons (with extension .per).

Click Finish. Xtext will generate the following components:

  • the grammar definition
  • the parser
  • the EMF metamodel
  • the editor

Step 2 — Define the grammar

Edit the .xtext file located in the project.

The screenshot below shows the two grammars that are considered in the next steps of the user’s guide.
FamilyModel:{FamilyModel}
	families+=Family*;
Family:
	'Family' lastName=ID '{'
		  ('Mother' theMother=Member)?
		  ('Father' theFather=Member)?
		  ('Daughters' (daughters+=Member)+)?
		  ('Sons' (sons+=Member)+)?
	'}';
Member : firstName=ID ;
PersonModel:{PersonModel}
	  persons+=Person*;
Person: Male | Female ;
Male : 'Male' (name+=ID)+ ;
Female : 'Female' (name+=ID)+ ;

Step 3 — Generate the Xtext artifacts

Right-click on the .xtext grammar file and select: Run As → Generate Xtext Artifacts

Xtext generates the following elements automatically:

  • an .ecore metamodel
  • a corresponding .genmodel file

These files are created in the folder: model/generated

The .ecore file represents the meta-model derived from the grammar, while the .genmodel file is used to generate the EMF infrastructure.

For each generated .genmodel file (Families.genmodel and Persons.genmodel):

  1. Open the file
  2. Right-click on the root element of the file
  3. Select Generate Edit Code

This step generates the EMF editing support required by BCerT.