<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://debianws.lexgopc.com/wiki1463/index.php?action=history&amp;feed=atom&amp;title=Interpreter_pattern</id>
	<title>Interpreter pattern - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://debianws.lexgopc.com/wiki1463/index.php?action=history&amp;feed=atom&amp;title=Interpreter_pattern"/>
	<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki1463/index.php?title=Interpreter_pattern&amp;action=history"/>
	<updated>2026-12-01T11:53:35Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.46.3</generator>
	<entry>
		<id>http://debianws.lexgopc.com/wiki1463/index.php?title=Interpreter_pattern&amp;diff=118945&amp;oldid=prev</id>
		<title>imported&gt;Squawk7700: Interceptor: Reverting non-constructive edits by ~2026-22219-79</title>
		<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki1463/index.php?title=Interpreter_pattern&amp;diff=118945&amp;oldid=prev"/>
		<updated>2026-04-10T09:17:58Z</updated>

		<summary type="html">&lt;p&gt;&lt;a href=&quot;/wiki1463/index.php?title=WP:CVPI&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new&quot; title=&quot;WP:CVPI (page does not exist)&quot;&gt;Interceptor&lt;/a&gt;: Reverting non-constructive edits by &lt;a href=&quot;/wiki1463/index.php?title=Special:Contributions/~2026-22219-79&quot; title=&quot;Special:Contributions/~2026-22219-79&quot;&gt;~2026-22219-79&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Short description|Approach in computer programming}}&lt;br /&gt;
{{Refimprove|date=November 2008}}&lt;br /&gt;
&lt;br /&gt;
In [[computer programming]], the &amp;#039;&amp;#039;&amp;#039;interpreter pattern&amp;#039;&amp;#039;&amp;#039; is a [[design pattern (computer science)|design pattern]] that specifies how to evaluate sentences in a language.&lt;br /&gt;
The basic idea is to have a [[Class (computer science)|class]] for each symbol ([[Terminal symbol|terminal]] or [[Nonterminal symbol|nonterminal]]) in a [[Domain specific languages|specialized computer language]]. The [[abstract syntax tree|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.&amp;lt;ref name=GoF&amp;gt;{{cite book |author1=Gamma, Erich |author2-link=Richard Helm |author2=Helm, Richard |author3=Johnson, Ralph |author4=Vlissides, John |&lt;br /&gt;
 title=Design Patterns: Elements of Reusable Object-Oriented Software |&lt;br /&gt;
 publisher=Addison-Wesley |&lt;br /&gt;
 year=1994 |&lt;br /&gt;
 isbn=0-201-63361-2&lt;br /&gt;
 |author1-link=Erich Gamma |title-link=Design Patterns }}&amp;lt;/ref&amp;gt;{{rp|243}} See also [[Composite pattern]].&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
The Interpreter&lt;br /&gt;
&amp;lt;ref name=&amp;quot;GoF2&amp;quot;&amp;gt;{{cite book|author=Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides|title=Design Patterns: Elements of Reusable Object-Oriented Software|year=1994|publisher=Addison Wesley|isbn=0-201-63361-2|pages=[https://archive.org/details/designpatternsel00gamm/page/243 243ff]|url-access=registration|url=https://archive.org/details/designpatternsel00gamm/page/243}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
design pattern is one of the twenty-three well-known &lt;br /&gt;
&amp;#039;&amp;#039;[[Design Patterns|GoF design patterns]]&amp;#039;&amp;#039; &lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
===What problems can the Interpreter design pattern solve?===&lt;br /&gt;
&lt;br /&gt;
Source:&amp;lt;ref&amp;gt;{{cite web|title=The Interpreter design pattern - Problem, Solution, and Applicability|url=http://w3sdesign.com/?gr=b03&amp;amp;ugr=proble|website=w3sDesign.com|access-date=2017-08-12}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* A [[Backus-Naur form|grammar]] for a simple language should be defined&lt;br /&gt;
* so that sentences in the language can be interpreted.&lt;br /&gt;
&lt;br /&gt;
When a problem occurs very often, it could be considered to represent it as a sentence in a simple language&lt;br /&gt;
([[Domain-specific language|Domain Specific Languages]]) so that an interpreter can solve the problem&lt;br /&gt;
by interpreting the sentence.&lt;br /&gt;
&lt;br /&gt;
For example, when many different or complex search expressions must be specified.&lt;br /&gt;
Implementing (hard-wiring) them directly into a class is inflexible&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
===What solution does the Interpreter design pattern describe?===&lt;br /&gt;
* Define a grammar for a simple language by defining an &amp;lt;code&amp;gt;Expression&amp;lt;/code&amp;gt; class hierarchy and implementing an &amp;lt;code&amp;gt;interpret()&amp;lt;/code&amp;gt; operation.&lt;br /&gt;
* Represent a sentence in the language by an abstract syntax tree (AST) made up of &amp;lt;code&amp;gt;Expression&amp;lt;/code&amp;gt; instances.&lt;br /&gt;
* Interpret a sentence by calling &amp;lt;code&amp;gt;interpret()&amp;lt;/code&amp;gt; on the AST.&lt;br /&gt;
&lt;br /&gt;
The expression objects are composed recursively into a composite/tree structure that is called&lt;br /&gt;
&amp;#039;&amp;#039;abstract syntax tree&amp;#039;&amp;#039; (see [[Composite pattern]]).&lt;br /&gt;
&amp;lt;br&amp;gt;The Interpreter pattern doesn&amp;#039;t describe how&lt;br /&gt;
to build an abstract syntax tree. This can&lt;br /&gt;
be done either manually by a client or automatically by a [[parser]].&lt;br /&gt;
&lt;br /&gt;
See also the UML class and object diagram below.&lt;br /&gt;
&lt;br /&gt;
== Uses ==&lt;br /&gt;
* Specialized database query languages such as [[SQL]].&lt;br /&gt;
* Specialized computer languages that are often used to describe communication protocols.&lt;br /&gt;
* Most general-purpose computer languages actually incorporate several specialized languages{{Citation needed|reason=Need reference to a language doing this|date=September 2022}}.&lt;br /&gt;
&lt;br /&gt;
== Structure ==&lt;br /&gt;
=== UML class and object diagram ===&lt;br /&gt;
[[File:w3sDesign Interpreter Design Pattern UML.jpg|frame|none|A sample UML class and object diagram for the Interpreter design pattern.&amp;lt;ref&amp;gt;{{cite web|title=The Interpreter design pattern - Structure and Collaboration|url=http://w3sdesign.com/?gr=b03&amp;amp;ugr=struct|website=w3sDesign.com|access-date=2017-08-12}}&amp;lt;/ref&amp;gt;]]&lt;br /&gt;
&lt;br /&gt;
In the above [[Unified Modeling Language|UML]] [[class diagram]], the &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; class refers to the common &amp;lt;code&amp;gt;AbstractExpression&amp;lt;/code&amp;gt; interface for interpreting an expression&lt;br /&gt;
&amp;lt;code&amp;gt;interpret(context)&amp;lt;/code&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;TerminalExpression&amp;lt;/code&amp;gt; class has no children and interprets an expression directly.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;NonTerminalExpression&amp;lt;/code&amp;gt; class maintains a container of child expressions&lt;br /&gt;
(&amp;lt;code&amp;gt;expressions&amp;lt;/code&amp;gt;) and forwards interpret requests&lt;br /&gt;
to these &amp;lt;code&amp;gt;expressions&amp;lt;/code&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The object collaboration diagram &lt;br /&gt;
shows the run-time interactions: The &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; object sends an interpret request to the abstract syntax tree.&lt;br /&gt;
The request is forwarded to (performed on) all objects downwards the tree structure.&lt;br /&gt;
&amp;lt;br&amp;gt;The &amp;lt;code&amp;gt;NonTerminalExpression&amp;lt;/code&amp;gt; objects (&amp;lt;code&amp;gt;ntExpr1,ntExpr2&amp;lt;/code&amp;gt;) forward the request to their child expressions.&lt;br /&gt;
&amp;lt;br&amp;gt;The &amp;lt;code&amp;gt;TerminalExpression&amp;lt;/code&amp;gt; objects (&amp;lt;code&amp;gt;tExpr1,tExpr2,&amp;amp;hellip;&amp;lt;/code&amp;gt;) perform the interpretation directly.&lt;br /&gt;
&lt;br /&gt;
=== UML class diagram ===&lt;br /&gt;
[[Image:Interpreter_UML_class_diagram.svg]]&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&amp;lt;!-- Wikipedia is not a list of examples. Do not add examples from favorite programming languages here; this page exists to explain the design pattern, not to show how it interacts with subtleties of every language extant. Feel free to add examples at: http://en.wikibooks.org/wiki/Computer_Science_Design_Patterns/Interpreter --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This C++23 implementation is based on the pre C++98 sample code in the book.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
import std;&lt;br /&gt;
&lt;br /&gt;
using String = std::string;&lt;br /&gt;
template &amp;lt;typename K, typename V&amp;gt;&lt;br /&gt;
using TreeMap = std::map&amp;lt;K, V&amp;gt;;&lt;br /&gt;
template &amp;lt;typename T&amp;gt;&lt;br /&gt;
using UniquePtr = std::unique_ptr&amp;lt;T&amp;gt;;&lt;br /&gt;
&lt;br /&gt;
class BooleanExpression {&lt;br /&gt;
public:&lt;br /&gt;
    BooleanExpression() = default;&lt;br /&gt;
    virtual ~BooleanExpression() = default;&lt;br /&gt;
    virtual bool evaluate(Context&amp;amp;) = 0;&lt;br /&gt;
    virtual UniquePtr&amp;lt;BooleanExpression&amp;gt; replace(String&amp;amp;, BooleanExpression&amp;amp;) = 0;&lt;br /&gt;
    virtual UniquePtr&amp;lt;BooleanExpression&amp;gt; copy() const = 0;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class VariableExpression;&lt;br /&gt;
&lt;br /&gt;
class Context {&lt;br /&gt;
private:&lt;br /&gt;
    TreeMap&amp;lt;const VariableExpression*, bool&amp;gt; m;&lt;br /&gt;
public:&lt;br /&gt;
    Context() = default;&lt;br /&gt;
&lt;br /&gt;
    [[nodiscard]]&lt;br /&gt;
    bool lookup(const VariableExpression* key) const { &lt;br /&gt;
        return m.at(key); &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    void assign(VariableExpression* key, bool value) { &lt;br /&gt;
        m[key] = value; &lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class VariableExpression : public BooleanExpression {&lt;br /&gt;
private:&lt;br /&gt;
    String name;&lt;br /&gt;
public:&lt;br /&gt;
    VariableExpression(const String&amp;amp; name):&lt;br /&gt;
        name{name} {}&lt;br /&gt;
&lt;br /&gt;
    virtual ~VariableExpression() = default;&lt;br /&gt;
&lt;br /&gt;
    [[nodiscard]]&lt;br /&gt;
    virtual bool evaluate(Context&amp;amp; context) const {&lt;br /&gt;
        return context.lookup(this);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    [[nodiscard]]&lt;br /&gt;
    virtual UniquePtr&amp;lt;VariableExpression&amp;gt; replace(const String&amp;amp; name, BooleanExpression&amp;amp; exp) {&lt;br /&gt;
        if (this-&amp;gt;name == name) {&lt;br /&gt;
            return std::make_unique&amp;lt;VariableExpression&amp;gt;(exp.copy());&lt;br /&gt;
        } else {&lt;br /&gt;
            return std::make_unique&amp;lt;VariableExpression&amp;gt;(name);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    [[nodiscard]]&lt;br /&gt;
    virtual UniquePtr&amp;lt;BooleanExpression&amp;gt; copy() const {&lt;br /&gt;
        return std::make_unique&amp;lt;BooleanExpression&amp;gt;(name);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    VariableExpression(const VariableExpression&amp;amp;) = delete;&lt;br /&gt;
    VariableExpression&amp;amp; operator=(const VariableExpression&amp;amp;) = delete;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class AndExpression : public BooleanExpression {&lt;br /&gt;
private:&lt;br /&gt;
    UniquePtr&amp;lt;BooleanExpression&amp;gt; operand1;&lt;br /&gt;
    UniquePtr&amp;lt;BooleanExpression&amp;gt; operand2;&lt;br /&gt;
public:&lt;br /&gt;
    AndExpression(UniquePtr&amp;lt;BooleanExpression&amp;gt; op1, UniquePtr&amp;lt;BooleanExpression&amp;gt; op2):&lt;br /&gt;
        operand1{std::move(op1)}, operand{std::move(op2)} {}&lt;br /&gt;
&lt;br /&gt;
    virtual ~AndExpression() = default;&lt;br /&gt;
&lt;br /&gt;
    [[nodiscard]]&lt;br /&gt;
    virtual bool evaluate(Context&amp;amp; context) const {&lt;br /&gt;
        return operand1-&amp;gt;evaluate(context) &amp;amp;&amp;amp; operand2-&amp;gt;evaluate(context);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    [[nodiscard]]&lt;br /&gt;
    virtual UniquePtr&amp;lt;BooleanExpression&amp;gt; replace(const String&amp;amp; name, BooleanExpression&amp;amp; exp) const {&lt;br /&gt;
        return std::make_unique&amp;lt;AndExpression&amp;gt;(&lt;br /&gt;
            operand1-&amp;gt;replace(name, exp),&lt;br /&gt;
            operand2-&amp;gt;replace(name, exp)&lt;br /&gt;
        );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    [[nodiscard]]&lt;br /&gt;
    virtual UniquePtr&amp;lt;BooleanExpression&amp;gt; copy() const {&lt;br /&gt;
        return std::make_unique&amp;lt;AndExpression&amp;gt;(operand1-&amp;gt;copy(), operand2-&amp;gt;copy());&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    AndExpression(const AndExpression&amp;amp;) = delete;&lt;br /&gt;
    AndExpression&amp;amp; operator=(const AndExpression&amp;amp;) = delete;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char* argv[]) {&lt;br /&gt;
    UniquePtr&amp;lt;BooleanExpression&amp;gt; expression;&lt;br /&gt;
    Context context;&lt;br /&gt;
    UniquePtr&amp;lt;VariableExpression&amp;gt; x = std::make_unique&amp;lt;VariableExpression&amp;gt;(&amp;quot;X&amp;quot;);&lt;br /&gt;
    UniquePtr&amp;lt;VariableExpression&amp;gt; y = std::make_unique&amp;lt;VariableExpression&amp;gt;(&amp;quot;Y&amp;quot;);&lt;br /&gt;
    UniquePtr&amp;lt;BooleanExpression&amp;gt; expression; = std::make_unique&amp;lt;AndExpression&amp;gt;(x, y);&lt;br /&gt;
&lt;br /&gt;
    context.assign(x.get(), false);&lt;br /&gt;
    context.assign(y.get(), true);&lt;br /&gt;
    bool result = expression-&amp;gt;evaluate(context);&lt;br /&gt;
    std::println(&amp;quot;{}&amp;quot;, result);&lt;br /&gt;
&lt;br /&gt;
    context.assign(x.get(), true);&lt;br /&gt;
    context.assign(y.get(), true);&lt;br /&gt;
    result = expression-&amp;gt;evaluate(context);&lt;br /&gt;
    std::println(&amp;quot;{}&amp;quot;, result);&lt;br /&gt;
    &lt;br /&gt;
    return 0;  &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The program output is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
0&lt;br /&gt;
1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Backus–Naur form]]&lt;br /&gt;
* [[Combinator#Combinatory logic in computing|Combinatory logic in computing]]&lt;br /&gt;
* &amp;#039;&amp;#039;[[Design Patterns]]&amp;#039;&amp;#039;&lt;br /&gt;
* [[Domain-specific language]]&lt;br /&gt;
* [[Interpreter (computing)]]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== External links ==&lt;br /&gt;
{{wikibooks|Computer Science Design Patterns|Interpreter}}&lt;br /&gt;
* [https://lukaszwrobel.pl/blog/interpreter-design-pattern Interpreter implementation] in [[Ruby (programming language)|Ruby]]&lt;br /&gt;
* [https://github.com/jamesdhutton/Interpreter Interpreter implementation] in [[C++]]&lt;br /&gt;
* [https://sourcemaking.com/design_patterns/interpreter SourceMaking tutorial]&lt;br /&gt;
* [http://c2.com/cgi/wiki?InterpreterPattern Interpreter pattern description from the Portland Pattern Repository]&lt;br /&gt;
&lt;br /&gt;
{{Design Patterns Patterns}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Articles with example C Sharp code]]&lt;br /&gt;
[[Category:Articles with example Java code]]&lt;br /&gt;
[[Category:Software design patterns]]&lt;/div&gt;</summary>
		<author><name>imported&gt;Squawk7700</name></author>
	</entry>
</feed>