# Interpreter

<figure><img src="/files/rBCDFbgCV3VqU5NY3sKu" alt=""><figcaption></figcaption></figure>

### Introduction <a href="#id-1-gioi-thieu-0" id="id-1-gioi-thieu-0"></a>

* Interpreter is a design pattern that belongs to the Behavioral Pattern group.
* The Interpreter Pattern helps programmers "build" "dynamic" objects by reading a description of the object and then "building" the object according to that description.
* For example, write a program that allows users to enter the command line according to a pre-specified structure. The program will recognize the Command based on its structure and return appropriate results.

### Architecture <a href="#id-2-kien-truc-1" id="id-2-kien-truc-1"></a>

<figure><img src="https://images.viblo.asia/149851fe-2892-4859-a772-f141d5543523.png" alt=""><figcaption></figcaption></figure>

Components in the model:

* **AbstractionExpression:** Declare an interface for performing an operation.
* **TerminalExpression:** Implements an interpretation operation associated with terminal symbols, acting as a required expression for all terminal symbols in a sentence.
* **NonterminalExpression:** Can contain an internal TerminalExpression and can also contain another NonterminalExpression. It serves as the “grammar” of the specification language.
* **Context** : Is the information object to perform interpretation. This object is global to the interpretation process (shared between nodes).

### Advantages & disadvantages <a href="#id-3-uu--nhuoc-diem-2" id="id-3-uu--nhuoc-diem-2"></a>

**Advantage**

* Reduce dependency between abstraction and implementation (loose coupling).
* Reduce the number of unnecessary subclasses.
* The code will be cleaner and the application size will be smaller.
* Easier to maintain.
* Easy to expand later.
* Allows you to hide implementation details from the client.

**Disadvantage**

* The constructed specification language requires a simple grammatical structure.
* Performance is not guaranteed

### When to use it <a href="#id-4-khi-nao-thi-su-dung-3" id="id-4-khi-nao-thi-su-dung-3"></a>

Use Interpreter Pattern when we want:

* Simple grammar set. This pattern needs to define at least one class for each rule in the grammar. Therefore grammars containing many rules can be difficult to manage and maintain.
* Don't care much about performance. Because the grammar is analyzed in a hierarchical structure (tree), performance is not guaranteed.
* The Interpreter Pattern is often used in compilers, defining grammars, rules, SQL, XML parsers, etc.

### Related Design Patterns <a href="#id-6-design-pattern-lien-quan-5" id="id-6-design-pattern-lien-quan-5"></a>

* The abstract syntax tree is an implementation of the **Composite** pattern .
* Interpreters often use an **Iterator** to traverse the structure.
* Visitor can be used to maintain behavior on each node in the class's abstract syntax tree.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://huy312100.gitbook.io/software-development/design-pattern/behavior-design-patterns/interpreter.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
