Presentation is loading. Please wait.

Presentation is loading. Please wait.

Design Pattern Interpreter By Swathi Polusani. What is an Interpreter? The Interpreter pattern describes how to define a grammar for simple languages,

Similar presentations


Presentation on theme: "Design Pattern Interpreter By Swathi Polusani. What is an Interpreter? The Interpreter pattern describes how to define a grammar for simple languages,"— 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 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.


Download ppt "Design Pattern Interpreter By Swathi Polusani. What is an Interpreter? The Interpreter pattern describes how to define a grammar for simple languages,"

Similar presentations


Ads by Google