Interpreter Design Pattern
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.
Classic Interpreter pattern example
Interpreter pattern using C# ExpressionTree example
- 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.
Nice info regarding interpreter Design pattern My sincere thanks for sharing this post Please Continue to share this postReplyDelete
Dot 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.ReplyDelete
dot net training in chennai
It was really an interesting blog, Thank you for providing unknown facts.ReplyDelete
Aviation 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
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 doReplyDelete
It’s a nice blog with very useful information!!!ReplyDelete
Web 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