Interpreter By: Mahmoodreza Jahanseir Amirkabir University of Technology Computer Engineering Department Fall 2010.

Slides:



Advertisements
Similar presentations
More on Classes Inheritance and Polymorphism
Advertisements

Semantics Static semantics Dynamic semantics attribute grammars
Intermediate Code Generation
ICE1341 Programming Languages Spring 2005 Lecture #6 Lecture #6 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Compilation 2011 Static Analysis Johnni Winther Michael I. Schwartzbach Aarhus University.
Chapter 5 Syntax Directed Translation. Outline Syntax Directed Definitions Evaluation Orders of SDD’s Applications of Syntax Directed Translation Syntax.
Winter Compiler Construction T7 – semantic analysis part II type-checking Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv.
Matt Klein. Decorator Pattern  Intent  Attach Additional responsibilities to an object by dynamically. Decorators provide a flexible alternative to.
Behavioral Pattern: Interpreter C h a p t e r 5 – P a g e 149 Some programs benefit from having a language to describe operations that they can perform.
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.
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
(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.
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Design Pattern Course Builder Pattern 1 Mahdieh Monzavi AmirKabir University of Technology, Department of Computer Engineering & Information Technology.
Fall 2007CS 2251 Miscellaneous Topics Deque Recursion and Grammars.
Visitor Pattern Jeff Schott CS590L Spring What is the Purpose of the Visitor Pattern ? n Represent an operation to be performed on the elements.
Visitor Matt G. Ellis. Intent Metsker: Let developers define a new operation for a hierarchy without changing the hierarchy classes. GoF: Represent an.
Template Method By: Mahmoodreza Jahanseir Amirkabir University of Technology Computer Engineering Department Fall 2010.
Algorithm Programming Behavioral Design Patterns Bar-Ilan University תשס " ו by Moshe Fresko.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
Chapter 2 A Simple Compiler
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VI Composite, Iterator, and Visitor Patterns.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
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.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Syntax Directed Translation. Syntax directed translation Yacc can do a simple kind of syntax directed translation from an input sentence to C code We.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
More Design Patterns In Delphi Jim Cooper Falafel Software Session Code: D3.03 Track: Delphi.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow.
Design Pattern Interpreter By Swathi Polusani. What is an Interpreter? The Interpreter pattern describes how to define a grammar for simple languages,
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Interpreter CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns Some material taken from:
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IX Interpreter, Mediator, Template Method recap.
Centralized vs. Decentralized Interpreter Pattern Visitor Pattern.
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
Prototype pattern Participants Prototype (Graphic) – declared an interface for cloning itself ConcretePrototype (EditBox, Slider) – implements an operation.
Chapter 3 Describing Syntax and Semantics
Introduction to Compiling
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
The Visitor Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Interpreter Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
CMSC 330: Organization of Programming Languages Operational Semantics.
Interpreter By: Mahmoodreza Jahanseir Amirkabir University of Technology Computer Engineering Department Fall 2010.
Chap. 7, Syntax-Directed Compilation J. H. Wang Nov. 24, 2015.
CSE 420 Lecture Program is lexically well-formed: ▫Identifiers have valid names. ▫Strings are properly terminated. ▫No stray characters. Program.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
A Simple Syntax-Directed Translator
Factory Patterns 1.
CSC 205 Java Programming II
Presentation by Julie Betlach 7/02/2009
Syntax-Directed Translation
Jim Fawcett CSE776 – Design Patterns Summer 2003
CMPE 152: Compiler Design September 13 Class Meeting
The Metacircular Evaluator
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Interpreter Pattern.
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 2/23/2019.
6.001 SICP Interpretation Parts of an interpreter
COMPILER CONSTRUCTION
Presentation transcript:

Interpreter By: Mahmoodreza Jahanseir Amirkabir University of Technology Computer Engineering Department Fall 2010

Intent Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language. 2

Amirkabir University of Technology Computer Engineering Department Fall 2010 Motivation If a particular kind of problem occurs often enough, then it might be worthwhile to express instances of the problem as sentences in a simple language. Then you can build an interpreter that solves the problem by interpreting these sentences. For example, searching for strings that match a pattern is a common problem. Regular expressions are a standard language for specifying patterns of strings. Rather than building custom algorithms to match each pattern against strings, search algorithms could interpret a regular expression that specifies a set of strings to match. 3

Amirkabir University of Technology Computer Engineering Department Fall 2010 Motivation (cont…) The Interpreter pattern describes:  how to define a grammar for simple languages,  represent sentences in the language,  and interpret these sentences. Suppose the following grammar defines the regular expressions: 4

Amirkabir University of Technology Computer Engineering Department Fall 2010 Motivation (cont…) The Interpreter pattern uses a class to represent each grammar rule. Symbols on the right-hand side of the rule are instance variables of these classes. The grammar above is represented by five classes:  An abstract class RegularExpression  Subclass LiteralExpression,  Subclass AlternationExpression,  Subclass SequenceExpression,  And subclass RepetitionExpression. The last three classes define variables that hold sub expressions. 5

Amirkabir University of Technology Computer Engineering Department Fall 2010 Motivation (cont…) 6

Amirkabir University of Technology Computer Engineering Department Fall 2010 Motivation (cont…) Every regular expression defined by this grammar is represented by an abstract syntax tree made up of instances of these classes. For example, the abstract syntax tree represents the regular Expression: raining & (dogs | cats) * 7

Amirkabir University of Technology Computer Engineering Department Fall 2010 Motivation (cont…) We can create an interpreter for these regular expressions by defining the Interpret operation on each subclass of RegularExpression. Interpret takes as an argument the context in which to interpret the expression. 8

Amirkabir University of Technology Computer Engineering Department Fall 2010 Applicability Use the Interpreter pattern when there is a language to interpret, and you can represent statements in the language as abstract syntax trees. The Interpreter pattern works best when 1) the grammar is simple.  For complex grammars, the class hierarchy for the grammar becomes large and unmanageable.  Tools such as parser generators are a better alternative in such cases.  They can interpret expressions without building abstract syntax trees, which can save space and possibly time. 9

