Interpreter Definition:
Defines the grammar of a language and creates an interpreter to interpret the sentences in that language.
Interpreter does not seem to have a wide range of facets, it describes how a language interpreter is constructed, and in practical applications we may rarely construct a language grammar. Let's just take a quick look at:
The first step is to create an interface to describe the common operation.
public interface AbstractExpression {
void interpret( Context context );
}
And look at some global information outside of the interpreter.
public interface Context { }
The specific implementations of Abstractexpression are divided into two types: non-terminal expressions and non-non-terminal expressions:
public class TerminalExpression implements AbstractExpression {
public void interpret( Context context ) { }
}
No non-terminal expression is required for a rule in the grammar:
public class NonterminalExpression implements AbstractExpression {
private AbstractExpression successor;
public void setSuccessor( AbstractExpression successor ) {
this.successor = successor;
}
public AbstractExpression getSuccessor() {
return successor;
}
public void interpret( Context context ) { }
}