某机器人控制程序包含一些简单的英文指令,其文法规则如下:
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个单位”。

package 实验17解释器模式; abstract class AbstractNode { public abstract String interpret(); }

package 实验17解释器模式; 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"; } } }

package 实验17解释器模式; 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; } }

package 实验17解释器模式; 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()); } }

package 实验17解释器模式; 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"; } } }

package 实验17解释器模式; 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; } }

package 实验17解释器模式; 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; } }

package 实验17解释器模式; 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(); } }