SlideShare une entreprise Scribd logo
1  sur  14
Interpreter Design Pattern
1- Review
2- Intent
3- Implements
4- Sample Code
5- Applicability
6- Consequences
7- Know Uses
Overview
1- Review
- What is Behavioral Pattern ?
-> Design patterns that identify common communication patterns between
object.
- What is a common pattern?
2- Intent
Given a language, define a representation for its grammar along with an interpreter
that uses the representation to interpret sentences in the language.
- The Implementation of the Interpreter pattern is just the use of the composite
pattern applied to represent a grammar.
- The Interpreter defines the behaviour while the composite defines only the
structure.
3- Implements
3- Implements (Cont.)
Abstract Expression
- Declares an interface for executing an operator
Terminal Expression
- Implements an Interpret operation associated with terminal symbols in the grammar.
- Interprets expressions containing any of the terminal tokens in the grammar.
Nonterminal Expression
- Interprets all of the nonterminal expressions in the grammar .
Context
- Contains the global information that is part of the parse.
Client
- Builds the syntax tree from the preceding expression types and invokes the interpret
operation.
3- Implements (Cont.)
public class Context {
private string input;
private int output;
public Context (String input) {
this.input = input;
}
public String getInput () {
return input;
}
public void setInput(String input) {
this.input = input;
}
public int getOutput () {
return output;
}
public void setOutput (int output) {
this.output = output;
}
}
4- Sample Code
public abstract class Expression {
public void interpret (Context context) {
if (context.getInput().length() == 0) {
return;
}
if (context.getInput().startsWith(nine)) {
context.setOutput(context.getOutput() + (9 * multiplier() ));
context.setInput(context.getInput().subString(2));
}
else if (context.getInput().startsWith(four)) {
context.setOutput(context.getOutput() + (4 * multiplier() ));
context.setInput(context.getInput().subString(2));
}
while (context.getInput().startsWith(one)) {
context.setOutput(context.getOutput() + (4 * multiplier() ));
context.setInput(context.getInput().subString(1));
}
}
4- Sample Code (Cont.)
public abstract String one();
public abstract String fout();
public abstract String nine();
public abstract int multiplier();
}
4- Sample Code (Cont.)
The template method pattern should be used :
- The interpreter pattern is used exhaustively in defining grammar, tokenize
input and store it.
- A specific area where Interpreter can be used are the rule engines.
- The Interpreter pattern can be used to add functionality to the composite
pattern.
5- Applicability
- Easier to change and extend the grammar
- Implementing the grammar is easily
- Adding new ways to interpret expressions
- Lets you embed a language into program
- Complex grammars are hard to maintain
6- Consequences
- Python uses the Interpreter pattern to generate byte code for a parse tree.
- A scheme interpreter pattern, directly executes the parse tree, making small
optimizations as it goes along.
- Text editors and Web browsers use the interpreter pattern to lay out document
and check spelling.
7- Know Uses
Thanks !

Contenu connexe

Tendances

OOAD unit1 introduction to object orientation
 OOAD unit1 introduction to object orientation OOAD unit1 introduction to object orientation
OOAD unit1 introduction to object orientation
Dr Chetan Shelke
 
Distributed Database System
Distributed Database SystemDistributed Database System
Distributed Database System
Sulemang
 
Thrashing allocation frames.43
Thrashing allocation frames.43Thrashing allocation frames.43
Thrashing allocation frames.43
myrajendra
 
Applet life cycle
Applet life cycleApplet life cycle
Applet life cycle
myrajendra
 

Tendances (20)

Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
 
Query processing
Query processingQuery processing
Query processing
 
I/O Streams
I/O StreamsI/O Streams
I/O Streams
 
jstl ( jsp standard tag library )
jstl ( jsp standard tag library )jstl ( jsp standard tag library )
jstl ( jsp standard tag library )
 
Distribution transparency and Distributed transaction
Distribution transparency and Distributed transactionDistribution transparency and Distributed transaction
Distribution transparency and Distributed transaction
 
Distributed database
Distributed databaseDistributed database
Distributed database
 
Java swing
Java swingJava swing
Java swing
 
Lec 7 query processing
Lec 7 query processingLec 7 query processing
Lec 7 query processing
 
Distributed DBMS - Unit 6 - Query Processing
Distributed DBMS - Unit 6 - Query ProcessingDistributed DBMS - Unit 6 - Query Processing
Distributed DBMS - Unit 6 - Query Processing
 
Backtracking
Backtracking  Backtracking
Backtracking
 
Java 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesJava 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional Interfaces
 
OOAD unit1 introduction to object orientation
 OOAD unit1 introduction to object orientation OOAD unit1 introduction to object orientation
