The interpreter pattern is a behavioural design pattern Chain of responsibility, Command, Interpreter, Iterator, Mediator, Memento, Observer, State, Strategy, Template method, Visitor that specifies how to evaluate sentences in a language. The basic idea is to have a class for each symbol (terminal or nonterminal) in a specialised computer language. The syntax tree of a sentence in the language is an instance of the composite pattern and is used to evaluate (interpret) the sentence for a client.
Overview
- What problems can the Interpreter design pattern solve?
- A grammar for a simple language should be defined
- so that sentences in the language can be interpreted.
When a problem occurs very often, it could be considered to represent it as a sentence in a simple language (Domain Specific Languages) so that an interpreter can solve the problem by interpreting the sentence.
- What solution does the Interpreter design pattern describe?
- Define a grammar for a simple language by defining an Expression class hierarchy and implementing an interpret() operation.
- Represent a sentence in the language by an abstract syntax tree (AST) made up of Expression instances.
- Interpret a sentence by calling interpret() on the AST.
The Interpreter pattern doesn't describe how to build an abstract syntax tree. This can be done either manually by a client or automatically by a parser.
No comments:
Post a Comment