CoursesGalaxy Courses Subscription Knowledge Hub
Knowledge Hub
Design Patterns
    Creational Patterns
    Structural Patterns
    Behavioral Patterns
        Chain of Responsibility
        Template Method

Interpreter Design Pattern

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 sentences.

  • 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.

Interpreter Design Pattern

Participant classes
  • 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.

Implementation Code

C++   Interpreter Pattern in C++   Example in C++
C#   Interpreter Pattern in C#   Example in C#
Java   Interpreter Pattern in Java   Example in Java
Python   Interpreter Pattern in Python   Example in Python
JavaScript   Interpreter Pattern in JavaScript   Example in JavaScript
PHP   Interpreter Pattern in PHP   Example in PHP
Ruby   Interpreter Pattern in Ruby   Example in Ruby
Swift   Interpreter Pattern in Swift   Example in Swift
Objective-C   Interpreter Pattern in Objective-C   Example in Objective-C

Suresh Kumar Srivastava is founder of online learning site 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.

Yearly Subscription

Design Patterns in PHP

Object Oriented C#




Knowledge Hub

Design Patterns

Tech Articles

About Us


Contact Us


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.

Learn From Us

2018 CourseGalaxy