自定义语言的实现——解释器模式(四)

18.4 完整解决方案

      为了能够解释机器人控制指令,Sunny软件公司开发人员使用解释器模式来设计和实现机器人控制程序。针对五条文法规则,分别提供五个类来实现,其中终结符表达式directionactiondistance对应DirectionNode类、ActionNode类和DistanceNode类,非终结符表达式expressioncomposite对应SentenceNode类和AndNode类。

      我们可以通过抽象语法树来表示具体解释过程,例如机器人控制指令“down run 10 and left move 20”对应的抽象语法树如图18-4所示:

18-4   机器人控制程序抽象语法树实例

      机器人控制程序实例基本结构如图18-5所示:

18-5   机器人控制程序结构图

      在图18-5中,AbstractNode充当抽象表达式角色,DirectionNodeActionNodeDistanceNode充当终结符表达式角色,AndNodeSentenceNode充当非终结符表达式角色。完整代码如下所示:

[java]  view plain copy
  1. //注:本实例对机器人控制指令的输出结果进行模拟,将英文指令翻译为中文指令,实际情况是调用不同的控制程序进行机器人的控制,包括对移动方向、方式和距离的控制等  
  2. import java.util.*;  
  3.   
  4. //抽象表达式  
  5. abstract class AbstractNode {  
  6.     public abstract String interpret();  
  7. }  
  8.   
  9. //And解释:非终结符表达式  
  10. class AndNode extends AbstractNode {  
  11.     private AbstractNode left; //And的左表达式  
  12.     private AbstractNode right; //And的右表达式  
  13.   
  14.     public AndNode(AbstractNode left, AbstractNode right) {  
  15.         this.left = left;  
  16.         this.right = right;  
  17.     }  
  18.       
  19.     //And表达式解释操作  
  20.     public String interpret() {  
  21.         return left.interpret() + "再" + right.interpret();  
  22.     }  
  23. }  
  24.   
  25. //简单句子解释:非终结符表达式  
  26. class SentenceNode extends AbstractNode {  
  27.     private AbstractNode direction;  
  28.     private AbstractNode action;  
  29.     private AbstractNode distance;  
  30.   
  31.     public SentenceNode(AbstractNode direction,AbstractNode action,AbstractNode distance) {  
  32.         this.direction = direction;  
  33.         this.action = action;  
  34.         this.distance = distance;  
  35.     }  
  36.       
  37.     //简单句子的解释操作  
  38.     public String interpret() {  
  39.         return direction.interpret() + action.interpret() + distance.interpret();  
  40.     }     
  41. }  
  42.   
  43. //方向解释:终结符表达式  
  44. class DirectionNode extends AbstractNode {  
  45.     private String direction;  
  46.       
  47.     public DirectionNode(String direction) {  
  48.         this.direction = direction;  
  49.     }  
  50.       
  51.     //方向表达式的解释操作  
  52.     public String interpret() {  
  53.         if (direction.equalsIgnoreCase("up")) {  
  54.             return "向上";  
  55.         }  
  56.         else if (direction.equalsIgnoreCase("down")) {  
  57.             return "向下";  
  58.         }  
  59.         else if (direction.equalsIgnoreCase("left")) {  
  60.             return "向左";  
  61.         }  
  62.         else if (direction.equalsIgnoreCase("right")) {  
  63.             return "向右";  
  64.         }  
  65.         else {  
  66.             return "无效指令";  
  67.         }  
  68.     }  
  69. }  
  70.   
  71. //动作解释:终结符表达式  
  72. class ActionNode extends AbstractNode {  
  73.     private String action;  
  74.       
  75.     public ActionNode(String action) {  
  76.         this.action = action;  
  77.     }  
  78.       
  79.     //动作(移动方式)表达式的解释操作  
  80.     public String interpret() {  
  81.         if (action.equalsIgnoreCase("move")) {  
  82.             return "移动";  
  83.         }  
  84.         else if (action.equalsIgnoreCase("run")) {  
  85.             return "快速移动";  
  86.         }  
  87.         else {  
  88.             return "无效指令";  
  89.         }  
  90.     }  
  91. }  
  92.   
  93. //距离解释:终结符表达式  
  94. class DistanceNode extends AbstractNode {  
  95.     private String distance;  
  96.       
  97.     public DistanceNode(String distance) {  
  98.         this.distance = distance;  
  99.     }  
  100.       
  101. //距离表达式的解释操作  
  102.     public String interpret() {  
  103.         return this.distance;  
  104.     }     
  105. }  
  106.   
  107. //指令处理类:工具类  
  108. class InstructionHandler {  
  109.     private String instruction;  
  110.     private AbstractNode node;  
  111.       
  112.     public void handle(String instruction) {  
  113.         AbstractNode left = null, right = null;  
  114.         AbstractNode direction = null, action = null, distance = null;  
  115.         Stack stack = new Stack(); //声明一个栈对象用于存储抽象语法树  
  116.         String[] words = instruction.split(" "); //以空格分隔指令字符串  
  117.         for (int i = 0; i < words.length; i++) {  
  118. //本实例采用栈的方式来处理指令,如果遇到“and”,则将其后的三个单词作为三个终结符表达式连成一个简单句子SentenceNode作为“and”的右表达式,而将从栈顶弹出的表达式作为“and”的左表达式,最后将新的“and”表达式压入栈中。                   if (words[i].equalsIgnoreCase("and")) {  
  119.                 left = (AbstractNode)stack.pop(); //弹出栈顶表达式作为左表达式  
  120.                 String word1= words[++i];  
  121.                 direction = new DirectionNode(word1);  
  122.                 String word2 = words[++i];  
  123.                 action = new ActionNode(word2);  
  124.                 String word3 = words[++i];  
  125.                 distance = new DistanceNode(word3);  
  126.                 right = new SentenceNode(direction,action,distance); //右表达式  
  127.                 stack.push(new AndNode(left,right)); //将新表达式压入栈中  
  128.             }  
  129.             //如果是从头开始进行解释,则将前三个单词组成一个简单句子SentenceNode并将该句子压入栈中  
  130.             else {  
  131.                 String word1 = words[i];  
  132.                 direction = new DirectionNode(word1);  
  133.                 String word2 = words[++i];  
  134.                 action = new ActionNode(word2);  
  135.                 String word3 = words[++i];  
  136.                 distance = new DistanceNode(word3);  
  137.                 left = new SentenceNode(direction,action,distance);  
  138.                 stack.push(left); //将新表达式压入栈中  
  139.             }  
  140.         }  
  141.         this.node = (AbstractNode)stack.pop(); //将全部表达式从栈中弹出  
  142.     }  
  143.       
  144.     public String output() {  
  145.         String result = node.interpret(); //解释表达式  
  146.         return result;  
  147.     }  
  148. }  

       工具类InstructionHandler用于对输入指令进行处理,将输入指令分割为字符串数组,将第1个、第2个和第3个单词组合成一个句子,并存入栈中;如果发现有单词“and”,则将“and”后的第1个、第2个和第3个单词组合成一个新的句子作为“and”的右表达式,并从栈中取出原先所存句子作为左表达式,然后组合成一个And节点存入栈中。依此类推,直到整个指令解析结束。

       编写如下客户端测试代码:

[java]  view plain copy
  1. class Client {  
  2.     public static void main(String args[]) {  
  3.         String instruction = "up move 5 and down run 10 and left move 5";  
  4.         InstructionHandler handler = new InstructionHandler();  
  5.         handler.handle(instruction);  
  6.         String outString;  
  7.         outString = handler.output();  
  8.         System.out.println(outString);  
  9.     }  
  10. }  

       编译并运行程序,输出结果如下:

向上移动5再向下快速移动10再向左移动5

【作者:刘伟     http://blog.csdn.net/lovelion


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值