一、简介
Interpreter解释器模式为了解决的问题是:一些应用需要提供自己内建(Build-In)的脚本或者宏语言来让用户可以定义他们能够在系统中进行的特定操作。Interpreter模式则是使用一个解释器为用户提供一个一门定义语言的语法表示的解释器,然后通过这个解释器来解释语言中的句子。
Interpreter模式的典型结构图为:
Interpreter模式中,提供了TerminalExpression和NonterminalExpression两种表达式的解释方式,Context类用于为解释过程提供一些附加的信息(例如全局的信息)。
二、详解
1、代码实现
(1)代码context.h:
#ifndef _CONTEXT_H_
#define _CONTEXT_H_
class Context
{
public:
Context();
~Context();
protected:
private:
};
#endif
(2)代码context.cpp:
#include <iostream>
#include "context.h"
using namespace std;
Context::Context()
{
cout<<"Context::Context"<<endl;
}
Context::~Context()
{
}
(3)代码interpret.h:
#ifndef _INTERPRET_H_
#define _INTERPRET_H_
#include <string>
#include "context.h"
using namespace std;
class AbstractExpression
{
public:
virtual ~AbstractExpression();
virtual void Interpret(const Context &ct);
protected:
AbstractExpression();
private:
};
class TerminalExpression : public AbstractExpression
{
public:
TerminalExpression(const string&statement);
~TerminalExpression();
void Interpret(const Context &ct);
protected:
private:
string _statement;
};
class NonterminalExpression : public AbstractExpression
{
public:
NonterminalExpression(AbstractExpression *expression, int times);
~NonterminalExpression();
void Interpret(const Context& ct);
protected:
private:
AbstractExpression* _expression;
int _times;
};
#endif
(4)代码interpret.cpp:
#include <iostream>
#include "interpret.h"
using namespace std;
AbstractExpression::AbstractExpression()
{
}
AbstractExpression::~AbstractExpression()
{
}
void AbstractExpression::Interpret(const Context &ct)
{
}
TerminalExpression::TerminalExpression(const string&statement)
{
_statement = statement;
}
TerminalExpression::~TerminalExpression()
{
}
void TerminalExpression::Interpret(const Context &ct)
{
cout<<"TerminalExpression::Interpret---"<<this->_statement<<endl;
}
NonterminalExpression::NonterminalExpression(AbstractExpression *expression, int times)
{
this->_expression = expression;
this->_times = times;
}
NonterminalExpression::~NonterminalExpression()
{
}
void NonterminalExpression::Interpret(const Context& ct)
{
for (int i = 0; i < _times; i++) {
_expression->Interpret(ct);
}
}
(5)代码main.cpp:
#include <iostream>
#include "context.h"
#include "interpret.h"
using namespace std;
int main()
{
Context* ct = new Context();
AbstractExpression *te = new TerminalExpression("hello");
AbstractExpression *nte = new NonterminalExpression(te, 3);
nte->Interpret(*ct);
return 0;
}
(6)makefile:
CFLAGS = -g
DEFINED = #-D _VERSION
LIBS =
CC = g++
INCLUDES = -I./
OBJS= main.o context.o interpret.o
TARGET= main
all:$(TARGET)
$(TARGET):$(OBJS)
$(CC) $(CFLAGS) -o $@ $(OBJS)
.SUFFIXES:.o .h
.SUFFIXES:.cpp .o
.cpp.o:
$(CC) $(DEFINED) -c $(CFLAGS) -o $@ $<
ok:
./$(TARGET)
clean:
rm -f $(OBJS) $(TARGET) core *.log
2、运行结果
(Centos6.3系统中运行结果:)
Interpreter模式代码只是为了说明模式的组织和使用,实际的解释Interpret 逻辑没有实际提供。
三、总结
(1)Interpreter 模式提供了一种很好的组织和设计这种解析器的架构。
(2)Interpreter 模式中使用类来表示文法规则,因此可以很容易实现文法的扩展。另外对于终结符我们可以使用 Flyweight 模式来实现终结符的共享。
(3)源码已经打包上传到csdn上可登录下载(http://download.csdn.net/detail/taiyang1987912/8438701)。