How Does MPS Work?

Dialects and languages that are subject-specific help people communicate accurately and effectively about their respective subjects. MPS brings the same flexibility to the world of programming languages. Unlike traditional programming languages with strict syntax and semantics, MPS makes it possible for a language to be created from scratch, altered, and extended by its users.

The core problem of extending languages lies in the parsers, which analyze strings to build data structures. Parsers restrict the code-persistent representation to a single notation, which means they cannot be easily combined with other parsers, preventing language modularization.

Implementing the non-textual presentation of program code is the alternative most commonly proposed to address this problem. A major benefit of this approach is that it eliminates the need for parsing. In MPS however, we propose another alternative. Our solution is to always have code maintained in what is known as an Abstract Syntax Tree (AST), a data structure that consists of nodes with a set of attributes (properties, children, and references) that fully describe the program code.

The task of the MPS editor then is to visualize the AST in a user-friendly way and to provide the means for editing it effectively. For example, if you are creating a language that simulates a standard textual language, the editor should provide an experience that replicates that of using a common text editor. Similarly, if you are creating a language with graphical notations, the editor should provide the same experience you would have using a diagramming editor.

When creating a language in MPS, you define the rules for editing the code and specify how the code is rendered to the user. You can also specify the language’s type system and constraints, as a set of rules applied to your language. Combined, these characteristics allow MPS to check program code on the fly, and they make programming with the new language easy and less susceptible to errors.

MPS uses a generative approach. This means you can define generators for your language to transform end-user input into a more conventional, typically general-purpose language. Currently, MPS is particularly good at but is not limited to, generating Java code. You can also generate C, C#, XML, FHTML, PDF, LaTeX, JavaScript, and more.

Watch video tutorial

What are Domain-Specific Languages (DSL)?

“Programming languages that raise the level of abstraction beyond programming by specifying the solution that directly uses concepts and rules from a specific problem domain.”

Kelly and Tolvanen (2008)

With MPS you can define custom editors for a new language and make using these DSLs simpler. Even domain experts, who are not familiar with traditional programming, can easily work in MPS with domain-specific languages designed around their domain-specific terminology.

Projection Editor

A projectional editor allows the user to edit the Abstract syntax tree (AST) representation of code in an efficient way. It can mimic the behavior of a textual editor for textual notations, a diagram editor for graphical languages, a tabular editor for editing tables and so on. The user interacts with the code through intuitive on screen visuals which they can even switch between for multiple displays of the same code.

Watch video tutorial

IDE Support

JetBrains is well known for its high-performance IDEs and development tools, and MPS is no exception.

MPS is a powerful IDE with all you need to take full advantage of DSLs. Some of the features included are: Code completion, navigation, refactoring, error checking, quick fixes, debugging of DSLs, language versioning with automatic migrations and integration with mainstream version control systems.

Watch video tutorial

Code Generation

Bridging the semantic gap between the business domain and the implementation domain is handled by the MPS generator.

Through a series of steps, the generator gradually transforms the original domain-specific model into a model represented in a low-level general-purpose language, such as Java, C, JavaScript, or XML. The resulting model may then be further transformed into textual source files, which can be fed into traditional compilers in order to generate runnable binaries.

While a domain-specific language formalizes the business knowledge of the domain experts, the generator encapsulates the implementation of that knowledge in a given technology. The generator preserves the expertise of the most senior software developers in a reusable form and lets others benefit from it.

The clear separation of the business knowledge, contained in the language, and the implementation knowledge, held in the generator, allows users to target multiple platforms at the same time by using multiple generators for the same language. Users can also save on efforts when switching the target implementation technology, since only the generator needs to be changed. The business knowledge contained in the existing domain-specific models can remain intact.

MPS for Creating DSLs and Language Extensions

MPS originally included a ready-to-use universal language called BaseLanguage that was a copy of Java. Over time additional languages have been added, like XML, C, and JavaScript. These can themselves be used to create programs. However, the real strength of MPS is when it is used to extend BaseLanguage or other universal languages and create new languages on top of them. MPS comes with several helpful extensions to BaseLanguage for working with strings, collections, dates, regular expressions, etc. You can also use them as a reference for creating new languages. These powerful tools make MPS quite proficient as an instrument for creating Domain-Specific Languages (DSL).