Interpreter
Usage
- Also known as: Little Language
- A way to include language elements in a program
- Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language
Diagram
Participants
AbstractExpression
TerminalExpression
- Declares an interface for executing an operation
NonterminalExpression
- Implements an Interpret operation associated with terminal symbols in the grammar.
- An instance is required for every terminal symbol in the sentence.
Context
- One such class is required for every rule R ::= R1R2...Rn in the grammar
- Maintains instance variables of type AbstractExpression for each of the symbols R1 through Rn.
- Implements an Interpret operation for nonterminal symbols in the grammar. Interpret typically calls itself recursively on the variables representing R1 through Rn.
Client
- Contains information that is global to the interpreter.
- Builds (or is given) an abstract syntax tree representing a particular sentence in the language that the grammar defines.
- The abstract syntax tree is assembled from instances of the NonterminalExpression and TerminalExpression classes.
- Invokes the Interpret operation.
Resources URL:
notes/design_patterns/resources
Sources URL:
notes/design_patterns/sources
