An in-depth analysis of the interpreter mode of PHP design mode _php Tutorial

Source: Internet
Author: User
Interpreter (interpreter) mode, which consists of a grammar representation with a hierarchical hierarchy of compounds, rules are mapped to classes, and expressions following the grammar can be converted into an abstract syntax tree, with no content other than the instance object graph of the composite pattern.

A tree is an abstract noun because, in fact, most of the time it is an abstract representation of an expression that ignores a specific expression that might have a string or a data structure (for example, "a" and "\x41" are different representations of the same abstract literal in PHP), and the results are decoupled by logical rules , which greatly simplifies the interpretation process.

The interpreter is not a very common pattern, but for a simple syntax it is as easy as adding a class, but it does not solve the transformation from the concrete representation to the abstract syntax tree, which is done by other services.

The interpreter pattern is designed to take advantage of a composite hierarchy for a simple abstract expression (abstractexpression) method (interpreter operation), where the parameters of the interpreter operations are generally collectively referred to as contexts, and for a given method, they are usually replaced by computed values, or they may not exist for some operations.

Similarly, when an interpreter is included, the leaf of the compound pattern and the container participant name are different, and these names reflect the role they play: Terminator (terminal) or non-terminator (nonterminal) expressions.

participants:
Client: Use the interpret operation.
Abstract expression (abstractexpression): Based on an expression tree abstraction.
Non-terminator expression (nonterminalexpression): An expression that recursively contains other abstract expressions (Abstractexpression instances).
Terminator expression (terminalexpression): An expression that cannot be further simplified.


"Design mode" for this model provides a sample extension, I will use the mathematical expression substitution Boolean expression re-modified, so this example solves a mathematical expression, its evaluate () is separated in a different concreteexpression class.
Copy CodeThe code is as follows:
/**
* Abstractexpression. All implementations of this interface
* is concreteexpressions.
*/
Interface Mathexpression
{
/**
* Calculates the value assumed by the expression.
* Note that $values was passed to all expression but it
* is used by Variable only. This is required to abstract
* Away the tree structure.
*/
Public function evaluate (array $values);
}

/**
* A terminal expression which is a literal value.
*/
Class Literal implements Mathexpression
{
Private $_value;

Public function __construct ($value)
{
$this->_value = $value;
}

Public function evaluate (array $values)
{
return $this->_value;
}
}

/**
* A terminal expression which represents a variable.
*/
Class Variable implements Mathexpression
{
Private $_letter;

Public function __construct ($letter)
{
$this->_letter = $letter;
}

Public function evaluate (array $values)
{
return $values [$this->_letter];
}
}

/**
* nonterminal expression.
*/
Class Sum implements Mathexpression
{
Private $_a;
Private $_b;

Public function __construct (mathexpression $a, mathexpression $b)
{
$this->_a = $a;
$this->_b = $b;
}

Public function evaluate (array $values)
{
return $this->_a->evaluate ($values) + $this->_b->evaluate ($values);
}
}

/**
* nonterminal expression.
*/
Class Product implements Mathexpression
{
Private $_a;
Private $_b;

Public function __construct (mathexpression $a, mathexpression $b)
{
$this->_a = $a;
$this->_b = $b;
}

Public function evaluate (array $values)
{
return $this->_a->evaluate ($values) * $this->_b->evaluate ($values);
}
}

Ten (A + 3)
$expression = new Product (new Literal, New Sum (New Variable (' A '), New Literal (3)));
echo $expression->evaluate (Array (' a ' = 4) '), ' \ n ';
Adding new rules to the grammar are easy:
E.g Power, subtraction ...
Thanks to the Composite, manipulation is even simpler:
We could add substitute ($letter, mathexpression $expr)
To the interface ...

http://www.bkjia.com/PHPjc/327555.html www.bkjia.com true http://www.bkjia.com/PHPjc/327555.html techarticle Interpreter (interpreter) pattern, which consists of a grammatical representation of a hierarchical structure with a composite class, the rules are mapped to classes, and the expressions following the grammar can be converted into an abstraction ...

  • 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.