Interpreter pattern: Difference between revisions

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search
imported>RJFJR
 
Not vandalism, go harass someone else with this nonsense
Line 76: Line 76:
<!-- 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 -->
<!-- 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 -->


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


<syntaxhighlight lang="c++">
<syntaxhighlight lang="c++">
#include <iostream>
import std;
#include <map>
#include <cstring>


class Context;
using String = std::string;
template <typename K, typename V>
using TreeMap = std::map<K, V>;
template <typename T>
using UniquePtr = std::unique_ptr<T>;


class BooleanExp {
class BooleanExpression {
public:
public:
  BooleanExp() = default;
    BooleanExpression() = default;
  virtual ~BooleanExp() = default;
    virtual ~BooleanExpression() = default;
  virtual bool evaluate(Context&) = 0;
    virtual bool evaluate(Context&) = 0;
  virtual BooleanExp* replace(const char*, BooleanExp&) = 0;
    virtual UniquePtr<BooleanExpression> replace(String&, BooleanExpression&) = 0;
  virtual BooleanExp* copy() const = 0;
    virtual UniquePtr<BooleanExpression> copy() const = 0;
};
};


class VariableExp;
class VariableExpression;


class Context {
class Context {
private:
    TreeMap<const VariableExpression*, bool> m;
public:
public:
  Context() :m() {}
    Context() = default;
  bool lookup(const VariableExp* key) { return m.at(key); }
 
  void assign(VariableExp* key, bool value) { m[key] = value; }
    [[nodiscard]]
private:
    bool lookup(const VariableExpression* key) const {  
  std::map<const VariableExp*, bool> m;
        return m.at(key);  
    }
 
    void assign(VariableExpression* key, bool value) {  
        m[key] = value;  
    }
};
};


class VariableExp : public BooleanExp {
class VariableExpression : public BooleanExpression {
private:
    String name;
public:
public:
  VariableExp(const char* name_) :name(nullptr) {
    VariableExpression(const String& name):
    name = strdup(name_);
        name{name} {}
  }
 
  virtual ~VariableExp() = default;
    virtual ~VariableExpression() = default;
  virtual bool evaluate(Context& aContext) {
 
    return aContext.lookup(this);
    [[nodiscard]]
  }
    virtual bool evaluate(Context& context) const {
  virtual BooleanExp* replace( const char* name_, BooleanExp& exp ) {
        return context.lookup(this);
    if (0 == strcmp(name_, name)) {
    }
      return exp.copy();
 
    } else {
    [[nodiscard]]
      return new VariableExp(name);
    virtual UniquePtr<VariableExpression> replace(const String& name, BooleanExpression& exp) {
        if (this->name == name) {
            return std::make_unique<VariableExpression>(exp.copy());
        } else {
            return std::make_unique<VariableExpression>(name);
        }
     }
     }
  }
 
  virtual BooleanExp* copy() const {
    [[nodiscard]]
    return new VariableExp(name);
    virtual UniquePtr<BooleanExpression> copy() const {
  }
        return std::make_unique<BooleanExpression>(name);
  VariableExp(const VariableExp&) = delete; // rule of three
    }
  VariableExp& operator=(const VariableExp&) = delete;
 
private:
    VariableExpression(const VariableExpression&) = delete;
  char* name;
    VariableExpression& operator=(const VariableExpression&) = delete;
};
};


class AndExp : public BooleanExp {
class AndExpression : public BooleanExpression {
private:
    UniquePtr<BooleanExpression> operand1;
    UniquePtr<BooleanExpression> operand2;
public:
public:
  AndExp(BooleanExp* op1, BooleanExp* op2)
    AndExpression(UniquePtr<BooleanExpression> op1, UniquePtr<BooleanExpression> op2):
    :operand1(nullptr), operand2(nullptr) {
        operand1{std::move(op1)}, operand{std::move(op2)} {}
    operand1 = op1;
 
     operand2 = op2;
     virtual ~AndExpression() = default;
  }
 
  virtual ~AndExp() = default;
    [[nodiscard]]
  virtual bool evaluate(Context& aContext) {
    virtual bool evaluate(Context& context) const {
    return operand1->evaluate(aContext) && operand2->evaluate(aContext);
        return operand1->evaluate(context) && operand2->evaluate(context);
  }
    }
  virtual BooleanExp* replace(const char* name_, BooleanExp& exp) {
 
    return new AndExp(
    [[nodiscard]]
        operand1->replace(name_, exp),
    virtual UniquePtr<BooleanExpression> replace(const String& name, BooleanExpression& exp) const {
        operand2->replace(name_, exp)
        return std::make_unique<AndExpression>(
      );
            operand1->replace(name, exp),
  }
            operand2->replace(name, exp)
  virtual BooleanExp* copy() const {
        );
    return new AndExp(operand1->copy(), operand2->copy());
    }
  }
 
