The Interpreter Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.

Slides:



Advertisements
Similar presentations
15-Dec-14 BNF. Metalanguages A metalanguage is a language used to talk about a language (usually a different one) We can use English as its own metalanguage.
Advertisements

ICE1341 Programming Languages Spring 2005 Lecture #5 Lecture #5 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics Fall 2005.
ISBN Chapter 3 Describing Syntax and Semantics.
(A Completely Colorless Powerpoint Presentation of) The Interpreter Pattern David Witonsky.
INTERPRETER Main Topics What is an Interpreter. Why should we learn about them.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Defining practical programming languages Carlos Varela RPI.
Honors Compilers An Introduction to Grammars Feb 12th 2002.
ISBN Chapter 3 More Syntax –BNF –Derivations –Practice.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Fall 2007CS 2251 Miscellaneous Topics Deque Recursion and Grammars.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Slide 1 Chapter 3 Attribute Grammars. Slide 2 Attribute Grammars Certain language structures cannot be described using EBNF. Attribute grammars are extensions.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Algorithm Programming Behavioral Design Patterns Bar-Ilan University תשס " ו by Moshe Fresko.
1 Introduction: syntax and semantics Syntax: a formal description of the structure of programs in a given language. Semantics: a formal description of.
30-Jun-15 BNF. Metalanguages A metalanguage is a language used to talk about a language (usually a different one) We can use English as its own metalanguage.
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Compilers and Syntax.
Chapter 3: Formal Translation Models
COP4020 Programming Languages
Interpreter By: Mahmoodreza Jahanseir Amirkabir University of Technology Computer Engineering Department Fall 2010.
UMBC Introduction to Compilers CMSC 431 Shon Vick 01/28/02.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
The Interpreter Pattern. Defining the Interpreter Intent Given a language, define a representation for its grammar along with an interpreter that uses.
Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
Chpater 3. Outline The definition of Syntax The Definition of Semantic Most Common Methods of Describing Syntax.
Compiler1 Chapter V: Compiler Overview: r To study the design and operation of compiler for high-level programming languages. r Contents m Basic compiler.
Design Pattern Interpreter By Swathi Polusani. What is an Interpreter? The Interpreter pattern describes how to define a grammar for simple languages,
CS 355 – PROGRAMMING LANGUAGES Dr. X. Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax.
Winter 2007SEG2101 Chapter 71 Chapter 7 Introduction to Languages and Compiler.
CS 331, Principles of Programming Languages Chapter 2.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Grammars CPSC 5135.
3-1 Chapter 3: Describing Syntax and Semantics Introduction Terminology Formal Methods of Describing Syntax Attribute Grammars – Static Semantics Describing.
ISBN Chapter 3 Describing Syntax and Semantics.
TextBook Concepts of Programming Languages, Robert W. Sebesta, (10th edition), Addison-Wesley Publishing Company CSCI18 - Concepts of Programming languages.
Copyright © by Curt Hill Grammar Types The Chomsky Hierarchy BNF and Derivation Trees.
Interpreter CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns Some material taken from:
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IX Interpreter, Mediator, Template Method recap.
Bernd Fischer RW713: Compiler and Software Language Engineering.
CPS 506 Comparative Programming Languages Syntax Specification.
Chapter 3 Describing Syntax and Semantics
ISBN Chapter 3 Describing Syntax and Semantics.
CS 331, Principles of Programming Languages Chapter 2.
The Visitor Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Programming Languages and Design Lecture 2 Syntax Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
LECTURE 4 Syntax. SPECIFYING SYNTAX Programming languages must be very well defined – there’s no room for ambiguity. Language designers must use formal.
Interpreter By: Mahmoodreza Jahanseir Amirkabir University of Technology Computer Engineering Department Fall 2010.
1 CS Programming Languages Class 04 September 5, 2000.
©SoftMoore ConsultingSlide 1 Context-Free Grammars.
Comp 311 Principles of Programming Languages Lecture 2 Syntax Corky Cartwright August 26, 2009.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Syntax.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
Describing Syntax and Semantics Chapter 3: Describing Syntax and Semantics Lectures # 6.
Chapter 3 – Describing Syntax
Chapter 3 – Describing Syntax
Presentation by Julie Betlach 7/02/2009
Jim Fawcett CSE776 – Design Patterns Summer 2003
R.Rajkumar Asst.Professor CSE
Interpreter Pattern.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
BNF 23-Feb-19.
Presentation transcript:

The Interpreter Pattern (Behavioral) ©SoftMoore ConsultingSlide 1

Motivation If a particular problem occurs often enough, then it might be worthwhile to express instances of that problem as “sentences” (programs) in a simple language. Then you can build an interpreter that solves the problem by interpreting these sentences. The Interpreter pattern describes how to –define a grammar for simple languages. –represent sentences in the language using abstract syntax trees. –interpret these sentences. ©SoftMoore ConsultingSlide 2

Domain-Specific Languages A domain-specific language (DSL) is a special-purpose programming language dedicated to a particular problem domain or a particular problem representation technique. Examples –domain-specific language for life insurance policies –domain-specific language for combat simulation –domain-specific language for salary calculation –domain-specific language for billing Contrast with a general-purpose programming language such as C++, Java, or Python, which is created to solve problems in many domains. ©SoftMoore ConsultingSlide 3

©SoftMoore ConsultingSlide 4 Specification of a Programming Language Syntax (form) –basic language symbols (or tokens) –specified formally by a context-free grammar Contextual Constraints –program rules and restrictions that can not be specified in a context-free grammar –consist primarily of type and scope rules –usually specified informally Semantics (meaning) –behavior of program when it is run on a machine –usually specified informally

