Writing generators

Hello! As part of my thesis I'm creating an example DSL in MPS. It's a really nice tool, I must admit, I really like it. My language is basically a notation for designing questionnaires, which have sections, questions and so on. The questions have different types. I'm not sure what is the preferred way for doing this, but in places where normally in EBNF I would say now comes BooleanQuestion or MultipleChoiceQuestion and so on I used concept inheritance in MPS. Now I am trying to write a generator and I'm not sure how to cast questions from the general concept type to the concrete subclass and call the proper template to instantiate the appropriate Java object. What I would like to do is loop over all questions in a particular section and branch the generation for different types. I suppose I should somehow use mapping rules or switch, but I can't seem to figure out how. Can anyone help, please?

Comment actions Permalink


you are right, there are two ways of doing this.


Create a LOOP macro and a SWITCH macro inside of it.

Create a "template switch" and specify it in SWITCH macro.

Create 2 cases in template switch - one per non-abstract concept

Create a template for each of the cases (the simpliest way is to invoke an intention)

Inside each of the templates "node" will be of the concept you want (as the templates are for BooleanQ and MultipleChoiceQ, the "node" expression inside of them will have a type of node<BooleanQ> and node<MultipleChoiceQ>, respectiely). So, you won't need explicit typecasts.

I've added an attachment with an illustration of what I'm talking about.


Create a LOOP macro and a COPY_SRC macro inside of it

Create 2 reduction rules - one per concrete question type

As in (1), no explicit typecasts will be needed in templates


If you will have to use typecasts in the future, you can use


or a more-DSL notation

myNode : MyConcreteConcept

which means exactly the same thing

Comment actions Permalink

Thank you for taking the time to write a great and detailed answer, Mihail. I will try what you suggest tommorow morning and check back with the results.


Please sign in to leave a comment.