1. Explain interpreter mode 1.1 interpreter mode definition
Given a language, it defines a representation of its syntax and an interpreter that uses this representation to interpret sentences in the language.
1.2 Main Points of interpreter Mode
Parser:Parses the expressions required for client calls to form an abstract syntax tree program.
Interpreter:Explanation syntax abstract tree
Generally, an interpreter processes a syntax rule.
1.3 Structure Diagram and description of interpreter Mode
Abstract Interpreter:Declare an abstract interface (or abstract class) that must be implemented by all specific expressions. An interface is mainly an interpret () method, which is called an interpreted operation. The specific interpretation task is completed by its implementation classes. The specific interpreter is completed by the terminalexpression and nonterminalexpression, respectively.
Terminator expression:Implementation of interpretation operations associated with elements in grammar. Generally, there is only one Terminator expression in a interpreter mode, but there are multiple instances that correspond to different terminologies. Half of the Terminator is the unit of operation in the grammar. For example, there is a simple formula r = R1 + R2 in which R1 and R2 are the Terminator, the interpreter for parsing R1 and R2 is the terminator expression.
Non-terminator expression:Each rule in the grammar corresponds to a non-terminator expression. A non-terminator expression is generally an operator or other keyword in the grammar. For example, in the formula r = R1 + R2, + is a non-Terminator, the parser + interpreter is a non-terminator expression. Non-terminator expressions increase based on the complexity of the logic. In principle, each grammar rule corresponds to a non-terminator expression.
Environment role:The task of this role is generally used to store the specific values of each terminator in the grammar. For example, if r = R1 + R2, we assign 100 to R1 and 200 to R2. This information needs to be stored in the Environment role. In many cases, it is enough to use map to act as the Environment role.
1.4 sample code of interpreter Mode
Package demow.interpreter. example2;/*** Abstract Expression */public abstract class into actexpression {/*** interpreted operation * @ Param CTX context object */public abstract void interpret (context CTX );} **************************************** **************************************** * ****************** package demo20.interpreter. example2;/*** Terminator expression */public class terminalexpression extends actexpression {public void interpret (context CTX) {// explain the operation associated with the terminator in the syntax rule }}************************ **************************************** * ********************************* package demo20.interpreter. example2;/*** non-terminator expression */public class nonterminalexpression extends actexpression {public void interpret (context CTX) {// implement the explain operation associated with non-terminator in the syntax rule }}*********************** **************************************** * ********************************** package demo20.interpreter. example2;/*** context, some global information except the package interpreter */public class context {}************************* **************************************** * ******************************* package demo20.interpreter. example2;/*** client using interpreter */public class client {// construct an abstract syntax tree for specific sentences according to the syntax rules // then call the interpretation operation}1.5 essence of interpreter Mode
Implement separation and explain execution
1.6 Advantages and Disadvantages of interpreter Mode
Advantages:Easy Algorithm Implementation and new syntax Extension
Disadvantages:Not suitable for complex syntaxes. The Interpreter mode causes class expansion. Each syntax requires a non-terminator expression. When syntax rules are complex, a large number of class files may be generated, it brings a lot of trouble to maintenance
1.7 when to choose
When a language needs to be interpreted and executed, and the sentences in the language can be expressed as an abstract syntax tree
Relatively simple syntax
Low Efficiency Requirements
1.8 notes
Since there are few interpreter applications, I will not give an example here.