©SoftMoore ConsultingSlide 5 Overview of Context-Free Grammars Provide a formal notation for specifying the syntax of a programming language (metalanguage) Finite notation that describes all “sentences” (programs) of the language Powerful enough to handle infinite languages Show the structure of the sentences in the language Used extensively for almost every programming language since the early days of FORTRAN Drive parser development

©SoftMoore ConsultingSlide 6 Overview of Context-Free Grammars (continued) Many different but similar notations Also known as Backus-Naur Form (BNF) grammars Often processed by compiler tools (“compiler compilers”) Expressed as a finite set of rules –often called “production rules” or simply “productions” –define how phrases are composed from terminal and nonterminal symbols –have the form “N = s1 s2 … sk.” where left-hand side of the rule, is a single nonterminal symbol. right-hand side of the rule is a sequence of terminal and nonterminal symbols.

©SoftMoore ConsultingSlide 7 Extended Grammars Allow special symbols on right-hand side of a rule: “|” for alternation (read “or” or “or alternatively”) “(“ and “)” for grouping “*” as a postfix operator to indicate that a syntax expression may be repeated zero or more times “+” as a postfix operator to indicate that a syntax expression may be repeated one or more times “?” as a postfix operator to indicate that a syntax expression is optional (i.e., it may be repeated zero or one times)

©SoftMoore ConsultingSlide 8 Using Extended Grammars Extended grammars allow rules to be expressed in a simple, easily understood form Example: identifiers = identifier ( "," identifier )*.

©SoftMoore ConsultingSlide 9 Alternate Rule Notations Use “  ”, “::=”, or simply “:” instead of “=” to separate left and right sides of rules. Use end of line (instead of period) to terminate rules. Use “{“ and “}” to enclose syntax expressions that can be repeated 0 or more times. Similarly, use “[“ and “]” to enclose optional syntax expressions. Enclose nonterminal symbols in “ ” and omit quotes around terminal symbols. Use font changes such as bold to distinguish between terminal and nonterminal symbols.

©SoftMoore ConsultingSlide 10 Example: Alternate Rule Notations ::= [ ]. period is a terminal symbol, not a rule terminator

Interpreter Pattern Intent: Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret “sentences” in the language. Applicability: The Interpreter pattern works best when –the grammar for the language being interpreted is simple. –efficiency of the interpreter is not a critical concern. If efficiency is a concern, consider implementing a compiler, which uses similar techniques. Slide 11©SoftMoore Consulting

Interpreter Pattern (continued) ©SoftMoore ConsultingSlide 12 AbstractExpression interpret(Context) Structure TerminalExpression interpret(Context) Context Client NonterminalExpression interpret(Context) * 1 Note: The structure of the …Expression class is a Composite.

Interpreter Pattern (continued) Participants AbstractExpression –declares an abstract interpret() operation that is common to all nodes in the abstract syntax tree. NonterminalExpression –one such class is required for every rule in the grammar. –maintains instance variables corresponding to the symbols on the right-hand side of the rule. –implements an interpret operation for the nonterminal symbol on the left-hand side of the rule (typically by using recursion). ©SoftMoore ConsultingSlide 13

Interpreter Pattern (continued) Participants (continued) TerminalExpression –implements an interpret() operation associated with the terminal symbols in the grammar. –an instance is required for every terminal symbol in a program. Context –contains information that is global to the interpreter. Client –builds an abstract syntax tree for a program. –invokes the interpret operation. ©SoftMoore ConsultingSlide 14

Interpreter Pattern (continued) Collaborations The Client –builds an abstract syntax tree of NonterminalExpression and TerminalExpression instances. –initializes the context. –invokes the interpret() operation. Each NonterminalExpression node defines interpret() in terms of interpret() on each subexpression. The interpret() operation of each TerminalExpression defines the base case in the recursion. The interpret() operations at each node use the context to store and access the state of the interpreter. ©SoftMoore ConsultingSlide 15

Interpreter Pattern (continued) Consequences: The Interpreter pattern has the following benefits and liabilities: It’s easy to change and extend the grammar. Since the pattern uses classes to represent grammar rules, you can use inheritance to change or extend the grammar. Implementing the grammar is straightforward. Classes defining nodes in the abstract syntax tree are relatively straightforward to implement. Automated tools (parser generators) can often be used to generate the classes automatically. ©SoftMoore ConsultingSlide 16

Interpreter Pattern (continued) Consequences (continued) Complex grammars are hard to maintain. The Interpreter pattern defines roughly one class for every rule in the grammar. Grammars containing many rules can make the interpreter hard to maintain. Parser generators can help in this case. The Interpreter pattern makes it easy to evaluate an expression in a new way. For example, it is easy to add type-checking or pretty printing to a language by defining a new operation on the expression classes. (See, also, the Visitor pattern.) ©SoftMoore ConsultingSlide 17

Interpreter Pattern (continued) Implementation Creating the abstract syntax tree. The abstract syntax tree can be created by any one of several parsing techniques (table-driven, recursive descent, etc.) Defining the interpret() operation. If it is common to create a new interpreter, then it might be better to use the Visitor pattern than to define an interpret() operation in the expression classes. Sharing terminal symbols with the Flyweight pattern. Grammars whose sentences contain many occurrences of a terminal symbol might benefit from sharing a single copy of that symbol (e.g., using Java enums). ©SoftMoore ConsultingSlide 18

Related Patterns The abstract syntax tree is an instance of the Composite pattern. The interpreter can use an Iterator to traverse the structure. Visitor can be used to maintain the behavior in each node of the abstract syntax tree in one class. ©SoftMoore ConsultingSlide 19

References Interpreter pattern (Wikipedia) Interpreter Design Pattern (SourceMaking) Interpreter Design Pattern (BlackWasp) ©SoftMoore ConsultingSlide 20