Interpreter definition: defines the grammar of a language and establishes an interpreter to interpret sentences in the language.
Interpreter seems to be not very broad in use. It describes how a language interpreter is constructed. In practical applications, we may rarely construct the grammar of a language. Let’s take a brief look.
First, an interface is established to describe common operations.
The code copy is as follows:
public interface AbstractExpression {
void interpret( Context context );
}
Let's take a look at some global information that contains the interpreter
The code copy is as follows:
public interface Context { }
The specific implementation of AbstractExpression is divided into two types: terminator expression and non-terminator expression.
public class TerminalExpression implements AbstractExpression {
public void interpret( Context context ) { }
}
For no rule in grammar, non-terminal expressions are required:
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 ) { }
}