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.[1]: 243 See also Composite pattern. OverviewThe 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]
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?
The expression objects are composed recursively into a composite/tree structure that is called
abstract syntax tree (see Composite pattern).
See also the UML class and object diagram below. Uses
StructureUML class and object diagramIn the above UML class diagram, the The object collaboration diagram
shows the run-time interactions: The UML class diagramExampleThis C++11 implementation is based on the pre C++98 sample code in the book. #include <iostream>
#include <map>
#include <cstring>
class Context;
class BooleanExp {
public:
BooleanExp() = default;
virtual ~BooleanExp() = default;
virtual bool evaluate(Context&) = 0;
virtual BooleanExp* replace(const char*, BooleanExp&) = 0;
virtual BooleanExp* copy() const = 0;
};
class VariableExp;
class Context {
public:
Context() :m() {}
bool lookup(const VariableExp* key) { return m.at(key); }
void assign(VariableExp* key, bool value) { m[key] = value; }
private:
std::map<const VariableExp*, bool> m;
};
class VariableExp : public BooleanExp {
public:
VariableExp(const char* name_) :name(nullptr) {
name = strdup(name_);
}
virtual ~VariableExp() = default;
virtual bool evaluate(Context& aContext) {
return aContext.lookup(this);
}
virtual BooleanExp* replace( const char* name_, BooleanExp& exp ) {
if (0 == strcmp(name_, name)) {
return exp.copy();
} else {
return new VariableExp(name);
}
}
virtual BooleanExp* copy() const {
return new VariableExp(name);
}
VariableExp(const VariableExp&) = delete; // rule of three
VariableExp& operator=(const VariableExp&) = delete;
private:
char* name;
};
class AndExp : public BooleanExp {
public:
AndExp(BooleanExp* op1, BooleanExp* op2)
:operand1(nullptr), operand2(nullptr) {
operand1 = op1;
operand2 = op2;
}
virtual ~AndExp() = default;
virtual bool evaluate(Context& aContext) {
return operand1->evaluate(aContext) && operand2->evaluate(aContext);
}
virtual BooleanExp* replace(const char* name_, BooleanExp& exp) {
return new AndExp(
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
AndExp& operator=(const AndExp&) = delete;
private:
BooleanExp* operand1;
BooleanExp* operand2;
};
int main() {
BooleanExp* expression;
Context context;
VariableExp* x = new VariableExp("X");
VariableExp* y = new VariableExp("Y");
expression = new AndExp(x, y);
context.assign(x, false);
context.assign(y, true);
bool result = expression->evaluate(context);
std::cout << result << '\n';
context.assign(x, true);
context.assign(y, true);
result = expression->evaluate(context);
std::cout << result << '\n';
}
The program output is: 0
1
See also
References
External linksThe Wikibook Computer Science Design Patterns has a page on the topic of: Interpreter |