my-server
← Wiki

Interpreter pattern

In computer programming, the interpreter pattern is a design pattern that specifies how to evaluate sentences in a language. The basic idea is to have a class for each symbol (terminal or nonterminal) in a specialized computer language. The syntax tree of a sentence in the language is an instance of the composite pattern and is used to evaluate (interpret) the sentence for a client. See also Composite pattern.

Overview

The Interpreter

design pattern is one of the twenty-three well-known GoF design patterns that describe how to solve recurring design problems to design flexible and reusable object-oriented software, that is, objects that are easier to implement, change, test, and reuse.

What problems can the Interpreter design pattern solve?

Source:

  • A grammar for a simple language should be defined
  • so that sentences in the language can be interpreted.

When a problem occurs very often, it could be considered to represent it as a sentence in a simple language (Domain Specific Languages) so that an interpreter can solve the problem by interpreting the sentence.

For example, when many different or complex search expressions must be specified. Implementing (hard-wiring) them directly into a class is inflexible because it commits the class to particular expressions and makes it impossible to specify new expressions or change existing ones independently from (without having to change) the class.

What solution does the Interpreter design pattern describe?

  • Define a grammar for a simple language by defining an <code>Expression</code> class hierarchy and implementing an <code>interpret()</code> operation.
  • Represent a sentence in the language by an abstract syntax tree (AST) made up of <code>Expression</code> instances.
  • Interpret a sentence by calling <code>interpret()</code> on the AST.

The expression objects are composed recursively into a composite/tree structure that is called abstract syntax tree (see Composite pattern). <br>The Interpreter pattern doesn't describe how to build an abstract syntax tree. This can be done either manually by a client or automatically by a parser.

See also the UML class and object diagram below.

Uses

  • Specialized database query languages such as SQL.
  • Specialized computer languages that are often used to describe communication protocols.
  • Most general-purpose computer languages actually incorporate several specialized languages.

Structure

UML class and object diagram

In the above UML class diagram, the <code>Client</code> class refers to the common <code>AbstractExpression</code> interface for interpreting an expression <code>interpret(context)</code>. <br> The <code>TerminalExpression</code> class has no children and interprets an expression directly. <br> The <code>NonTerminalExpression</code> class maintains a container of child expressions (<code>expressions</code>) and forwards interpret requests to these <code>expressions</code>. <br>

The object collaboration diagram shows the run-time interactions: The <code>Client</code> object sends an interpret request to the abstract syntax tree. The request is forwarded to (performed on) all objects downwards the tree structure. <br>The <code>NonTerminalExpression</code> objects (<code>ntExpr1,ntExpr2</code>) forward the request to their child expressions. <br>The <code>TerminalExpression</code> objects (<code>tExpr1,tExpr2,&hellip;</code>) perform the interpretation directly.

UML class diagram

Example

This C++23 implementation is based on the pre C++98 sample code in the book.

The program output is:

See also

References

External links