encyclopegia.org

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.

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 Expression class hierarchy and implementing an interpret() operation. Represent a sentence in the language by an abstract syntax tree (AST) made up of Expression instances. Interpret a sentence by calling interpret() on the AST. The expression objects are composed recursively into a composite/tree structure that is called abstract syntax tree (see Composite pattern). 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. import std; using String = std::string; template using TreeMap = std::map ; template using UniquePtr = std::unique_ptr ; class BooleanExpression { public: BooleanExpression() = default; virtual ~BooleanExpression() = default; virtual bool evaluate(Context&) = 0; virtual UniquePtr replace(String&, BooleanExpression&) = 0; virtual UniquePtr copy() const = 0; }; class VariableExpression; class Context { private: TreeMap m; public: Context() = default; nodiscard bool lookup(const VariableExpression* key) const { return m.at(key); } void assign(VariableExpression* key, bool value) { m[key] = value; } }; class VariableExpression : public BooleanExpression { private: String name; public: VariableExpression(const String& name): name{name} {} virtual ~VariableExpression() = default; nodiscard virtual bool evaluate(Context& context) const { return context.lookup(this); } nodiscard virtual UniquePtr replace(const String& name, BooleanExpression& exp) { if (this->name == name) { return std::make_unique (exp.copy()); } else { return std::make_unique (name); } } nodiscard virtual UniquePtr copy() const { return std::make_unique (name); } VariableExpression(const VariableExpression&) = delete; VariableExpression& operator=(const VariableExpression&) = delete; }; class AndExpression : public BooleanExpression { private: UniquePtr operand1; UniquePtr operand2; public: AndExpression(UniquePtr op1, UniquePtr op2): operand1{std::move(op1)}, operand{std::move(op2)} {} virtual ~AndExpression() = default; nodiscard virtual bool evaluate(Context& context) const { return operand1->evaluate(context) && operand2->evaluate(context); } nodiscard virtual UniquePtr replace(const String& name, BooleanExpression& exp) const { return std::make_unique ( operand1->replace(name, exp), operand2->replace(name, exp) ); } nodiscard virtual UniquePtr copy() const { return std::make_unique (operand1->copy(), operand2->copy()); } AndExpression(const AndExpression&) = delete; AndExpression& operator=(const AndExpression&) = delete; }; int main(int argc, char* argv[]) { UniquePtr expression; Context context; UniquePtr x = std::make_unique ("X"); UniquePtr y = std::make_unique ("Y"); UniquePtr expression; = std::make_unique (x, y); context.assign(x.get(), false); context.assign(y.get(), true); bool result = expression->evaluate(context); std::println("{}", result); context.assign(x.get(), true); context.assign(y.get(), true); result = expression->evaluate(context); std::println("{}", result); return 0; } The program output is: 0 1

References

No reference rows published for this article.

Cite this article

MLA

"Interpreter pattern." encyclopegia.org, 2026, https://encyclopegia.org/wiki/interpreter-pattern. Accessed 24 November 2026.

Chicago

"Interpreter pattern." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/interpreter-pattern.

APA

encyclopegia.org. (2026). Interpreter pattern. Retrieved November 24, 2026, from https://encyclopegia.org/wiki/interpreter-pattern

Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON

Related articles

Time Bandits

enwiki · references available

Battle of Bautzen (1813)

enwiki · references available

Battle of Dresden

enwiki · references available

Down (band)

enwiki · references available

Glass fiber

enwiki · references available

Peter the Hermit

enwiki · references available

Howl (poem)

enwiki · references available

Jesus Seminar

enwiki · references available

Little Orphan Annie

enwiki · references available

Juan Rodríguez Cabrillo

enwiki · references available

Seán T. O'Kelly

enwiki · references available

1947 in music

enwiki · references available
CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1348036781 · 0.5 · snapshot enwiki-001348036781

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.