Java and mode 26-25th-interpreter Mode

Source: Internet
Author: User
Document directory
  • Source code

The interpreter mode is the behavior mode of the class. Given a language, the interpreter mode can define a representation of its syntax and provide an interpreter at the same time. The client can use this interpreter to explain sentences in this language.

Interpreter mode structure

  The following uses a schematic system as an example to discuss the structure of the interpreter mode. The system structure is as follows:

The role involved in the mode is as follows:

  (1) Abstract Expression role:Declare an abstract interface that all specific expression roles need to implement. This interface is mainly an interpret () method, which is called an interpreted operation.

  (2) terminal expression role:Implements the interface required by the abstract expression role, mainly an interpret () method. Each terminator in the grammar has a specific Terminator corresponding to it. For example, there is a simple formula r = R1 + R2 in which R1 and R2 are the Terminator, and the interpreter for parsing R1 and R2 is the terminator expression.

  (3) Non-terminal expression (nonterminal expression) role:Each rule in the grammar requires a specific non-terminator expression. A non-terminator expression is generally an operator or other keyword in the grammar, such as in the formula r = R1 + R2, "+" is a non-Terminator, and the interpreter for parsing "+" is a non-terminator expression.

  (4) Environment (context) 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.

 

  To illustrate the implementation of the interpreter mode, we provide the simplest syntax and the implementation of the corresponding interpreter mode. This is to simulate the operation and evaluation of boolean expressions in Java.

In this language, the Terminator is a Boolean variable, that is, constants true and false. Non-terminator expressions include boolean expressions such as and, or, and not. This simple syntax is as follows:

Expression: = constant | variable | or | and | not

And: = expression' and 'Expression

Or: = expression' or 'Expression

Not: = 'not' expression

Variable: = any identifier

Constant: = 'true' | 'false'

The structure of the interpreter mode is as follows:

 

Source code

Abstract Expression role

Package COM. bankht. interpreter;/*** @ Author: -AK47 * @ Creation Time: 03:08:58 pm, ** @ Class description: abstract Expression role */public abstract class expression {/*** is based on the environment. This method interprets any given expression */Public Abstract Boolean interpret (context CTX ); /*** check whether the two expressions are in the same structure */Public Abstract Boolean equals (Object OBJ ); /*** return the expression's hash code */public abstract int hashcode ();/*** convert the expression to a string */public abstract string tostring ();}

A constant object represents a Boolean constant

Package COM. bankht. interpreter;/*** @ Author: -AK47 * @ Creation Time: 03:09:35 pm, ** @ Class description: A constant object represents a Boolean constant */public class constant extends expression {private Boolean value; Public constant (Boolean value) {This. value = value ;}@ overridepublic Boolean equals (Object OBJ) {If (OBJ! = NULL & OBJ instanceof constant) {return this. value = (constant) OBJ ). value;} return false;} @ overridepublic int hashcode () {return this. tostring (). hashcode () ;}@ overridepublic Boolean interpret (context CTX) {return value ;}@ overridepublic string tostring () {return new Boolean (value ). tostring ();}}

A variable object represents a famous variable.

Package COM. bankht. interpreter;/*** @ Author: -AK47 * @ Creation Time: 03:10:16 pm, ** @ Class description: A variable object represents a famous variable */public class variable extends expression {private string name; Public variable (string name) {This. name = Name ;}@ overridepublic Boolean equals (Object OBJ) {If (OBJ! = NULL & OBJ instanceof variable) {return this. name. equals (variable) OBJ ). name);} return false;} @ overridepublic int hashcode () {return this. tostring (). hashcode () ;}@ overridepublic string tostring () {return name ;}@ overridepublic Boolean interpret (context CTX) {return CTX. lookup (this );}}

Represents the and class of the logical "and" operation, indicating that the operation of a new Boolean expression is given by two boolean expressions through the logical "and" Operation

