Srinivas Sampath

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

November 2008 - Posts

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!

Oslo

The first thing that I started understanding about was Oslo. Oslo is a platform for model driven applications. There are a host of sessions around Olso and a web site at MSDN dedicated for the same: http://msdn.microsoft.com/hi-in/oslo/default(en-us).aspx. From here, you can download the Oslo SDK.

The first step in getting over Oslo was the installation of the M language. I had written an earlier blog post about single letter languages making a come back and here is another one :-) Before poking around the M language (which I will write in another post), I had to install the SDK.

As all installations tend to fail, my first attempt at installing the SDK failed by not creating the repository for the models in the SQL Server. I had a named instance of SQL Server 2008. Some poking around in the forums for Oslo revealed that this was a common issue, but finally, I managed to sift through all of them and address my specific issue, which I will explain here.

Basically, during installation, because of the named instance of SQL Server, the installer was not able to create the default model repository database and upload some files into it. When I started doing this manually, although I had given a custom connection string, it still tried to reach the domain controller for authenticating logon and this failed, as I was working offline. Thus, the creation of the database manually also failed.So it looks like I needed to be on the network. The manual commands for creating the repository look like the following:

CreateRepository /cs:"Server=<your server>;User Id=user;Password=pass" /db:<instance name>

Thus successfully creates a database by name Repository and puts in a whole host of stuff into it. Next was to upload a model image and this command worked well.

Mx /i <image file.mx> -db:<database name> -s:<instance> -u:user -p:password

So after both these, I was up and running.

Oslo also comes with a new text editor called Intellipad that allows you to create models. Not sure why the team did not just integrate it into Visual Studio, but the editor today is quite primitive. So in the long run, it may get sophisticated.

Now, for creating some models....

PDC 2008

Recently, I was browsing all the recordings of the various sessions from PDC 2008 and few of them are really interesting and am spending some time learning more about them. They are:

  • Windows Azure
  • Velocity
  • Oslo
  • CCR and DSS Toolkit

I will be blogging about some of these as I get to try them and see their usage.