Design Patterns-behavior patterns-Interpreter patterns (Interpreter)

Source: Internet
Author: User

Overview
Given a language, it defines a representation of its syntax and an interpreter that uses this representation to interpret sentences in the language.
Applicability
When a language needs to be interpreted and executed, and you can represent sentences in the language as an abstract syntax tree, you can use the interpreter mode. This mode works best in the following situations:
This grammar is simple for complex grammar, and the class layers of grammar become huge and cannot be managed. At this time, the syntax analysis program generator is a better tool. They can interpret expressions without building an abstract syntax tree, which saves space and time.
Efficiency is not a key issue. The most efficient interpreter is usually not implemented by directly interpreting the syntax analysis tree. Instead, they are first converted into another form. For example, regular expressions are usually converted to state machines. However, even in this case, the converter can still be implemented in interpreter mode, which is still useful.
Participants
1. AbstractExpression (Abstract Expression)
Declare an abstract interpretation operation, which is shared by all nodes in the abstract syntax tree.

2. TerminalExpression (Terminator expression)
Implements interpretation operations associated with Terminators in grammar.
Each terminator in a sentence needs an instance of this class.

3. NonterminalExpression (non-terminator expression)
Implements the interpretation (Interpret) operation for non-terminator in the grammar.

4. Context)
Contains global information other than the interpreter.

5. Client (customer)
Construct (or be given) an abstract syntax tree that represents a specific sentence in the language defined by the syntax.
The abstract syntax tree is composed of NonterminalExpression and TerminalExpression instances.
Call the explain operation.
Example:
A bool expression and a date expression interpreter interpret the context.
Package com. sql9.actioned;
 
Import java. text. ParseException;
 
Abstract class Expression {
String val;
Public abstract void interpret (Context ctx );
}
 
Class BoolExpression extends Expression {
Public BoolExpression (String val ){
Super. val = val; www.2cto.com
}
@ Override
Public void interpret (Context ctx ){
Boolean v = Boolean. valueOf (ctx. exp. val );
System. out. println ("value parsed in BoolExpression is:" + v );
}
}
 
Class DateExpression extends Expression {
Public DateExpression (String val ){
Super. val = val;
}
@ Override
Public void interpret (Context ctx ){
String v = ctx. exp. val;
Try {
Java. util. Date date = new java. text. SimpleDateFormat ("yyy-mm-dd"). parse (v );
System. out. println ("DateExpression parsed value:" + date );
} Catch (ParseException e ){
System. out. println ("Invalid date in the source value parsed in DateExpression ");
}
}

}
 
Class Context {
Expression exp;
Public void assign (Expression exp ){
This. exp = exp;
}
}
 
 
Public class InterpreterTest {
 
Public static void main (String [] args ){
Expression exp = new BoolExpression ("false ");
Context context = new Context ();
Context. assign (exp );
Exp. interpret (context );

Exp = new DateExpression ("2008-09-01 ");
Context. assign (exp );
Exp. interpret (context );
}
 
}

Result
Value parsed in BoolExpression is: false
DateExpression parsed values: Tue Jan 01 00:09:00 CST 2008


 

Related Article

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.