Article Outline
TOC
Collection Outline
#1.定义 解释器模式(Interpreter),给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子。
#2.概述
#3.描述
Expression:抽象表达式,声明一个所有的具体表达式都需要实现的抽象接口;这个接口主要是一个interpret()方法,称做解释操作
Terminal Expression:终结符表达式,实现了抽象表达式所要求的接口;文法中的每一个终结符都有一个具体终结表达式与之相对应。
Nonterminal Expression:非终结符表达式,文法中的每一条规则都需要一个具体的非终结符表达式,非终结符表达式一般是文法中的运算符或者其他关键字
Context:环境,它的任务一般是用来存放文法中各个终结符所对应的具体值 #3.实现
抽象表达式 Expression
public interface Node{ public void parse(Context text); public void execute(); }
>Terminal Expression:终结符表达式_1
```java
public class SubjectPronounOrNounNode implements Node{
String [] word={"You","He","Teacher","Student"};
String token;
boolean boo;
public void parse(Context context){
token=context.nextToken();
int i=0;
for(i=0;i<word.length;i++){
if(token.equalsIgnoreCase(word[i])){
boo=true;
break;
}
}
if(i==word.length)
boo=false;
}
public void execute(){
if(boo){
if(token.equalsIgnoreCase(word[0]))
System.out.print("你");
if(token.equalsIgnoreCase(word[1]))
System.out.print("他");
if(token.equalsIgnoreCase(word[2]))
System.out.print("老师");
if(token.equalsIgnoreCase(word[3]))
System.out.print("学生");
}
else{
System.out.print(token+"(不是该语言中的语句)");
}
}
}
Terminal Expression:终结符表达式_2
public class ObjectPronounOrNounNode implements Node{ String [] word={"Me","Him","Tiger","Apple"}; String token; boolean boo; public void parse(Context context){ token=context.nextToken(); int i=0; for(i=0;i<word.length;i++){ if(token.equalsIgnoreCase(word[i])){ boo=true; break; } } if(i==word.length) boo=false; } public void execute(){ if(boo){ if(token.equalsIgnoreCase(word[0])) System.out.print("我"); if(token.equalsIgnoreCase(word[1])) System.out.print("他"); if(token.equalsIgnoreCase(word[2])) System.out.print("老虎"); if(token.equalsIgnoreCase(word[3])) System.out.print("苹果"); } else{ System.out.print(token+"(不是该语言中的语句)"); } } }
>Terminal Expression:终结符表达式_3
```java
public class VerbNode implements Node{
String [] word={"Drink","Eat","Look","beat"};
String token;
boolean boo;
public void parse(Context context){
token=context.nextToken();
int i=0;
for(i=0;i<word.length;i++){
if(token.equalsIgnoreCase(word[i])){
boo=true;
break;
}
}
if(i==word.length)
boo=false;
}
public void execute(){
if(boo){
if(token.equalsIgnoreCase(word[0]))
System.out.print("喝");
if(token.equalsIgnoreCase(word[1]))
System.out.print("吃");
if(token.equalsIgnoreCase(word[2]))
System.out.print("看");
if(token.equalsIgnoreCase(word[3]))
System.out.print("打");
}
else{
System.out.print(token+"(不是该语言中的语句)");
}
}
}
Nonterminal Expression:非终结符表达式_1
public class SentenceNode implements Node{ Node subjectNode,predicateNode; public void parse(Context context){ subjectNode =new SubjectNode(); predicateNode=new PredicateNode(); subjectNode.parse(context); predicateNode.parse(context); } public void execute(){ subjectNode.execute(); predicateNode.execute(); } }
Nonterminal Expression:非终结符表达式_2
public class SubjectNode implements Node{ Node node; public void parse(Context context){ node =new SubjectPronounOrNounNode(); node.parse(context); } public void execute(){ node.execute(); } }
>Nonterminal Expression:非终结符表达式_3
```java
public class PredicateNode implements Node{
Node verbNode,objectNode;
public void parse(Context context){
verbNode =new VerbNode();
objectNode=new ObjectNode();
verbNode.parse(context);
objectNode.parse(context);
}
public void execute(){
verbNode.execute();
objectNode.execute();
}
}
Nonterminal Expression:非终结符表达式_4
public class ObjectNode implements Node{ Node node; public void parse(Context context){ node =new ObjectPronounOrNounNode(); node.parse(context); } public void execute(){ node.execute(); } }
上下文Context
import java.util.StringTokenizer; public class Context{ StringTokenizer tokenizer; String token; public Context(String text){ setContext(text); } public void setContext(String text){ tokenizer=new StringTokenizer(text); } String nextToken(){ if(tokenizer.hasMoreTokens()){ token=tokenizer.nextToken(); } else token=""; return token; } }
运行类
public class Application{ public static void main(String args[]){ String text="Teacher beat tiger"; Context context=new Context(text); Node node=new SentenceNode(); node.parse(context); node.execute();
text="You eat apple";
context.setContext(text);
System.out.println();
node.parse(context);
node.execute();
text="you look him";
context.setContext(text);
System.out.println();
node.parse(context);
node.execute();
}
}
# 4.总结
>优点
![image.png](https://upload-images.jianshu.io/upload_images/7149586-924de5fa5ce5a7a1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
> 缺点
- 需要建大量的类,因为每一种语法都要建一个非终结符的类。
- 解释的时候采用递归调用方法,导致有时候函数的深度会很深,影响效率。