解释器模式(Interpreter)
-
介绍:给分析对象定义一个语言,并定义该语言的文法表示,再设计一个解析器来解释语言中的句子。也就是说,用编译语言的方式来分析应用中的实例。
-
解释器模式需要解决的是,如果一种特定类型的问题发生的频率足够高,那么可能就值得将该问题的各个实例表述为一个简单语言中的句子。这样就可以构件一个解释器,该解释器通过解释这些句子,来解决该问题。
-
优点:
扩展性好。由于在解释器模式中使用类来表示语言的文法规则,因此可以通过继承等机制来改变或扩展文法。
容易实现。在语法树中的每个表达式节点类都是相似的,所以实现其文法较为容易。 -
缺点:
执行效率较低。解释器模式中通常使用大量的循环和递归调用,当要解释的句子较复杂时,其运行速度很慢,且代码的调试过程也比较麻烦。
会引起类膨胀。解释器模式中的每条规则至少需要定义一个类,当包含的文法规则很多时,类的个数将急剧增加,导致系统难以管理与维护。
可应用的场景比较少。在软件开发中,需要定义语言文法的应用实例非常少,所以这种模式很少被使用到。 -
主要角色
抽象表达式(Abstract Expression)角色:定义解释器的接口,约定解释器的解释操作,主要包含解释方法 interpret()。
终结符表达式(Terminal Expression)角色:是抽象表达式的子类,用来实现文法中与终结符相关的操作,文法中的每一个终结符都有一个具体终结表达式与之相对应。
非终结符表达式(Nonterminal Expression)角色:也是抽象表达式的子类,用来实现文法中与非终结符相关的操作,文法中的每条规则都对应于一个非终结符表达式。
环境(Context)角色:通常包含各个解释器需要的数据或是公共的功能,一般用来传递被所有解释器共享的数据,后面的解释器可以从这里获取这些值。
客户端(Client):主要任务是将需要分析的句子或表达式转换成使用解释器对象描述的抽象语法树,然后调用解释器的解释方法,当然也可以通过环境角色间接访问解释器的解释方法。
#include "iostream"
#include "vector"
#include "string"
using namespace std;
// 抽象表达式类
class AbstractNode
{
public:
AbstractNode(){}
// 声明抽象接口
virtual char interpret() = 0;
};
// 终结符表达式:ValueNode
class ValueNode :public AbstractNode
{
public:
ValueNode(){}
ValueNode(int iValue){
value = iValue;
}
// 实现解释操作
char interpret(){
return value;
}
private:
int value;
};
// 终结符表达式:OperationNode
class OperatorNode :public AbstractNode
{
public:
OperatorNode(){}
OperatorNode(string iOp){
this->op = iOp;
}
// 实现解释操作
char interpret(){
if (op == "and"){
return '&';
}
else if (op == "or"){
return '|';
}
return 0;
}
private:
string op;
};
// 非终结符表达式:SentenceNode
class SentenceNode :public AbstractNode
{
public:
SentenceNode(){}
SentenceNode(AbstractNode *iLeftNode,
AbstractNode *iRightNode, AbstractNode* iOperatorNode){
this->leftNode = iLeftNode;
this->rightNode = iRightNode;
this->operatorNode = iOperatorNode;
}
char interpret(){
if (operatorNode->interpret() == '&'){
return leftNode->interpret()&rightNode->interpret();
}
else{
return leftNode->interpret() | rightNode->interpret();
}
return 0;
}
private:
AbstractNode *leftNode;
AbstractNode *rightNode;
AbstractNode *operatorNode;
};
// 处理者
class Handler
{
public:
Handler(){}
void setInput(string iInput){
this->input = iInput;
}
void handle(){
AbstractNode *left = NULL;
AbstractNode *right = NULL;
AbstractNode *op = NULL;
AbstractNode *sentence = NULL;
string iInput = this->input;
vector<string>inputList;
char* inputCh = const_cast<char*>(iInput.c_str());
char *token = NULL;
char *buf;
token = strtok_s(inputCh, " ", &buf);
while (token != NULL){
inputList.push_back(token);
token = strtok_s(NULL, " ", &buf);
}
for (int i = 0; i < inputList.size() - 2; i += 2){
left = new ValueNode(*(inputList[i].c_str()));
op = new OperatorNode(inputList[i + 1]);
right = new ValueNode(*(inputList[i + 2].c_str()));
sentence = new SentenceNode(left, right, op);
inputList[i + 2] = string(1, sentence->interpret());
}
string tmpRes = inputList[inputList.size() - 1];
if (tmpRes == "1"){
result = 1;
}
else if (tmpRes == "0"){
result = 0;
}
else{
result = -1;
}
this->output();
}
void output(){
printf("%s = %d\n", input.c_str(), result);
}
private:
string input;
char result;
};
int main()
{
Handler *handler = new Handler();
string input_1 = "1 and 1";
string input_2 = "1 and 0";
string input_3 = "0 and 1";
string input_4 = "0 and 0";
string input_5 = "0 or 0";
string input_6 = "0 or 1";
string input_7 = "1 or 0";
string input_8 = "1 or 1";
string input_9 = "1 and 0 or 1";
string input_10 = "0 or 0 and 1";
string input_11 = "1 or 1 and 1 and 0";
string input_12 = "0 and 1 and 1 and 1";
string input_13 = "0 and 1 and 1 and 1 or 1 or 0 and 1";
handler->setInput(input_1); handler->handle();
handler->setInput(input_2); handler->handle();
handler->setInput(input_3); handler->handle();
handler->setInput(input_4); handler->handle();
handler->setInput(input_5); handler->handle();
handler->setInput(input_6); handler->handle();
handler->setInput(input_7); handler->handle();
handler->setInput(input_8); handler->handle();
handler->setInput(input_9); handler->handle();
handler->setInput(input_10); handler->handle();
handler->setInput(input_11); handler->handle();
handler->setInput(input_12); handler->handle();
handler->setInput(input_13); handler->handle();
system("pause");
return 1;
}
运行结果:
1 and 1 = 1
1 and 0 = 0
0 and 1 = 0
0 and 0 = 0
0 or 0 = 0
0 or 1 = 1
1 or 0 = 1
1 or 1 = 1
1 and 0 or 1 = 1
0 or 0 and 1 = 0
1 or 1 and 1 and 0 = 0
0 and 1 and 1 and 1 = 0
0 and 1 and 1 and 1 or 1 or 0 and 1 = 1
请按任意键继续. . .