Atitit. Interpreter mode frame selection and application Scenario attilax summary OAO
1. Interpreter Mode Structure Description 1
2. How to implement ( simple interpreter mode, only through lexical analysis can be achieved, without the token stream processing. 2
3. The word stream must be recognized as reserved words, identifiers ( variables ), constants, operators (operators) and bounds of the five major classes 2
3.1. Operator (Operators)::: 2
3.2.4. interface characters: " ; " semicolon ," {} "curly braces, single quotes, double quotes 3
4. Terminalexpression and nonterminalexpression are two implementations. 3
5. Application Scenario 3
5.1. Conversion of Chinese characters to numbers 3
5.2. Sql,hql,criteria Convert Hibernate 3
5.3. Expression Parsing 4
5.4. Translation Dictionaries 4
6. Interpreter Frame Selection 4
1. Interpreter Mode Structure description
Context stores the global context environment, abstractexpression is the interface that all expressions must inherit, and Terminalexpression and Nonterminalexpression are two implementations.
In addition to the classes used to represent expressions, an environment class is usually provided in the interpreter pattern Context, which is used to store some global information, usually contains a collection object of type HashMap or ArrayList (or a collection class such as HashMap, which can also act as an environment class), storing a series of public information, such as the mapping of a variable name to a value (key /value), which is used to obtain relevant information during the specific interpretation operation. The typical code snippet is as follows:
Class Context { Private HashMap map = new HashMap (); public void assign (string key, String value) { Setting values in the Environment class } public string Lookup (string key) { Gets the value stored in the Environment class } } |
when the system does not need to provide global public information can omit the environment class, depending on the actual situation to decide whether or not to require an environment class.
Author:: Old Wow's paw attilax Ayron, email:[email protected]
Reprint please indicate source: Http://blog.csdn.net/attilax
2. How to implement (Simple interpreter mode, only through lexical analysis can be achieved, without the token stream processing.
In fact, there are some simple interpreter patterns that can be implemented only through lexical analysis, and functions may be written in state-changing functions without the need to process the resulting token stream.
3. The word stream must be recognized as reserved words, identifiers (variables), constants, operators (operators) and bounded characters in five major categories
3.1. Operator (Operators):::
() [].
? : |
Conditions |
From right to left |
() []. |
Parentheses (functions, etc.), arrays, two struct members access |
From left to right |
, |
Comma (order) |
+ - |
Add, Subtract |
From left to right |
Brackets , spinning brackets , equals sign
Reference
Compiler DIY--Lexical analysis -godlike- Blog channel -CSDN.NET.htm
operator to use a state to describe the ...
3.2.4.interface characters:";"Semicolon,"{}" curly braces,single quotes, double quotes
interface in the processing of the time , the Forest bar filter ...
4. Terminalexpression and Nonterminalexpression are two implementations.
In general , operators (operators) are implemented using Nonterminalexpression to Create a class to implement ,
An op a class
5. Application Scenarios
5.1. Conversion of Chinese characters to digital
Of the project, with the need to parse the Chinese character data more and more large, need to resolve the method to deal with the larger level of data (million, billion ... ), by extending the Express class to produce a processing method that resolves the new level
5.2. Sql,hql,criteria ConversionHibernate
Criteria 2sql can use the HB API
5.3. Expression parsing 5.4. Translation dictionaries
6. Interpreter Frame Selection
6.0.1.1. Best practices
The interpreter pattern is used very little in the actual system development because it can cause problems such as efficiency, performance and maintenance, which can be found in large and medium-sized framework projects, such as some data analysis tools, report design tools, scientific computing tools and so on, if you do encounter"a particular type of problem occurs at a sufficiently high frequency"situation, when you are ready to use the interpreter mode, considerexpression4j,MESP(Math Expression String Parser),Jepand other Open Source parsing toolkit (these three open source products can be Baidu,GoogleSearch, please the reader Self-query), the function is very powerful, and very easy to use, the efficiency is also good, to achieve most of the mathematical operation is no problem, I do not have to start from scratch to write an interpreter, someone has established a road, why go to their own muddy path it?
expression4j Baidu
MESP interpreter
Reference
Implementation of custom language--interpreter mode (iii) - Liu Wei technology blog - Blog channel -CSDN.NET.htm
Atitit. Interpreter mode frame selection and application Scenario Attilax summary OAO