Srinivas Sampath

from t in myThoughts where myThoughts.Thoughts = "Technology" select t

Simple MGrammar

MGrammar is part of the Oslo SDK and allows the definition of grammar files that can be used to thus define textual domain specific languages. For example, you can write a mgrammar file to understand musical notes in a given format (which is also one of the example that is present in MSDN). I wanted to see how a typical mgrammar file might look like (with not too much frills).

I wanted to write a mgrammar file to understand complex numbers. Complex numbers consist of a real number and an impaginary number and is of the form: x + yi. You can read more about complex numbers from this location. I also wanted to ensure that I have one complex number per line of input.

To begin writing this grammar:

·    Start Intellipad with samples enabled

·    First create a file called ComplexNumbers.mg and then save the same

·    I had to restart Intellipad at this time and then press Ctrl+Shift+T to open the tree view mode

·    Choose the file created above

·    You will now see 3 panes: an input pane, a grammar pane and a treeview pane

You can now author the grammar file. Here is the grammar that I wrote:

module ComplexNumbers {
    language myComplexNumbers {
        // Allow 1 or more complex numbers to be defined
        syntax Main = complexNumber+;
       
        // Define the structure of a typical complex number
        syntax complexNumber = realPart "+" imaginaryPart "i";
       
        // Define the various tokens that make up the complex number
        token realPart = Digits#1..2;
        token imaginaryPart = Digits#1..2;
        token Digits = "0".."9";
       
        // Ignore any noise characters
        interleave whiteSpace = " " | " " | " ";
    }
}

You can take a structured approach to build a grammar file. In our case, the following logic will help us arrive at the grammar file:

·         A complex number is made up of a real part and an imaginary part. This definition is present in the line:

// Define the structure of a typical complex number
syntax complexNumber = realPart "+" imaginaryPart "i";

·         The realPart and imaginaryPart references are tokens which are defined in the next line as follows:

// Define the various tokens that make up the complex number
token realPart = Digits#1..2;
token imaginaryPart = Digits#1..2;
token Digits = "0".."9";

·         We have specified that the token Digits further define the real part and imaginary part. The Digits token indicates that it is a sequence of 2 numbers ranging from 0 to 9

·         Finally, we want the user to enter arbitrary white space characters in the representation. The interleave command allows us to specify characters that need to be ignored

Once you type this grammar, you can specify different types of inputs in the leftmost pane. Some examples are:

1.     3 + 4i

2.     2+3i

3.     13 + 17 i

As you keep entering these valid sequences, the rightmost pane will display a directed graph type of output of the results as shown:

Main[

  [

    complexNumber[

      "17",

      "+",

      "13",

      "i"

    ]

  ]

]

 

If you enter anything invalid, for example: A + Bi, you will see that an error is raised in the error pane below.

 

This was an example of a very simple MGrammar for parsing complex numbers. In subsequent posts, we will go deeper into some more features. Have fun!