Title: INTERPRETER
1INTERPRETER
- Main Topics
- What is an Interpreter.
- Why should we learn about them.
2Simple Definition
- Interpreter
- A medium through which unrecognized
information is changed into a form that can be
recognized.
3Why Should we learn about Interpreters
- Makes Your Life Easier
- Amount of work
- Amount of programming
- Adaptability to new situations
4Why 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)
5Why 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
6Why 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
7More 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 )
8More Definitions
- Sequence Expression
- Determines if both objects are present (operator
)
9Class Diagram
Every regular expression defined by this grammar
is represented by an abstract syntax tree made up
of instances of these classes. p.244
10Syntax Tree
- This tree represents the regular expression
- Raining (dog cat)
- Diagram p.244
11Why 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)
12Collaboration Diagram
Participants AbstractExpression,
TerminalExpression, NonTerminalExpression,
Context, Client. (p.245)
13Pros Cons of Interpreters
ANY GUESSES???
14Pros Cons of Interpreters
- Its 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 thats what the book says
- Adding new ways to interpret expressions.
- Flexible - Tokenizers
15Pros Cons of Interpreters
- Complex Grammars are hard to maintain
- The interpreter pattern defines at least one
class for each rule.
16Implementation
- 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 whos sentences contain many occurrences
of a terminal symbol might benefit from sharing a
single copy of that symbol.
17Implementation
- Creating the abstract syntax tree
- You can (but have the option of not) define the
Interpret operation in your expression classes. - SequenceExpression, LiteralExpression etc.
18Known Uses
- The interpreter pattern is widely used in
compilers implemented with object oriented
languages - Example SmallTalk
19Related 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
20Final Questions or Comments
- Sample code
- SmallTalk can be found on pages 248 251
- C can be found on pages 251 - 255