0% found this document useful (0 votes)
47 views15 pages

Interpreter Pattern - Behavioural!: - Intent"

The Interpreter pattern describes how to define a grammar for languages, represent sentences in the language as abstract syntax trees, and interpret those trees. It is useful when a problem occurs often enough that it can be represented as a domain-specific language. The pattern defines classes for each grammar rule that interpret sentences by traversing the syntax tree.

Uploaded by

ali abbas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views15 pages

Interpreter Pattern - Behavioural!: - Intent"

The Interpreter pattern describes how to define a grammar for languages, represent sentences in the language as abstract syntax trees, and interpret those trees. It is useful when a problem occurs often enough that it can be represented as a domain-specific language. The pattern defines classes for each grammar rule that interpret sentences by traversing the syntax tree.

Uploaded by

ali abbas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Interpreter Pattern – Behavioural!

•  Intent"
»  Given a language, define a representation for tis
grammar along with an interpreter that uses the
representation to interpret sentences in the
language!

Interpreter-1

Motivation!

•  If problem type occurs often enough, then it is


worthwhile to express instances of the problem as
sentences in a language

"
•  You build an interpreter that solves the problem by
interpreting those sentences"

Interpreter-2

Example Searching for Substrings!

•  Searching for patterns within a string is a common


problem"
•  Rather than construct a custom algorithm for each
pattern"
»  Express a pattern as a regular expression!
»  Interpret the regular expression against the string
to search!

•  The Interpreter pattern describes"


»  How to define a grammar for languages!
»  Represent sentences in the language!
»  Interpret those sentences!
Interpreter-3

Example Grammar!

Expression ::= ( Literal , Alternation , Sequence!


, Repetition , ʻ(ʼ Expression ʻ)ʼ ) ;!
!

Alternation ::= Expression ʻ|ʼ Expression ;!


!

Sequence ::= Expression ʻ&ʼ Expression ;!


!

Repetition ::= Expression ʻ*ʼ ;!


!

Literal ::= +( ʻaʼ , ʻbʼ , ʻcʼ , … , ʻzʼ ) ;!

Legend!
Space !means ʻandʼ!
, !(comma) means ʻorʼ!
(…) !(must choose) means choose one ! +(…) means choose one or more !
[…] !(optional) means choose one or none! +[…] means choose zero or more!
!

Interpreter-4

Example Architecture!

ALTERNATION_EXPRESSION SEQUENCE_EXPRESSION
interpret interpret

alternative_2 expression_2
REGULAR_EXPRESSION
alternative_1 interpret expression_1

repetiton

REPETITION_EXPRESSION LITERAL_EXPRESSION
interpret interpret
NONE
literal

Interpreter-5

Example Abstract Syntax Tree!
aSequenceExpression
expression_1
expression_2

aLiteralExpression aSequenceExpression
"raining" repetition

anAlternativeExpression
alternative_1
altlernative_2

aLiteralExpression aLiteralExpression
"cats" "dogs"

Interpreter-6

Abstract Architecture!

CONTEXT
context ABSTRACT_EXPRESSION
expression interpret(context)

CLIENT

TERMINAL_EXPRESSION NONTERMINAL_EXPRESSION
interpret(context) interpret(context)

Interpreter-7

Scenario!

Scenario: Interpret!

1 Build / get abstract syntax tree! CLIENT!


2 Interpret the tree!

1! 2!

ABSTRACT_SYNTAX_TREE! INTERPRET!

Interpreter-8

Participants!

•  AbstractExpression"
»  Declares an abstract Intepret operation that is
common to all nodes in the abstract syntax tree!

"
•  TerminalExpression"
»  Implements an Interpret operation associated with
terminal symbols in the grammar!

•  Context"
»  Contains information thatʼs global to the interpreter!

Interpreter-9

Participants – 2!

•  NonTerminalExpression"
»  One class required for each non-terminal in the
grammar!
»  Maintains instance variables for sub-expressions!
»  Implements an Interpret operation for nonterminal
symbols in the grammar!

•  Client"
»  Builds / gets an abstract syntax tree for a sentence
in the language!
»  Invokes the interpret operation!

Interpreter-10

Applicability!

•  Use when there is a language to interpret and you


can build an abstract syntax tree to represent a
sentence in the language"
•  Works best when"
»  Grammar is simple!
>  For complex grammars use parser generators!
»  Efficiency is not a critical concern!
>  Efficient interpretation is best done by
translating AST to another form!
–  E.g. a state machine

>  Can use the pattern to do the translation!

Interpreter-11

Consequences!

•  It is easy to change and extend the grammar"


»  Use inheritance!
»  Can build incrementally

!

•  Implementing the grammar is easy"


»  Classes representing nodes in ASTs have similar
implementations, making them easy to define!

Interpreter-12

Consequences – 2!

•  Complex grammars are hard to maintain"


»  Interpret needs at least one class for every rule in
the grammar!
>  Rules in BNF may require more than one class
per rule!
»  The number of classes and variation make it
difficult to use the pattern!

•  Easy to add new way to interpret expressions"


»  Support pretty printing and type checking by
adding new operations to the classes!
>  If the number of new operations is likely to keep
growing, then consider use of the Visitor pattern!

Interpreter-13

Related Patterns!

•  Composite is used to represent an abstract syntax


tree

"
•  Flyweight shows how to share terminal symbols
within the abstract syntax tree

"
•  The interpreter can use an Iterator to traverse the
structure

"
•  Visitor can be used to maintain the behaviour in each
node in the abstract syntax tree in one class"

Interpreter-14

Interpreter in Java API!

•  The Container class in java.awt aggregates


Component objects.

"
•  Interpretation is applied in Container by its
validateTree method, which recursively descends the
container tree and recomputes the layout for any sub-
trees"

Interpreter-15

You might also like