As stated in GoF, p243 :
Given a language, define a repreentation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.
In a few words :
- Process structured text data by tokenizing && parsing
Examples of use
Interpreters are everywhere - especially in programming languages - where the front end of every compiler is an Interpreter. They are also used for mathematical expressions that need to be interpreted before being processed. (See the provided sample code for an example).
Participants
- AbstractExpression : Abstract interpret operation common to all nodes in the abstract syntax tree.
- TerminalExpression : Implements an interpret operation associated with terminal symbols in the grammar.
- NonTerminalExpression :
- One for each rule in the grammar.
- Implements an interpret operation for nonterminal symbols in the grammar. Context : Contains informations that's global to the interpreter. Client :
- Builds (or is given) an abstract syntax tree representing a particular sentence in the language that the grammar defines.
- Inokes the interpret operation.
Note : UML class diagram taken from here
Pros
- Easy to change and extend the grammar.
Here are some usefull ressources :
