ASTs, Models and Meta-Models
Language engineers solve challenging system or software modeling problems by creating domain-specific languages and models. Language engineers are usually part of a multidisciplinary team that includes team members and subject matter experts from the client side. This invariably means that language engineers gain deep insights into many different areas.
When working with language engineering and domain-specific languages (DSLs), you may encounter terms like “model” and “meta-model”. These terms are used to describe different aspects of a language and its representation. Let’s explore what these terms mean and how they relate to each other.
What is a Model?
In the context of language engineering:
-
A model is an instance of a language. It represents a concrete example of the language’s syntax and semantics.
-
For example, if you have a DSL for defining greetings, a specific file like:
greet "Alice" greet "Bob"
is a model. It conforms to the rules defined by the language’s grammar and represents a specific use case.
In Langium, the AST (Abstract Syntax Tree) is often referred to as a model because it is a structured representation of the concrete syntax (the actual text) of a program or DSL instance.
What is a Meta-Model?
A meta-model is a higher-level abstraction that defines the structure and rules of a language. It describes what constitutes a valid model.
- In Langium, the grammar (e.g., the
.langium
file) acts as the meta-model. It defines the rules for constructing valid ASTs (models). - For example, the grammar rule:
is part of the meta-model. It specifies that a valid
Greeting: 'greet' name=STRING;
Greeting
in the model must consist of the keywordgreet
followed by a string.
The file src/language/generated/ast.ts
in a Langium project contains the AST classes generated from the grammar (using npx langium generate
). These classes represent the structure of the language and are used to build the ASTs (models) during parsing. It is a TypeScript representation of the meta-model.
- Model: Represents a specific instance of the language (e.g., a specific program or DSL file).
- Meta-Model: Represents the definition of the language itself (e.g., the grammar rules).
Alternative Terminology
If the terms “model” and “meta-model” feel too abstract, you can use more concrete terms depending on the context:
- AST: Always appropriate when referring to the parsed structure of a program or DSL.
- Grammar: Appropriate when referring to the language definition.
- Instance: Can be used instead of “model” to refer to a specific use of the language.
See Also
See also Grammars, Meta-Models, and Ontologies for a broader comparison of these concepts.