Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Who are the participants in the web interpreter pattern

Shulou Source: shulou.com Published: 2022-06-01 03:42:30 10月04日 Update

In this article, the editor introduces in detail "who are the participants in the web interpreter mode", the content is detailed, the steps are clear, and the details are handled properly. I hope this article "what are the participants in the web interpreter mode" can help you solve your doubts? let's follow the editor's ideas to learn new knowledge.

Applicability

If a language can be represented as an abstract syntax tree that is simple enough, and efficiency is not the most important factor, you can use the interpreter pattern.

structure

Participant

Abstract_Expression (abstract expression, such as Expr)

-declares an abstract interpretation operation that is shared by all subclasses.

Nonterminal_Expression (non-Terminator expressions, such as AdditiveExpr, MultiplicativeExpr, etc.)

-all syntax nodes in a grammar that can be decomposed into a combination of subordinate syntax nodes need a NonterminalExpression class to implement their functions.

-NonterminalExpression maintains a list of AbstractExpression types for subordinate syntax nodes.

-implement the Interpret operation. In addition to recursively calling the interpretation operation that represents the subordinate syntax node, Interpret can also further calculate the interpretation results of each node. For example, AdditiveExpr needs to perform a generalized (addition or subtraction) addition calculation for the interpretation results of each MultiplicativeExpr.

Terminal_Expression (Terminator expression, such as IntegerLiteral, etc.)

-directly implement simple operations or calculations in grammar.

Context (context)

-contains some global information outside the interpreter.

Client (customer)

-build a syntax tree.

-invoke the interpretation operation.

Collaboration

Client builds an abstract syntax tree. The context is then initialized and the interpretation operation is invoked. Building a syntax tree can also be implemented as a function of syntax node classes. After all, the one who is most familiar with the syntax node is the grammar node class itself.

Each non-Terminator expression node invokes the interpretation operation of the corresponding subexpression and synthesizes the processing result. Each Terminator expression is responsible for simple processing or computation that does not depend on other nodes.

The interpretation operation of each node can store and access the state of the interpreter through context. For example, obtaining or storing calculation results, obtaining pre-defined constants, calling pre-logged-in functions, and so on, all need to be implemented through context.

After reading this, the article "who are the participants in the web interpreter mode" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it. If you want to know more about the article, please follow the industry information channel.

Tags: Interpretation node grammar interpreter expression participant pattern up and down context Terminator result subordinate article processing content function grammar proper heaviest global Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Shulou Technology MariaDB Huawei OPPO Reno