解释器模式

实验17:解释器模式

本次实验属于模仿型实验,通过本次实验学生将掌握以下内容: 

1、理解解释器模式的动机,掌握该模式的结构;

2、能够利用解释器模式解决实际问题。

 

[实验任务一]:解释器模式

 

某机器人控制程序包含一些简单的英文指令,其文法规则如下:

 

expression ::= direction action distance | composite

 

composite ::= expression and expression

 

direction ::= ‘up’ | ‘down’ | ‘left’ | ‘right’

 

action ::= ‘move’ | ‘run’

 

distance ::= an integer //一个整数值

 

如输入:up move 5,则输出向上移动5个单位;输入:down run 10 and left move 20,则输出向下移动10个单位再向左移动20个单位

类图

 

源代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
abstract class AbstractNode {
public abstract String interpret();
}
public class ActionNode extends AbstractNode{
private String action;
public ActionNode(String action) {
super();
this.action = action;
}
@Override
public String interpret() {
// TODO Auto-generated method stub
if(action.equals("move")){
return "移动";
}else if(action.equals("run")){
return "快速移动";
}else{
return "error";
}
 
}
}
public class AndNode extends AbstractNode {
private AbstractNode left;
private AbstractNode right;
@Override
public String interpret() {
// TODO Auto-generated method stub
return left.interpret() + "再" +right.interpret();
}
public AndNode(AbstractNode left, AbstractNode right) {
super();
this.left = left;
this.right = right;
}
}
public class Client {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = "down run 10 and left move 20";
InstructionHandler handler = new InstructionHandler();
handler.handle(s);
 
System.out.println(handler.output());
}
}
public class DirectionNode extends AbstractNode{
private String direction;
 
public DirectionNode(String direction) {
super();
this.direction = direction;
}
@Override
public String interpret() {
// TODO Auto-generated method stub
if(direction.equals("up")){
return "向上";
}else if(direction.equals("down")){
return "向下";
}else if(direction.equals("left")){
return "向左";
}else if(direction.equals("right")){
return "向右";
}else{
return "error";
}
}
}
public class DistanceNode extends AbstractNode{
private String distance;
public DistanceNode(String dis) {
super();
this.distance = dis;
}
@Override
public String interpret() {
// TODO Auto-generated method stub
return this.distance;
}
 
}
import java.util.Stack;
public class InstructionHandler {
private AbstractNode node;
public void handle(String instruction){
AbstractNode left = null;
AbstractNode right = null;
AbstractNode direction = null;
AbstractNode action = null;
AbstractNode distance = null;
Stack<AbstractNode> stack = new Stack<>();
String [] words = instruction.split(" ");
for (int i = 0; i < words.length; i++) {
if(words[i].equalsIgnoreCase("and")){
left = (AbstractNode)stack.pop();
String dir = words[++i];
direction = new DirectionNode(dir);
String a = words[++i];
action = new ActionNode(a);
String dis = words[++i];
distance = new DistanceNode(dis);
right = new SentenceNode(direction, action, distance);
stack.push(new AndNode(left, right));
}else{
String dir = words[i];
direction = new DirectionNode(dir);
String a = words[++i];
action = new ActionNode(a);
String dis = words[++i];
distance = new DistanceNode(dis);
left = new SentenceNode(direction, action, distance);
stack.push(left);
 
}
}
this.node = (AbstractNode)stack.pop();
}
public String output(){
String result = node.interpret();
return result;
}
}
public class SentenceNode extends AbstractNode{
private AbstractNode direction;
private AbstractNode action;
private AbstractNode distance;
public SentenceNode(AbstractNode direction, AbstractNode action, AbstractNode distance) {
super();
this.direction = direction;
this.action = action;
this.distance = distance;
}
@Override
public String interpret() {
// TODO Auto-generated method stub
return direction.interpret()+action.interpret()+distance.interpret();
}
}

  

 

posted @   平安喜乐×  阅读(8)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示