December 22, 2014
Hot Topics:

Text Transformation with MGrammar and the Oslo SDK

  • February 17, 2009
  • By Jeffrey Juday
  • Send Email »
  • More Articles »

Figure 2 depicts the sample application running in Intellipad, a development tool shipping with the Oslo SDK.



Click here for a larger image.

Figure 2: Application Execution

Enter a typical phrase and the text is translated to MGraph targeting the Requirement model. Enter Server and the application targets the ServerConfiguration model.

I've shown what the application does. Now, I want to show how it works.

MGrammar Structure

Following is the entire MGrammar sample.

module Sample.MGrammar
{
   language ModelMGraph
   {
      syntax Main = Req | ServerConfig | Nil;

      syntax Req = InputText:MultipleTextValues =>
         Requirement { Description {InputText},
         ApplicationId  {100} };
      syntax ServerConfig = "Server="
         InputNum:MultipleTextValues =>
         ServerConfiguration { Server {InputNum},
         ApplicationId  {100} };
      //Case where nothing there without this there is an error
      syntax Nil = empty;

      token TextValue = "a".."z" | "A".."Z" | " ";
      token MultipleTextValues = (TextValue)+;
   }
}

As with other "M" programs, MGrammar applications are scoped to a module. Also like MSchema, MGrammar application can import and export libraries of Modules. As you can see, MGrammar supports comments and as I mentioned before the language syntax supports other things like, for example, preprocessor directives like #if and #define. Keep in mind, though, that the Oslo SDK is a CTP and MGrammar's current incarnation is not complete. The Language keyword begins the application definition.

Main is the application entry point. Main must always be a Syntax. Text input into the MGrammar must match a defined Syntax or the application emits an error. In the example, there are three Syntaxes defining three distinct patterns: Req, ServerConfig, and Nil. Later in the article, I'll explain how a Syntax is constructed. I want to start with the Tokens.

Token Rules

The Tokens in the sample application appear below.

token TextValue = "a".."z" | "A".."Z" | " ";
token MultipleTextValues = (TextValue)+;

Tokens look and act a lot like Regular Expressions. Like Regular Expressions, Tokens can define patterns of text, numeric ranges, whitespace characters, and operators (&, |, *, +, etc.). Tokens can also include other Tokens.

In the example, TextValue defines a pattern containing any lowercase character, uppercase character, or space. MultipleTextValues defines a pattern matching one or more alphabetic characters.

Syntax Rules

The application Syntaxes appear below.

syntax Main = Req | ServerConfig | Nil;

syntax Req = InputText:MultipleTextValues =>
   Requirement { Description {InputText}, ApplicationId  {100} };
syntax ServerConfig = "Server="
   InputNum:MultipleTextValues =>
   ServerConfiguration { Server {InputNum}, ApplicationId  {100} };
//Case where nothing there without this there is an error
syntax Nil = empty;

Syntaxes handle the input and resulting MGraph output, also called a Projection. Everything to the right of the "=>" symbol is the Projection (output text).

Syntaxes can include scoped variables. Variables can be used in the Projection. Syntaxes can be composed from multiple Syntaxes and Tokens.

In the example Req uses a variable called InputText. InputText contains the full text passed into the application and is used in the MGraph output. ServerConfig utilizes a variable called InputNum in a similar fashion.

Nil is a special case Syntax. If nothing is passed into the application an empty Projection is generated. Empty is a MGrammar keyword.

Main includes all of the defined Syntaxes. As I mentioned before, Main is the entry point of the application.





Page 2 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel