Interpreter Design Pattern
Interpreter design pattern is used to define a problem in simple language and resolving it through interpreting its simple sentences.
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.
- Given a language, define a representation for its grammar along with an interpreter that uses the representation to
interpret sentences in the language.
So the set of problem is well defined and coming repeatedly. As it is well defined, it can be defined in language and simple
- A set of defined problem is coming again and again.
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.
- Define the problem in sentences of language and interpret these sentences to solve the problem.
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.
How they work together?
- 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.
- 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.
Design Patterns in PHP
Object Oriented C#