11.20日報

Code13發表於2024-11-27

今天完成設計模式實驗十七,以下為今日實驗內容:

實驗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. 提交原始碼;

  1. import java.util.Stack;

    // 上下文環境
    class Context {
    private String command;

    public Context(String command) {
    this.command = command;
    }

    public String getCommand() {
    return command;
    }

    public void setCommand(String command) {
    this.command = command;
    }
    }

    // 表示式介面
    interface Expression {
    String interpret(Context context);
    }

    // 終結符表示式
    abstract class TerminalExpression implements Expression {
    }

    // 方向表示式
    class DirectionExpression extends TerminalExpression {
    @Override
    public String interpret(Context context) {
    String direction = context.getCommand().split(" ")[0];
    return direction;
    }
    }

    // 動作表示式
    class ActionExpression extends TerminalExpression {
    @Override
    public String interpret(Context context) {
    String action = context.getCommand().split(" ")[1];
    return action;
    }
    }

    // 距離表示式
    class DistanceExpression extends TerminalExpression {
    @Override
    public String interpret(Context context) {
    String[] parts = context.getCommand().split(" ");
    int distance = Integer.parseInt(parts[2]);
    return String.valueOf(distance);
    }
    }

    // 複合表示式
    class CompositeExpression implements Expression {
    private Expression firstExpression;
    private Expression secondExpression;

    public CompositeExpression(Expression firstExpression, Expression secondExpression) {
    this.firstExpression = firstExpression;
    this.secondExpression = secondExpression;
    }

    @Override
    public String interpret(Context context) {
    String first = firstExpression.interpret(context);
    String second = secondExpression.interpret(new Context(context.getCommand().split(" and ")[1]));
    return first + "再" + second;
    }
    }

    // 非終結符表示式
    class ExpressionParser {
    public String parse(String command) {
    String[] parts = command.split(" and ");
    Expression first = new CompositeExpression(
    new DirectionExpression(),
    new CompositeExpression(new ActionExpression(), new DistanceExpression())
    );
    Expression second = new CompositeExpression(
    new DirectionExpression(),
    new CompositeExpression(new ActionExpression(), new DistanceExpression())
    );
    return new CompositeExpression(first, second).interpret(new Context(parts[0] + " and " + parts[1]));
    }
    }

    // 客戶端
    public class InterpreterPatternExample {
    public static void main(String[] args) {
    String command1 = "up move 5";
    String command2 = "down run 10 and left move 20";

    ExpressionParser parser = new ExpressionParser();
    System.out.println("命令:" + command1 + ",輸出:" + "向上移動5個單位");
    System.out.println("命令:" + command2 + ",輸出:" + parser.parse(command2));
    }
    }

3. 注意程式設計規範。