230 likes | 270 Views
INTERPRETER. Main Topics What is an Interpreter. Why should we learn about them. Simple Definition. Interpreter: A medium through which unrecognized information is changed into a form that can be recognized. Why Should we learn about Interpreters. Makes Your Life Easier Amount of work
E N D
INTERPRETER Main Topics What is an Interpreter. Why should we learn about them.
Simple Definition Interpreter: A medium through which unrecognized information is changed into a form that can be recognized.
Why Should we learn about Interpreters • Makes Your Life Easier • Amount of work • Amount of programming • Adaptability to new situations
Why Should we learn about Interpreters • Makes Your Life Easier • Amount of work • If a problem occurs often enough, you might want to express instances of the problem as a simple language. • Example: String Matching (Regular Expressions)
Why Should we learn about Interpreters • Makes Your Life Easier • Amount of work • The interpreter pattern describes how to define a grammar for a simple language, how to represent sentences in the language and interpret those sentences
Why Should we learn about Interpreters • Makes Your Life Easier • Amount of programming • The interpreter pattern uses a simple class to represent each grammar rule. • First, some more definitions
More Definitions • Literal Expression • This determines if there is an exact match of two objects ( operator = ) • Alternation Expression • Is there an alternate expression that is acceptable (operator | ) • Repetition Expression • Does this repeat itself (operator * )
More Definitions • Sequence Expression • Determines if both objects are present (operator &)
Class Diagram Every regular expression defined by this grammar is represented by an abstract syntax tree made up of instances of these classes. p.244
Syntax Tree • This tree represents the regular expression • Raining & (dog | cat)* • Diagram p.244
Why Should we learn about Interpreters • Makes Your Life Easier • Amount of programming • Works best when • The grammar is simple • (class hierarchy) • Efficiency is not a concern • (space & time management)
Collaboration Diagram Participants: AbstractExpression, TerminalExpression, NonTerminalExpression, Context, Client. (p.245)
Pros & Cons of Interpreters ANY GUESSES???
Pros & Cons of Interpreters • It’s easy to change and extend the grammar • Inheritance – existing expressions can be modified, and new expressions can be defined as variations of old ones • Implementing the grammar is ‘easy’ too. • At least that’s what the book says • Adding new ways to interpret expressions. • Flexible - Tokenizers
Pros & Cons of Interpreters • Complex Grammars are hard to maintain • The interpreter pattern defines at least one class for each rule.
Implementation • Creating the abstract syntax tree • The interpreter pattern does not explain how to create an abstract syntax tree. • Sharing terminal symbols with the flyweight pattern • Grammars who’s sentences contain many occurrences of a terminal symbol might benefit from sharing a single copy of that symbol.
Implementation • Creating the abstract syntax tree • You can (but have the option of not) define the Interpret operation in your expression classes. • SequenceExpression, LiteralExpression… etc.
Known Uses • The interpreter pattern is widely used in compilers implemented with object oriented languages • Example: SmallTalk
Related Patterns • Composite (p.163): the abstract syntax tree is an instance of the composite pattern • FlyWeight (p.193): shows how to share terminal symbols within the abstract syntax tree. • Iterator (p.257): can be used to traverse the structure. • Visitor (p.331): can be used to maintain the behavior in each node in the abstract syntax tree in one class
Final Questions or Comments • Sample code • SmallTalk can be found on pages 248 – 251 • C++ can be found on pages 251 - 255