Hello World (MGrammar)

[This content is no longer valid. For the latest information on "M", "Quadrant", SQL Server Modeling Services, and the Repository, see the Model Citizen blog.]

This tutorial is an introduction to creating a domain specific language (DSL) using the MGrammar feature of the Microsoft code name “M” language. It enables you to describe a basic DSL with “M”, provide sample input to parse, and view the output resulting from the parse. In this tutorial, you code your DSL using the code name “Intellipad” tool editor.

The DSL in this tutorial will parse a text file that consists of the string "Hello, World".

You will learn to do the following in this tutorial:

  • How to create a DSL that recognizes a fixed string.

  • How to use the “Intellipad” editor.

To code the module

  1. Open the “Intellipad” editor by clicking Start, All Programs, Microsoft Oslo SDK, Tools, and Intellipad (Samples Enabled). The editor window appears. Be sure to pick this one, and not the entry that says "Intellipad".

  2. Add the following code fragment, which is the smallest possible “M” program, because every “M” statement must be contained inside a module. A module is the smallest unit of compilation in “M”.

    
    module Hello 
    { 
    }
    
  3. Now add a language declaration to give your language a unique name.

    
    module Hello 
    {
       language HelloWorld
       {
       } 
    }
    
  4. Save the code as a file names "Hello.mg". Note the mode indicator on the right side of the window has changed to "DSL Grammar Mode".

To Test Sample Input

  1. Now to test this empty language with a line of input. In “Intellipad”, click the DSL menu entry, and select Tree Preview. An Open dialog box appears.

  2. Navigate to a directory of your choice, or right-click and select New and then Folder to create a new directory (and name it and double-click to navigate to that folder).

  3. Right-click, and select New and then Text File. Name it Hello.txt and click Open.

  4. Note that “Intellipad” now displays 3 additional windows: one on the left in DSL Input Mode, one on the right in Output Mode, and an error window at the bottom.

  5. Type the following line into the input pane.

    Hello, World
    
  6. Note that there are no errors flagged because this language has no rules, so the language accepts all forms of input, and produces no output.

Adding Rules

  1. Add the following line of code inside the language braces. Every useful DSL contains a hierarchy of rules, many of which start with the keyword syntax, and MGrammar requires that the top-most rule have the name Main.

    syntax Main = "Hello, World";
    
  2. Note there are no errors and the generated output consists of a sequence that contains one element, which is the entire data string that was input. So now there is a language that recognizes exactly one input string, and rejects everything else.

  3. Now change the input (in the left-most pane) by typing additional characters. Note the errors that are generated.

Example

The following is the complete “M” code sample for this tutorial.

module Hello 
{
    language HelloLanguage 
    {
        syntax Main = "Hello, World";
    }
}

See Also

Other Resources

language (M)
ebaa3ef7-dd0e-4916-b0de-29a43bb084c7
syntax (M)