Interpreter Design Pattern

interpreter design pattern image

Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language. (dofactory)

  • AbstractExpression declares an interface for executing an operation
  • TerminalExpression implements an Interpret operation associated with terminal symbols in the grammar (An instance is required for every terminal symbol in the sentence).
  • NonterminalExpression One such class is required for every rule R ::= R1R2...Rn in the grammar. It maintains instance variables of type AbstractExpression for each of the symbols R1 through Rn and implements an Interpret operation for nonterminal symbols in the grammar. Interpret typically calls itself recursively on the variables representing R1 through Rn.
  • Context contains information that is global to the interpreter
  • Client 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. Client invokes the Interpret operation

  • The Interpreter pattern is used exhaustively in defining grammars, tokenize input and store it.
  • A specific area where Interpreter can be used are the rules engines.
  • The Interpreter pattern can be used to add functionality to the Composite pattern.

  • Map a domain to a language, the language to a grammar, and the grammar to a hierarchical object-oriented design.
  • Decouple the underlying expressions from the grammar.

  • As the grammar becomes more complicated, the maintenance effort is increased. 
  • The Interpreter pattern has a limited area where it can be applied. We can discuss the Interpreter pattern only in terms of formal grammars but in this area there are better solutions and this is the reason why this pattern is not so frequently used. This pattern can be applied for parsing light expressions defined in simple grammars and sometimes in simple rule engines.

Sample Code
Classic Interpreter pattern example 

Interpreter pattern using C# ExpressionTree example

Related Patterns
  • Interpreter can use State to define parsing contexts.
  • The abstract syntax tree of Interpreter is a Composite (therefore Iterator and Visitor are also applicable).
  • Terminal symbols within Interpreter’s abstract syntax tree can be shared with Flyweight.
  • The pattern does not address parsing. When the grammar is very complex, other techniques (such as a parser) are more appropriate.


  1. Nice info regarding interpreter Design pattern My sincere thanks for sharing this post Please Continue to share this post
    Dot Net Training in Chennai

  2. really nice blog has been shared by you. before i read this blog i didn't have any knowledge about this but now i got some knowledge. so keep on sharing such kind of an interesting blog.
    dot net training in chennai

  3. Great Article Image Processing Projects Deep Learning Projects for Final Year JavaScript Training in Chennai JavaScript Training in Chennai The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training Project Centers in Chennai

  4. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. I was exactly searching for. Thanks for such post and please keep it up. Great work. what does an interpreter do


Post a Comment

Popular Posts