Interpreter design pattern is used to define a problem in simple language and resolving it through interpreting its simple sentences.
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 is to define the grammar for a language and use the interpreter to interpret the simple sentences of the language
using this grammar.
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
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 it 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.
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
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
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 is from his courses on Design Patterns.
CourseGalaxy is focused on transformation of Engineering graduate to Software Engineer to fill the gap
between education and industry and provide the training on specific problems of industry to enhance the skills
of Engineers in industry to do their job appropriately.