OOAD unit1 introduction to object orientation
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Distributed Database System
Distributed Database SystemDistributed Database System
Distributed Database System
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
 
Daa notes 1
Daa notes 1Daa notes 1
Daa notes 1
 
Shared memory
Shared memoryShared memory
Shared memory
 
Thrashing allocation frames.43
Thrashing allocation frames.43Thrashing allocation frames.43
Thrashing allocation frames.43
 
Applet life cycle
Applet life cycleApplet life cycle
Applet life cycle
 
Use case diagram
Use case diagramUse case diagram
Use case diagram
 

En vedette

2010 sector study_report_english
2010 sector study_report_english2010 sector study_report_english
2010 sector study_report_english
Saravuth kong
 
Get started with dropbox
Get started with dropboxGet started with dropbox
Get started with dropbox
Arnold Laconsay
 
IndagineConoscitivaEbook2005
IndagineConoscitivaEbook2005IndagineConoscitivaEbook2005
IndagineConoscitivaEbook2005
Chiara Pacquola
 

En vedette (14)

2010 sector study_report_english
2010 sector study_report_english2010 sector study_report_english
2010 sector study_report_english
 
Management of diabetes
Management of diabetesManagement of diabetes
Management of diabetes
 
Anemia
AnemiaAnemia
Anemia
 
Get started with dropbox
Get started with dropboxGet started with dropbox
Get started with dropbox
 
The most outstanding Leader i have worked with.
The most outstanding Leader i have worked with.The most outstanding Leader i have worked with.
The most outstanding Leader i have worked with.
 
Promoting Clinical Research and Application to Practice - Counties Manukau He...
Promoting Clinical Research and Application to Practice - Counties Manukau He...Promoting Clinical Research and Application to Practice - Counties Manukau He...
Promoting Clinical Research and Application to Practice - Counties Manukau He...
 
Working Seamlessly Across Organisations - Kidney Society, Auckland and Count...
 Working Seamlessly Across Organisations - Kidney Society, Auckland and Count... Working Seamlessly Across Organisations - Kidney Society, Auckland and Count...
Working Seamlessly Across Organisations - Kidney Society, Auckland and Count...
 
IndagineConoscitivaEbook2005
IndagineConoscitivaEbook2005IndagineConoscitivaEbook2005
IndagineConoscitivaEbook2005
 
Deepak & Celia Mahtani - Freedom talk, Oasis Church, Collierswood, London
Deepak & Celia Mahtani - Freedom talk, Oasis Church, Collierswood, LondonDeepak & Celia Mahtani - Freedom talk, Oasis Church, Collierswood, London
Deepak & Celia Mahtani - Freedom talk, Oasis Church, Collierswood, London
 
Assuring quality improvement - Jerome Ng - Waitemata DHB
Assuring quality improvement - Jerome Ng - Waitemata DHBAssuring quality improvement - Jerome Ng - Waitemata DHB
Assuring quality improvement - Jerome Ng - Waitemata DHB
 
Freedom from depression
Freedom from depressionFreedom from depression
Freedom from depression
 
Statistika
StatistikaStatistika
Statistika
 
Hepatitis and its complications
Hepatitis and its complicationsHepatitis and its complications
Hepatitis and its complications
 
Working Seamlessly Across Organisations - National Hauora Coalition, Tamariki...
Working Seamlessly Across Organisations - National Hauora Coalition, Tamariki...Working Seamlessly Across Organisations - National Hauora Coalition, Tamariki...
Working Seamlessly Across Organisations - National Hauora Coalition, Tamariki...
 

Similaire à Interpreter Design Pattern

COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxCOMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
monicafrancis71118
 
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxCOMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
cargillfilberto
 
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxCOMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
drandy1
 
Faculty of ScienceDepartment of ComputingFinal Examinati.docx
Faculty of ScienceDepartment of ComputingFinal Examinati.docxFaculty of ScienceDepartment of ComputingFinal Examinati.docx
Faculty of ScienceDepartment of ComputingFinal Examinati.docx
mydrynan
 

Similaire à Interpreter Design Pattern (20)

Core java
Core javaCore java
Core java
 
Golang basics for Java developers - Part 1
Golang basics for Java developers - Part 1Golang basics for Java developers - Part 1
Golang basics for Java developers - Part 1
 
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxCOMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
 
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxCOMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
 
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxCOMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
 
What's new in c# 8.0
What's new in c# 8.0What's new in c# 8.0
What's new in c# 8.0
 
Declare Your Language: Syntax Definition
Declare Your Language: Syntax DefinitionDeclare Your Language: Syntax Definition
Declare Your Language: Syntax Definition
 