Amirkabir University of Technology Computer Engineering Department Fall 2010 Applicability (cont…) 2) efficiency is not a critical concern.  The most efficient interpreters are usually not implemented by interpreting parse trees directly but by first translating them into another form.  For example, regular expressions are often transformed into state machines. But even then, the translator can be implemented by the Interpreter pattern, so the pattern is still applicable. 10

Amirkabir University of Technology Computer Engineering Department Fall 2010 Structure 11

Amirkabir University of Technology Computer Engineering Department Fall 2010 Participants AbstractExpression  declares an abstract Interpret operation that is common to all nodes in the abstract syntax tree. TerminalExpression  implements an Interpret operation associated with terminal symbols in the grammar.  an instance is required for every terminal symbol in a sentence. NonterminalExpression  one such class is required for every rule R ::= R 1 R 2... R n in the grammar.  maintains instance variables of type AbstractExpression for each of the symbols R 1 through R n. 12

Amirkabir University of Technology Computer Engineering Department Fall 2010 Participants (cont…)  implements an Interpret operation for nonterminal symbols in the grammar. Interpret typically calls itself recursively on the variables representing R 1 through R n. Context  contains information that's global to the interpreter. Client  builds (or is given) an abstract syntax tree representing a particular sentence in the language that the grammar defines.  invokes the Interpret operation. 13

Amirkabir University of Technology Computer Engineering Department Fall 2010 Collaborations The client builds (or is given) the sentence as an abstract syntax tree of NonterminalExpression and TerminalExpression instances. Then the client initializes the context and 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. 14

Amirkabir University of Technology Computer Engineering Department Fall 2010 Consequences Benefits:  It's easy to change and extend the grammar. Because the pattern uses classes to represent grammar rules, you can use inheritance to change or extend the grammar.  Implementing the grammar is easy, too. Classes defining nodes in the abstract syntax tree have similar implementations.  Adding new ways to interpret expressions. The Interpreter pattern makes it easier to evaluate an expression in a new way.  For example, you can support pretty printing or type-checking an expression by defining a new operation on the expression classes.  If you keep creating new ways of interpreting an expression, then consider using the Visitor pattern to avoid changing the grammar classes. 15

Amirkabir University of Technology Computer Engineering Department Fall 2010 Consequences (cont…) Liability:  Complex grammars are hard to maintain. The Interpreter pattern defines at least one class for every rule in the grammar. Hence grammars containing many rules can be hard to manage and maintain. when the grammar is very complex, other techniques such as parser or compiler generators are more appropriate. 16

