Pattern > Créateurs

Pattern > Structuraux

Pattern > Comportementaux

Références

L'actualité

Librairie

L'information

Design pattern > Comportementaux > Interpreter

Le patron comporte deux composants centraux : le contexte et l'expression ainsi que des objets qui sont des représentations d'éléments de grammaire d'un langage de programmation. Le patron est utilisé pour transformer une expression écrite dans un certain langage de programmation - un texte source - en quelque chose de manipulable par programmation : Le code source est écrit conformément à une ou plusieurs règles de grammaire, et un objet est créé pour chaque utilisation d'une règle de grammaire. L'objet interpreter est responsable de transformer le texte source en objets.


AbstractExpression (Expression)
  • declares an interface for executing an operation
TerminalExpression ( ThousandExpression, HundredExpression, TenExpression, OneExpression )
  • implements an Interpret operation associated with terminal symbols in the grammar.
  • an instance is required for every terminal symbol in the sentence.
NonterminalExpression ( not used )
  • 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.
Context (Context)
  • contains information that is global to the interpreter
Client (InterpreterApp)
  • 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
			
MCMXXVIII = 1928