1 / 12

Design Pattern Interpreter

Design Pattern Interpreter. By Swathi Polusani. What is an Interpreter?. The Interpreter pattern describes how to define a grammar for simple languages, represent sentences in the language, and interpret these sentences. Structure. Participants. Abstract Expression (Regular Expression)

kasen
Download Presentation

Design Pattern Interpreter

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Design Pattern Interpreter By Swathi Polusani

  2. What is an Interpreter? The Interpreter pattern describes how to define a grammar for simple languages, represent sentences in the language, and interpret these sentences.

  3. Structure

  4. Participants • Abstract Expression (Regular Expression) declares an abstract Interpret operation that is common to all nodes in the abstract syntax tree. • Terminal Expression (Literal Expression) implements an Interpret operation associated with terminal symbols in the grammar. • NonterminalExpression (Alternation Expression, Repetition Expression, Sequence Expressions) implements an Interpret operation for nonterminal symbols in the grammar. • Context contains information that's global to the interpreter. • Client builds (or is given) an abstract syntax tree representing a particular sentence in the language that the grammar defines and invokes the interpret operation. The abstract syntax tree is assembled from instances of the NonterminalExpression and Terminal Expression classes.

  5. Collaborations • The client builds the sentence as an abstract syntax tree of NonterminalExpression and TerminalExpression instances. Then the client initializes the context and invokes the Interpret operation. • Each NonterminalExpression node defines Interpret in terms of Interpret on each subexpression. • The Interpret operations at each node use the context to store and access the state of the interpreter.

  6. Example • expression ::= literal | alternation | sequence | repetition | '(' expression ')' • alternation ::= expression '|' expression • sequence ::= expression '&' expression • repetition ::= expression '*' • literal ::= 'a' | 'b' | 'c' | ... { 'a' | 'b' | 'c' | ... }*

  7. Abstract syntax tree

  8. Abstract syntax tree Regular expression: raining & (dogs | cats) *

  9. Applicability • Grammar is simple - due to the use of abstract syntax tree concept, the class hierarchy and representation of complex grammars becomes unmanageable. • When efficiency is not a critical concern. Known Uses • Compilers.

  10. Advantages It is easy to change and extend the grammar. Implementing the grammar is easy. • Disadvantages Complex grammars are hard to maintain due to addition of new sub class for every rule.

  11. Related Patterns • Composite: The abstract syntax tree is an instance of composite patterns. • Flyweight: Shows how to share terminal symbols within the abstract syntax tree. • Iterator: The interpreter can use this to traverse the structure. • Visitor: This can be used to maintain the behavior in each node.

  12. Thank you.

More Related