Interpreter Design Pattern
Definition
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
Applicability
- 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.
Consequences
- 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.
Downsides
- 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.
Further readings
http://en.wikipedia.org/wiki/Interpreter_pattern
http://ayende.com/blog/160705/design-patterns-in-the-test-of-time-interpreter
Nice info regarding interpreter Design pattern My sincere thanks for sharing this post Please Continue to share this post
ReplyDeleteDot Net Training in Chennai
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.
ReplyDeletedot net training in chennai
great
ReplyDeleteIt was really an interesting blog, Thank you for providing unknown facts.
ReplyDeleteAviation Courses in Chennai
air hostess institute in Chennai
airline and airport management courses in Chennai
airport ground staff training courses in Chennai
Aviation Academy in Chennai
air hostess training in Chennai
airport management courses in Chennai
ground staff training in Chennai
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
ReplyDeleteWe 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
ReplyDeleteIt’s a nice blog with very useful information!!!
ReplyDeleteWeb Development courses in Chennai
PHP Training Institute in Chennai
Spoken English in Chennai
German Language Classes in Chennai
salesforce training institute in chennai
IELTS Training in Chennai