C # design mode-interpreter mode

Source: Internet
Author: User

1. Interpreter Mode definition

Given a language, defines a representation of its grammar, and defines an interpreter that uses the representation to interpret a sentence in a language.

Description: The interpreter pattern is designed into grammar rules and abstract syntax trees.

2, the structure of the interpreter mode

The interpreter mode contains four roles:

1 abstract expression (abstractexpress): declares an abstract interpretation operation, which is a public parent class for all non-terminal expressions and non-terminal expressions.

2) non-terminal expression (terminalexpress): is a subclass of an abstract expression that implements the interpretation operation associated with the non-terminal in the grammar, and each non-terminal in the sentence is an instance of the class.

For non-terminal expressions, the code is relatively complex and can be combined into more complex structures through a non-terminal expression.

3) A non-terminal expression (nonterminalexpress): A subclass of an abstract expression that implements the interpretation of a non-terminal expression in a grammar. A non-terminal expression can be included in a non-terminal expression, or it can contain a non-terminal expression, and its interpretation operations are generally implemented by recursion.

4 Environment Class (context): Also known as the contextual class, used to store some global information outside the interpreter. In this class, you typically include a collection object of the type Hashtable or list, which stores a series of public information, such as the mapping of variable names to values (Key/value).

3, the advantages of the interpreter model

1) Easy to change or expand the grammar;

2 The realization of grammar is easier;

3) conforms to the opening and closing principle.

4, the shortcomings of the interpreter model

1 difficult to maintain for complex grammar;

2 implementation efficiency is low.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.