Amirkabir University of Technology Computer Engineering Department Fall 2010 Implementation 1) Creating the abstract syntax tree.  The Interpreter pattern doesn't explain how to create an abstract syntax tree.  The abstract syntax tree can be created by a table-driven parser, by a hand-crafted parser, or directly by the client. 2) Defining the Interpret operation.  If it's common to create a new interpreter, then it's better to use the Visitor pattern to put Interpret in a separate "visitor" object. 3) 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. 17

Amirkabir University of Technology Computer Engineering Department Fall 2010 Example First example is a system for manipulating and evaluating Boolean expressions implemented in C++. The grammar is defined as follows: We define two operations on Boolean expressions: 1. Evaluate, evaluates a Boolean expression in a context that assigns a true or false value to each variable. 2. Replace, produces a new Boolean expression by replacing a variable with an expression. 18

Amirkabir University of Technology Computer Engineering Department Fall 2010 Example (cont…) Replace shows how the Interpreter pattern can be used for more than just evaluating expressions. In this case, it manipulates the expression itself. 19

Amirkabir University of Technology Computer Engineering Department Fall 2010 Example (cont…) BooleanExp defines the interface for all classes that define a Boolean expression: public abstract class BooleanExp { public BooleanExp(){} public abstract bool Evaluate(Context aContext); public abstract BooleanExp Replace(string name, BooleanExp bExp); public abstract BooleanExp Copy(); } 20

Amirkabir University of Technology Computer Engineering Department Fall 2010 Example (cont…) The class Context defines a mapping from variables to Boolean values. public class Context { Hashtable values; public Context() { values = new Hashtable(); } public bool Lookup(VariableExp varExp) { return(bool)values[varExp.name]; } public void Assign(VariableExp varExp, bool bval) { values.Add(varExp.name, bval); } 21

Amirkabir University of Technology Computer Engineering Department Fall 2010 Example (cont…) public class VariableExp : BooleanExp { public string name; public VariableExp(string _name){name = _name;} public override bool Evaluate(Context aContext) { return aContext.Lookup(this); } public override BooleanExp Replace(string _name, BooleanExp bExp) { if (_name.Equals(name)) return bExp.Copy(); else return new VariableExp(name); } public override BooleanExp Copy() { return new VariableExp(name); } 22

Amirkabir University of Technology Computer Engineering Department Fall 2010 Example (cont…) public class AndExp : BooleanExp { public BooleanExp operand1; public BooleanExp operand2; public VariableExp(BooleanExp op1, BooleanExp op2) {operand1 = op1; operand2 = op2;} public override bool Evaluate(Context aContext) { return operand1.Evaluate(aContext) && operand2.Evaluate(aContext); } public override BooleanExp Replace(string _name, BooleanExp bExp) { return new AndExp(operand1.Replace(_name, bExp), operand2.Replace(_name, bExp)); } public override BooleanExp Copy() { return new AndExp(operand1.Copy(),operand2.Copy()); } 23

Amirkabir University of Technology Computer Engineering Department Fall 2010 Example (cont…) (true and x) or (y and (not x)) BooleanExp expression; Context context; VariableExp x = new VariableExp("X"); VariableExp y = new VariableExp("Y"); expression = new OrExp( new AndExp(new Constant(true), x), new AndExp(y, new NotExp(x)) ); context.Assign(x, false); context.Assign(y, true); bool result = expression.Evaluate(context); we can replace the variable y with a new expression and then reevaluate it: VariableExp z = new VariableExp("Z"); NotExp not_z = new NotExp(z); BooleanExp replacement = expression.Replace("Y", not_z); context.Assign(z, true); result = replacement.Evaluate(context); 24

Amirkabir University of Technology Computer Engineering Department Fall 2010 Example (cont…) This example illustrates an important point about the Interpreter pattern: many kinds of operations can "interpret" a sentence. Evaluate fits our idea of what an interpreter should do most closely—that is, it interprets a program or expression and returns a simple result. However, Replace can be viewed as an interpreter as well. It's an interpreter whose context is the name of the variable being replaced along with the expression that replaces it, and whose result is a new expression. Even Copy can be thought of as an interpreter with an empty context. 25

Amirkabir University of Technology Computer Engineering Department Fall 2010 Related Patterns Composite : The abstract syntax tree is an instance of the Composite pattern. Flyweight shows how to share terminal symbols within the abstract syntax tree. Iterator : The interpreter can use an Iterator to traverse the structure. Visitor can be used to maintain the behavior in each node in the abstract syntax tree in one class. 26

Amirkabir University of Technology Computer Engineering Department Fall 2010 Q&A