  AndExp(const AndExp&) = delete; // rule of three
    [[nodiscard]]
  AndExp& operator=(const AndExp&) = delete;
    virtual UniquePtr<BooleanExpression> copy() const {
private:
        return std::make_unique<AndExpression>(operand1->copy(), operand2->copy());
  BooleanExp* operand1;
    }
  BooleanExp* operand2;
 
    AndExpression(const AndExpression&) = delete;
    AndExpression& operator=(const AndExpression&) = delete;
};
};


int main() {
int main(int argc, char* argv[]) {
  BooleanExp* expression;
    UniquePtr<BooleanExpression> expression;
  Context context;
    Context context;
  VariableExp* x = new VariableExp("X");
    UniquePtr<VariableExpression> x = std::make_unique<VariableExpression>("X");
  VariableExp* y = new VariableExp("Y");
    UniquePtr<VariableExpression> y = std::make_unique<VariableExpression>("Y");
  expression = new AndExp(x, y);
    UniquePtr<BooleanExpression> expression; = std::make_unique<AndExpression>(x, y);


  context.assign(x, false);
    context.assign(x.get(), false);
  context.assign(y, true);
    context.assign(y.get(), true);
  bool result = expression->evaluate(context);
    bool result = expression->evaluate(context);
  std::cout << result << '\n';
    std::println("{}", result);


  context.assign(x, true);
    context.assign(x.get(), true);
  context.assign(y, true);
    context.assign(y.get(), true);
  result = expression->evaluate(context);
    result = expression->evaluate(context);
  std::cout << result << '\n';
    std::println("{}", result);
   
    return 0;
}
}
</syntaxhighlight>
</syntaxhighlight>

Revision as of 20:08, 4 November 2025

Template:Short description Template:Refimprove

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.[1]Template:Rp See also Composite pattern.

Overview

The Interpreter [2] 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:[3]

  • 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.

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 languagesScript error: No such module "Unsubst"..

Structure

UML class and object diagram

File:W3sDesign Interpreter Design Pattern UML.jpg
A sample UML class and object diagram for the Interpreter design pattern.[4]

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

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

UML class diagram

File:Interpreter UML class diagram.svg

Example

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

import std;

using String = std::string;
template <typename K, typename V>
using TreeMap = std::map<K, V>;
template <typename T>
using UniquePtr = std::unique_ptr<T>;

class BooleanExpression {
public:
    BooleanExpression() = default;
    virtual ~BooleanExpression() = default;
    virtual bool evaluate(Context&) = 0;
    virtual UniquePtr<BooleanExpression> replace(String&, BooleanExpression&) = 0;
    virtual UniquePtr<BooleanExpression> copy() const = 0;
};

class VariableExpression;

class Context {
private:
    TreeMap<const VariableExpression*, bool> 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<VariableExpression> replace(const String& name, BooleanExpression& exp) {
        if (this->name == name) {
            return std::make_unique<VariableExpression>(exp.copy());
        } else {
            return std::make_unique<VariableExpression>(name);
        }
    }

    [[nodiscard]]
    virtual UniquePtr<BooleanExpression> copy() const {
        return std::make_unique<BooleanExpression>(name);
    }

    VariableExpression(const VariableExpression&) = delete;
    VariableExpression& operator=(const VariableExpression&) = delete;
};

class AndExpression : public BooleanExpression {
private:
    UniquePtr<BooleanExpression> operand1;
    UniquePtr<BooleanExpression> operand2;
public:
    AndExpression(UniquePtr<BooleanExpression> op1, UniquePtr<BooleanExpression> 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<BooleanExpression> replace(const String& name, BooleanExpression& exp) const {
        return std::make_unique<AndExpression>(
            operand1->replace(name, exp),
            operand2->replace(name, exp)
        );
    }

    [[nodiscard]]
    virtual UniquePtr<BooleanExpression> copy() const {
        return std::make_unique<AndExpression>(operand1->copy(), operand2->copy());
    }

    AndExpression(const AndExpression&) = delete;
    AndExpression& operator=(const AndExpression&) = delete;
};

int main(int argc, char* argv[]) {
    UniquePtr<BooleanExpression> expression;
    Context context;
    UniquePtr<VariableExpression> x = std::make_unique<VariableExpression>("X");
    UniquePtr<VariableExpression> y = std::make_unique<VariableExpression>("Y");
    UniquePtr<BooleanExpression> expression; = std::make_unique<AndExpression>(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

See also

References

  1. Script error: No such module "citation/CS1".
  2. Script error: No such module "citation/CS1".
  3. Script error: No such module "citation/CS1".
  4. Script error: No such module "citation/CS1".

External links

Template:Sister project

Template:Design Patterns Patterns