Package COM. bankht. interpreter;/*** @ Author: -AK47 * @ Creation Time: 03:10:55 ** @ Class description: represents the and class of the logical "and" operation, two boolean expressions are used to operate a new Boolean expression through the logical "and" Operation */public class and extends expression {private expression left, right; Public and (expression left, expression right) {This. left = left; this. right = right ;}@ overridepublic Boolean equals (Object OBJ) {If (OBJ! = NULL & OBJ instanceof and) {return left. equals (and) OBJ ). left) & right. equals (and) OBJ ). right);} return false;} @ overridepublic int hashcode () {return this. tostring (). hashcode () ;}@ overridepublic Boolean interpret (context CTX) {return left. interpret (CTX) & right. interpret (CTX) ;}@ overridepublic string tostring () {return "(" + left. tostring () + "and" + right. tostring () + ")";}}

The or class that represents the logical "or" operation. It indicates that the operation of a new Boolean expression is given by two boolean expressions through the logical "or" operation.

Package COM. bankht. interpreter;/*** @ Author: -AK47 * @ Creation Time: 03:11:20 ** @ Class description: The or class that represents the logical "or" operation, represents the operation of a new Boolean expression through two boolean expressions through logical "or" operations */public class or extends expression {private expression left, right; public or (expression left, expression right) {This. left = left; this. right = right ;}@ overridepublic Boolean equals (Object OBJ) {If (OBJ! = NULL & OBJ instanceof or) {return this. left. equals (OR) OBJ ). left) & this. right. equals (OR) OBJ ). right);} return false;} @ overridepublic int hashcode () {return this. tostring (). hashcode () ;}@ overridepublic Boolean interpret (context CTX) {return left. interpret (CTX) | right. interpret (CTX) ;}@ overridepublic string tostring () {return "(" + left. tostring () + "or" + right. tostring () + ")";}}

A Not class that represents a logical "Non" operation. It indicates that a new Boolean expression operation is provided by a Boolean expression through a logical "Non" operation.

Package COM. bankht. interpreter;/*** @ Author: -AK47 * @ Creation Time: 03:11:36 ** @ Class description: Not class indicating logical "not" operations, represents the operation of a new Boolean expression through a logical "Non" Operation */public class not extends expression {private expression exp; public not (expression exp) {This. exp = exp ;}@ overridepublic Boolean equals (Object OBJ) {If (OBJ! = NULL & OBJ instanceof not) {return exp. equals (not) OBJ ). exp);} return false;} @ overridepublic int hashcode () {return this. tostring (). hashcode () ;}@ overridepublic Boolean interpret (context CTX) {return! Exp. interpret (CTX) ;}@ overridepublic string tostring () {return "(not" + exp. tostring () + ")";}}

 

The context class defines a ing from a variable to a Boolean value.

 

Package COM. bankht. interpreter; import Java. util. hashmap; import Java. util. map;/*** @ Author: -AK47 * @ Creation Time: 03:11:54 ** @ Class description: Environment (context) class defines a ing from the variable to the Boolean value */public class context {private Map <variable, Boolean> map = new hashmap <variable, Boolean> (); public void assign (variable VAR, Boolean value) {map. put (VAR, new Boolean (value);} public Boolean Lookup (variable VAR) throws illegalargumentexception {Boolean value = map. get (VAR); If (value = NULL) {Throw new illegalargumentexception ();} return value. booleanvalue ();}}

Client type

Package COM. bankht. interpreter;/*** @ Author: -AK47 * @ Creation Time: 03:12:17 pm, ** @ Class description: client class */public class client {public static void main (string [] ARGs) {context CTX = new context (); variable X = new variable ("X "); variable y = new variable ("Y"); constant C = new constant (true); CTX. assign (x, false); CTX. assign (Y, true); expression exp = new or (new and (C, x), new and (Y, new not (X); system. out. println ("x =" + X. interpret (CTX); system. out. println ("Y =" + Y. interpret (CTX); system. out. println (exp. tostring () + "=" + exp. interpret (CTX ));}}

The running result is as follows:

x=falsey=true((true AND x) OR (y AND (Not x)))=true

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.