INTERPRETER Main Topics What is an Interpreter Why

  • Slides: 20
Download presentation
INTERPRETER Main Topics What is an Interpreter. Why should we learn about them.

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

Simple Definition Interpreter: A medium through which unrecognized information is changed into a form that can be recognized.

Why Should we learn about Interpreters l Makes Your Life Easier – Amount of

Why Should we learn about Interpreters l Makes Your Life Easier – Amount of work – Amount of programming – Adaptability to new situations

Why Should we learn about Interpreters l Makes Your Life Easier – Amount of

Why Should we learn about Interpreters l Makes Your Life Easier – Amount of work l l 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 l Makes Your Life – Amount of work

Why Should we learn about Interpreters l Makes Your Life – Amount of work l Easier The interpreter pattern describes how to define a grammar for a simple language, how to representences in the language and interpret those sentences

Why Should we learn about Interpreters l Makes Your Life Easier – Amount of

Why Should we learn about Interpreters l Makes Your Life Easier – Amount of programming l l The interpreter pattern uses a simple class to represent each grammar rule. First, some more definitions

More Definitions – Literal Expression l This determines if there is an exact match

More Definitions – Literal Expression l This determines if there is an exact match of two objects ( operator = ) – Alternation Expression Is there an alternate expression that is acceptable (operator | ) l – Repetition Expression l Does this repeat itself (operator * )

More Definitions – Sequence Expression l Determines if both objects are present (operator &)

More Definitions – Sequence Expression l Determines if both objects are present (operator &)

Class Diagram Every regular expression defined by this grammar is represented by an abstract

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 l This tree represents the regular expression – Raining & (dog |

Syntax Tree l This tree represents the regular expression – Raining & (dog | cat)* – Diagram p. 244

Why Should we learn about Interpreters l Makes Your Life Easier – Amount of

Why Should we learn about Interpreters l Makes Your Life Easier – Amount of programming l Works best when – The grammar is simple • (class hierarchy) – Efficiency is not a concern • (space & time management)

Collaboration Diagram Participants: Abstract. Expression, Terminal. Expression, Non. Terminal. Expression, Context, Client. (p. 245)

Collaboration Diagram Participants: Abstract. Expression, Terminal. Expression, Non. Terminal. Expression, Context, Client. (p. 245)

Pros & Cons of Interpreters ANY GUESSES? ? ?

Pros & Cons of Interpreters ANY GUESSES? ? ?

Pros & Cons of Interpreters l It’s easy to change and extend the grammar

Pros & Cons of Interpreters l It’s easy to change and extend the grammar l Inheritance – existing expressions can be modified, and new expressions can be defined as variations of old ones l. Implementing l At least that’s what the book says l. Adding l the grammar is ‘easy’ too. new ways to interpret expressions. Flexible - Tokenizers

Pros & Cons of Interpreters l Complex l Grammars are hard to maintain The

Pros & Cons of Interpreters l Complex l Grammars are hard to maintain The interpreter pattern defines at least one class for each rule.

Implementation l Creating l the abstract syntax tree The interpreter pattern does not explain

Implementation l Creating l the abstract syntax tree The interpreter pattern does not explain how to create an abstract syntax tree. l Sharing terminal symbols with the flyweight pattern l Grammars who’s sentences contain many occurrences of a terminal symbol might benefit from sharing a single copy of that symbol.

Implementation l Creating l the abstract syntax tree You can (but have the option

Implementation l Creating l the abstract syntax tree You can (but have the option of not) define the Interpret operation in your expression classes. – Sequence. Expression, Literal. Expression… etc.

Known Uses l The interpreter pattern is widely used in compilers implemented with object

Known Uses l The interpreter pattern is widely used in compilers implemented with object oriented languages – Example: Small. Talk

Related Patterns – Composite (p. 163): the abstract syntax tree is an instance of

Related Patterns – Composite (p. 163): the abstract syntax tree is an instance of the composite pattern – Fly. Weight (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 • Small. Talk can be found on

Final Questions or Comments • Sample code • Small. Talk can be found on pages 248 – 251 • C++ can be found on pages 251 - 255