IgorShare Thoughts and Ideas

Consulting and Training

Archive for March 23rd, 2009

Oslo: Basic M grammar for a generic Language

Posted by Igor Moochnick on 03/23/2009

I’ve noticed that each time when I need to create yet another language definition or some rules grammar, I write the same (or a very similar) “prelude”. I guess that you do the same thing. To simplify the process I’ve crystallized this basis into a simple module. It contains the definition of a Whitespaces, Single- and Multi-line comments and a commands list. You can reuse this module by importing it into your language definition.

Here is the example of a very simple language:

// This is a comment


/* This is a multi-
line comment */


The grammar is pretty trivial (if you reuse the foundation grammar – note the import in the beginning):

module SomeLanguageModule
    import LanguageBaseModule { Common as C };

    language RulesDefinition
        // Delimiters
        token commandDelimiter = ";";

        token Command = c:(C.AlphaNum | "_")+;

        // The Program
        syntax Main
            = C.CommandList(Command, commandDelimiter);

        // Ignore whitespace
        interleave Whitespace = C.Whitespace | C.Comment | C.MultiLineComment;

The middle section of the grammar  (Command) is where you concentrate your development efforts. Make sure to replace the Command “token” with Command “syntax”.

The Tree result from this grammar and the sample, shown at the top of this post, looks like this:


Download the sample language and the grammar from my SkyDrive.

See the LanguageBaseModule grammar after the break …

Read the rest of this entry »


Posted in M, Oslo | Leave a Comment »

%d bloggers like this: