After defining structure and editor for your language you may want to face another challenge: create a generator for your language. MPS generator framework greatly simplifies this boring error-prone task. You don't have to deal with Velocity or any other template solution any more. All that you have to do is to create several templates add rules to mappings, create several query methods and enjoy your language. Each generator in MPS consists of rules and templates. Let's consider them in detail.
If you have worked with a code generation solution other than MPS you may be familiar with a
notion of template. It makes possible to separate target language details from a generator.
In templates you write code in a target language. But in some places you may insert macros with additional information
that will be replaced with some information from your generator by generator framework. The same approach is used
in MPS. You have to write code in your target language and mark some parts of it with macros. Lets consider
Concept declaration template from Structure Language generator:
All macros contain a dollar sign. One macro you can see in the figure above is a property macro (It starts with $).
Property macros are replaced during gneration with some property values. Here the property macro sets Concept class
stub name. Another one is a reference macro (starts with ->$).
Reference macros are replaced in generated code with some references to nodes.
This reference macro finds parent concept's stub class and sets it as a parent
class for our class stub. Each macro is associated with one or several query methods that supply it with
a value that will be set.
In templates, we have a lot of different kinds of macros. The most important of them are:

There are two kinds of templates. The simplest one is when you just create a concept of target language
and mark it with macros. We've already considered this kind of templates when we spoke about Concept Declaration
stub template. Another kind of template is called template fragment. We use template fragments when we want
to modify a concept instance already partly generated. They are used for example in weaving and reduction rules. Lets consider
template used in a rule that generates getters and setters for a concept stub class:

Inside this template fragment, we declared class and marked some parts of it with <TF ... TF>
brackets. When template is applied the code inside the brackets will be inserted, but the code outside of the
brackets won't be inserted, it is used for another purpose: to declare context.
When applying template fragments, the generator framework will try to resolve variables by their names.
So if you declared some variable in a template fragment context and use it inside template fragment, it will be
resolved when template is inserted into the context (if variable with such name exists, otherwise an error will be displayed).
We have several kinds of generator rules:
Generation in MPS is performed with an aid of those Node Builders. Node Builders know how to build nodes from a source model. Node builders can have child node builders that will build child nodes of a node that will be created by parent node builder. Lets consider how a node builder will be created from different kinds of rules:
ITemplateGenerator.findNodeBuilderForSource(SNode)Use this method if your generator creates only one node builder from specified SNode.
ITemplateGenerator.findNodeBuilderForSource(SNode, String)Use this method if your generator creates several node builders from specified SNode. In this case you have to mark a rule with a string (rule name). You have to pass this string as a second parameter.