AFPS_2011
AFPS_2011AFPS_2011
AFPS_2011
 
Advance python
Advance pythonAdvance python
Advance python
 
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 3)
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 3)Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 3)
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 3)
 
srgoc
srgocsrgoc
srgoc
 
A Proposal to Orchestrate Test Cases
A Proposal to Orchestrate Test CasesA Proposal to Orchestrate Test Cases
A Proposal to Orchestrate Test Cases
 
(3) cpp procedural programming
(3) cpp procedural programming(3) cpp procedural programming
(3) cpp procedural programming
 
It’s sometimes useful to make a little language for a simple problem.pdf
It’s sometimes useful to make a little language for a simple problem.pdfIt’s sometimes useful to make a little language for a simple problem.pdf
It’s sometimes useful to make a little language for a simple problem.pdf
 
Python Modules, executing modules as script.pptx
Python Modules, executing modules as script.pptxPython Modules, executing modules as script.pptx
Python Modules, executing modules as script.pptx
 
Presentation slides for "A formal foundation for trace-based JIT compilation"
Presentation slides for "A formal foundation for trace-based JIT compilation"Presentation slides for "A formal foundation for trace-based JIT compilation"
Presentation slides for "A formal foundation for trace-based JIT compilation"
 
Faculty of ScienceDepartment of ComputingFinal Examinati.docx
Faculty of ScienceDepartment of ComputingFinal Examinati.docxFaculty of ScienceDepartment of ComputingFinal Examinati.docx
Faculty of ScienceDepartment of ComputingFinal Examinati.docx
 
Functional Programming You Already Know
Functional Programming You Already KnowFunctional Programming You Already Know
Functional Programming You Already Know
 
Interpreter
InterpreterInterpreter
Interpreter
 

Dernier

DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
MayuraD1
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 

Dernier (20)

Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLEGEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
Wadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptxWadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptx
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and properties
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 

Interpreter Design Pattern

  • 2. 1- Review 2- Intent 3- Implements 4- Sample Code 5- Applicability 6- Consequences 7- Know Uses Overview
  • 3. 1- Review - What is Behavioral Pattern ? -> Design patterns that identify common communication patterns between object. - What is a common pattern?
  • 4. 2- Intent Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.
  • 5. - The Implementation of the Interpreter pattern is just the use of the composite pattern applied to represent a grammar. - The Interpreter defines the behaviour while the composite defines only the structure. 3- Implements
  • 7. Abstract Expression - Declares an interface for executing an operator Terminal Expression - Implements an Interpret operation associated with terminal symbols in the grammar. - Interprets expressions containing any of the terminal tokens in the grammar. Nonterminal Expression - Interprets all of the nonterminal expressions in the grammar . Context - Contains the global information that is part of the parse. Client - Builds the syntax tree from the preceding expression types and invokes the interpret operation. 3- Implements (Cont.)
  • 8. public class Context { private string input; private int output; public Context (String input) { this.input = input; } public String getInput () { return input; } public void setInput(String input) { this.input = input; } public int getOutput () { return output; } public void setOutput (int output) { this.output = output; } } 4- Sample Code
  • 9. public abstract class Expression { public void interpret (Context context) { if (context.getInput().length() == 0) { return; } if (context.getInput().startsWith(nine)) { context.setOutput(context.getOutput() + (9 * multiplier() )); context.setInput(context.getInput().subString(2)); } else if (context.getInput().startsWith(four)) { context.setOutput(context.getOutput() + (4 * multiplier() )); context.setInput(context.getInput().subString(2)); } while (context.getInput().startsWith(one)) { context.setOutput(context.getOutput() + (4 * multiplier() )); context.setInput(context.getInput().subString(1)); } } 4- Sample Code (Cont.)
  • 10. public abstract String one(); public abstract String fout(); public abstract String nine(); public abstract int multiplier(); } 4- Sample Code (Cont.)
  • 11. The template method pattern should be used : - The interpreter pattern is used exhaustively in defining grammar, tokenize input and store it. - A specific area where Interpreter can be used are the rule engines. - The Interpreter pattern can be used to add functionality to the composite pattern. 5- Applicability
  • 12. - Easier to change and extend the grammar - Implementing the grammar is easily - Adding new ways to interpret expressions - Lets you embed a language into program - Complex grammars are hard to maintain 6- Consequences
  • 13. - Python uses the Interpreter pattern to generate byte code for a parse tree. - A scheme interpreter pattern, directly executes the parse tree, making small optimizations as it goes along. - Text editors and Web browsers use the interpreter pattern to lay out document and check spelling. 7- Know Uses