1 / 20

INTERPRETER

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

jimmiee
Download Presentation

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. INTERPRETER Main Topics What is an Interpreter. Why should we learn about them.

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

  3. Why Should we learn about Interpreters • Makes Your Life Easier • Amount of work • Amount of programming • Adaptability to new situations

  4. 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)

  5. 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

  6. 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

  7. 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 * )

  8. More Definitions • Sequence Expression • Determines if both objects are present (operator &)

  9. 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

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

  11. 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)

  12. Collaboration Diagram Participants: AbstractExpression, TerminalExpression, NonTerminalExpression, Context, Client. (p.245)

  13. Pros & Cons of Interpreters ANY GUESSES???

  14. 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

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

  16. 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.

  17. 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.

  18. Known Uses • The interpreter pattern is widely used in compilers implemented with object oriented languages • Example: SmallTalk

  19. 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

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

More Related