Interpreter design pattern is used to define a problem in simple language and resolving it through interpreting its simple sentences.
Intent
Given a language, define a representation for its grammar along with an interpreter that uses the representation to
interpret sentences in the language.
The intent of interpreter design pattern is to define the grammar for a language and use the interpreter to interpret the simple sentences of the language
using this grammar.
Problem
A set of defined problem is coming again and again.
So the set of problem is well defined and coming repeatedly. As it is well defined, it can be defined in language and simple
sentences.
Solution
Define the problem in sentences of language and interpret these sentences to solve the problem.
So the set of well-defined problem can be defined in simple sentences of language. We can come up with interpreter which can
interpret these sentences to solve the problem. So the solution comes in form of problem defined in language and grammar, and
interpreter is used to interpret it.
Where interpreter design pattern is applicable?
A set of well-defined problem can be represented in simple sentences of language. That means the grammar of language
has to be simple, otherwise parser tools are better alternative.
Interpreter Design Pattern UML Structure
Participant classes of interpreter design pattern
AbstractExpression class provides interface Interpret().
TerminalExpression class represents the terminal symbol in grammar and implements method Interpret().
NonTerminalExpression class represents rule in the grammar and will be there for every rule. This implements
Interpret() method and interprets every rule considering other rules.
Context class has the context information and will be used while interpretation of Terminal and Non Terminal
expressions.
Client has the representation of simple sentences of language with defined grammar. This is in form of terminal
and Non terminal expressions.
How they work together?
The client builds or gets the abstract syntax tree from the simple sentences of language with defined grammar. Here
sentence represents the node of tree either terminal or non-terminal expression. The rule of grammar represents the
classes.
Client has the context information and invokes the Interpret() method. NonTerminalExpression interprets the expression
based on rule of the grammar. The TerminalExpression interprets the expression as terminal. Interpreter knows the context
information through context for interpreting expression.
So we can have interpreter to solve the problem defined in simple sentences of language with defined grammar. Here it
is easy to change and add new grammar too. We can easily modify the interpreter also. But it will be better to use parser
tools if grammar is complex.
There is an opportunity to use Flyweight pattern for terminal expression if they are in large number as intrinsic and
extrinsic state is easily distinguishable.
Suresh Kumar Srivastava is founder of online learning site coursegalaxy.com and author of popular books C In Depth,
Data Structures Through C In Depth. He has 18+ years experience in industry and worked on architecture and design of
multiple products. This article Interpreter Design Pattern is from his Design Patterns course.