Interpreter

Intent

Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.

Class diagram of the original GoF pattern

structure-GoF:interpreter

Improvement in MixJuice

Solved Problem(s)

Introduction problem
It is impossible to introduce Interpreter pattern into existing tree structure without modifying the source code. (This problem is not mentioned in the GoF book.)
Extensibility problem
It is impossible to add new ways to interpret expressions without modifying the source code. (This problem is mentioned at p.247,line.11-16 in the GoF book.)

Used programming technique(s)

Abstract method addition to AbstractExpression.

Consequences

Structure and pseudo code

structure:interpreter_sample1

module original {
  define class Client {
    AbstractExpression exp;
    Context context;
    define void m(){
      exp.interpret(context);
    }
  }
  define class Context {...}
  define abstract class AbstractExpression {
    define abstract void interpret(Context context);
  }
  define class ExpressionA extends AbstractExpression {
    void interpret(Context context){...}
  }
  define class ExpressionB extends AbstractExpression {
    void interpret(Context context){...}
  }
}
module extension extends original {
  class AbstractExpression {
    define abstract void newOperation();
  }
  class ExpressionA {
    void newOperation(){...}
  }
  class ExpressionB {
    void newOperation(){...}
  }
}

Akira TANAKA <akr@m17n.org>, Yuuji ICHISUGI <y-ichisugi@aist.go.jp>

Top