First Mg program
May 15, 2009
Well, lets get started with some Mgrammer. I’ve been given a piece of work to do and I believe Mg can provide me a solution using minimal effort.
The problem – our company provides software that looks up words in a selection of dictionaries. However we want to allow users to enter their own custom words in the dictionary and to have these returned in preference to any other dictionary. This is to be a web based tool and I have to provide UI, business logic and storage for the new words. Each word can have multiple definitions associated with it and each definition can have multiple associated words or inflections.
My plan is to use Mgrammer to define a language for the entry of new words, definitions, and inflections then translate that into actual database tables and associated logic within the Oslo repository. The work of creating and populating the database will hopefully be done for me by Oslo.
The solution!
If you are reading this blog I’m assuming that you have already installed the Oslo SDK and have read through some of the getting started docs. I’m using the intellipad provided with the Oslo SDK as this seems to be the best tool available.
As I’m just learning this myself I hope to present each step of the process in a series of short posts on this blog, in this first post I will write some grammer that allows the user to enter a series of words.
Here is some input I want to process:
NewWord:Hello; NewWord:World;
Each new word is prefixed with “NewWord:” and they are separated by a semi colon. There can be any amount of space or newline characters between each word.
Below is some M, I draw your attention to the main rule which is “syntax Main = (Word WordText WordSeparator)+;”
- Main, WordText and WordSeparator are all tokens that we define further down.
- the + means that we must have 1 or more occurances of the whole pattern in the brackets.
module Dictionary
{
language CustomDictionary
{
// Require one or more words separated by semicolon
syntax Main = (Word WordText WordSeparator)+;
// This token will be used to prefix a word declaration
token Word = “NewWord:”;
// This token will be used to separate words
token WordSeparator = “;”;
/* This token describes a valid word, we will allow
upper and lower case letters as well as – and ‘
*/
token WordText = (“a”..”z” | “A”..”Z” | “-” | “‘”)+;
// Add an interleave rule to ignore whitespace between words
syntax LF = “\u000A”;
syntax CR = “\u000D”;
syntax Space = “\u0020″;
interleave Whitespace = LF | CR | Space;
}
}
This will then translate our input into the following:
Main[
[
[
"NewWord:",
"Hello",
";"
],
[
"NewWord:",
"World",
";"
]
]
]
Which hopefully when this project is finished can be used to populate a table in the Oslo Repository.
That’s it for now, in the next post I will expand the language to allow definitions to be added.
L8r