11.14实验17:解释器模式

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

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

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. 提交源代码;

AbstractNode.java

package org.example.shiyanshiqi;

public abstract class AbstractNode {
    public abstract String interpret();
}

 

AndNode.java

package org.example.shiyanshiqi;

public class AndNode extends AbstractNode{
    private AbstractNode left;
    private AbstractNode right;

    public AndNode(AbstractNode left, AbstractNode right) {
        this.left = left;
        this.right = right;
    }

    @Override
    public String interpret() {
        return left.interpret() + "  " + right.interpret();
    }
}

 

SentenceNode.java

package org.example.shiyanshiqi;

public class SentenceNode extends AbstractNode {
    private AbstractNode direction;
    private AbstractNode action;
    private AbstractNode distance;

    public SentenceNode(AbstractNode direction, AbstractNode action, AbstractNode distance) {
        this.direction = direction;
        this.action = action;
        this.distance = distance;
    }

    @Override
    public String interpret() {
        return direction.interpret() + action.interpret() + distance.interpret();
    }
}

 

DirectionNode.java

package org.example.shiyanshiqi;

 

public class DirectionNode extends AbstractNode {

    private String direction;

 

    public DirectionNode(String direction) {

        this.direction = direction;

    }

 

    @Override

    public String interpret() {

        if (direction.equalsIgnoreCase("up")) {

            return "向上";

        } else if (direction.equalsIgnoreCase("down")) {

            return "向下";

        } else if (direction.equalsIgnoreCase("left")) {

            return "向左";

        } else if (direction.equalsIgnoreCase("right")) {

            return "向右";

        } else {

            return "无效指令";

        }

    }

}

 

 

DistanceNode.java

package org.example.shiyanshiqi;

public class DistanceNode extends AbstractNode{
    private String distance;
    public DistanceNode(String distance){
        this.distance=distance;
    }

    @Override
    public String interpret() {
        return this.distance;
    }
}

 

ActionNode.java

package org.example.shiyanshiqi;

public class ActionNode extends AbstractNode {
    private String action;

    public ActionNode(String action) {
        this.action = action;
    }

    @Override
    public String interpret() {
        if (action.equalsIgnoreCase("move")) {
            return "移动";
        } else if (action.equalsIgnoreCase("run")) {
            return "快速移动";
        } else {
            return "停止";
        }
    }
}

 

InstructionHandler.java

package org.example.shiyanshiqi;
import java.util.*;
public class InstructionHandler {
    private String instruction;
    private AbstractNode node;
    public void handle(String instruction){
        AbstractNode left=null,right=null;
        AbstractNode direction=null,action=null,distance=null;
        Stack 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 word1=words[++i];
                direction=new DirectionNode(word1);
                String word2=words[++i];
                action = new ActionNode(word2);
                String word3=words[++i];
                distance=new DistanceNode(word3);
                right=new SentenceNode(direction,action,distance);
                stack.push(new AndNode(left,right));
            }else{
                String word1=words[i];
                direction=new DirectionNode(word1);
                String word2=words[++i];
                action=new ActionNode(word2);
                String word3=words[++i];
                distance=new DistanceNode(word3);
                left=new SentenceNode(direction,action,distance);
                stack.push(left);
            }
        }
        this.node=(AbstractNode) stack.pop();
    }
    public String output(){
        return node.interpret();
    }
}

 

Client.java

package org.example.shiyanshiqi;

public class Client {
    public static void main(String[] args) {
        String instruction="up move 6 and down run 11 and left move 6";
        InstructionHandler handler=new InstructionHandler();
        handler.handle(instruction);
        String outString=handler.output();
        System.out.println(outString);
    }
}

 

3. 注意编程规范。

 

 

posted @     阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
点击右上角即可分享
微信分享提示