Macrocoder

HOWTOs - Creating and editing grammars

HOWTO - Creating and editing grammars

Creating a grammar

To create a grammar, first execute the Create and add a grammar file on the rules project. The grammar will be associated to a file with extension .fcg.

Double click on the grammar file in the rules project (circled in red in the image below) and the empty grammar editor will open.

Creation of a grammar

Adding a rule

A grammar is a list of rules. To add a new rule to the grammar, follow the instructions below:

Creation of a grammar
  1. right click in the grammar area 1;
  2. click on Add named rule 2;
  3. a dialog box will pop up; give a name to the rule 3;
  4. if this is a root rule, assign it a file extension 4 (see below for further details);

The root rule is the rule that is invoked when a given target source file is to be parsed. In order to know which root rule reads which target source file, the rule is associated to a file extension. In this example, the extension is abc: this means that the target files having name filename.abc will be parsed by this rule.

Once added, the rule will look like the picture below. The red dotted rectangle means that this rule is incomplete because no rule elements have been added yet:

Empty rule to be completed

Adding a rule element

Rule elements (fields, keywords, references, repetitions, choices and operators) can be addded by selecting the insertion point and opening the right-click menu. Insertion points can be the dotted red-box or any arrow in the diagram. In the picture below, the insertion points have been evicented with a purple dot. Once selected the insertion point and selected the type of element from the menu, the new grammar element will be created and placed at that position. If the grammar element has some configuration parameters, a dialog box will pop up immediately:

Empty rule to be completed

Editing or deleting a rule or element

Rule parameters can be edited by right-clicking their icon and selecting Edit or simply by double clicking them. To delete them, select and click on Delete or press the shortcut DEL.

Editing a choice rule

To add a branch to a choice rule, right click on the diamond icon and select Add branch; to add the empty choice branch select Activate empty choice branch.

Empty rule to be completed

To delete a branch, select the entire branch content and right-click Delete or press the shortcut DEL.