INTERPRETER - PowerPoint PPT Presentation

About This Presentation
Title:

INTERPRETER

Description:

The interpreter pattern describes how to define a grammar for a simple language, ... Pros & Cons of Interpreters. Complex Grammars are hard to maintain ... – PowerPoint PPT presentation

Number of Views:452
Avg rating:3.0/5.0
Slides: 21
Provided by: SYS562
Learn more at: http://www.cs.rpi.edu
Category:

less

Transcript and Presenter's Notes

Title: INTERPRETER


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
  • 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

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 whos 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
Write a Comment
User Comments (0)
About PowerShow.com