Interpreter CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns Some material taken from:

Slides:



Advertisements
Similar presentations
Natural Language Understanding Difficulties: Large amount of human knowledge assumed – Context is key. Language is pattern-based. Patterns can restrict.
Advertisements

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.
Lecture # 7 Chapter 4: Syntax Analysis. What is the job of Syntax Analysis? Syntax Analysis is also called Parsing or Hierarchical Analysis. A Parser.
GRAMMAR & PARSING (Syntactic Analysis) NLP- WEEK 4.
(A Completely Colorless Powerpoint Presentation of) The Interpreter Pattern David Witonsky.
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
Fall 2007CS 2251 Miscellaneous Topics Deque Recursion and Grammars.
Grammars Examples and Issues. Examples from Last Lecture a + b a b + a*bc* First draw a state diagram Then create a rule for each transition.
Let remember from the previous lesson what is Knowledge representation
CS 330 Programming Languages 09 / 23 / 2008 Instructor: Michael Eckmann.
Lecture 9UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.
Interpreter By: Mahmoodreza Jahanseir Amirkabir University of Technology Computer Engineering Department Fall 2010.
Class 4: Stacks. cis 335 Fall 2001 Barry Cohen What is a stack? n A stack is an ordered sequence of items, of which only the last (‘top’) item can be.
ANTLR with ASTs. Abstract Syntax Trees ANTLR can be instructed to produce ASTs for the output of the parser ANTLR uses a prefix notation for representing.
EECS 6083 Intro to Parsing Context Free Grammars
Compiler Principles Winter Compiler Principles Exercises on scanning & top-down parsing Roman Manevich Ben-Gurion University.
March 2006 CLINT-CS 1 Introduction to Computational Linguistics Chunk Parsing.
The Interpreter Pattern. Defining the Interpreter Intent Given a language, define a representation for its grammar along with an interpreter that uses.
Syntax Directed Translation. Syntax directed translation Yacc can do a simple kind of syntax directed translation from an input sentence to C code We.
ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
Lecture 6 Data Flow Modeling
Design Pattern Interpreter By Swathi Polusani. What is an Interpreter? The Interpreter pattern describes how to define a grammar for simple languages,
AN IMPLEMENTATION OF A REGULAR EXPRESSION PARSER
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Chapter 6 Programming Languages (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Ambiguity in Grammar By Dipendra Pratap Singh 04CS1032.
Towards the better software metrics tool motivation and the first experiences Gordana Rakić Zoran Budimac.
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
Abstract Factory and Factory Method CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Lex & Yacc By Hathal Alwageed & Ahmad Almadhor. References *Tom Niemann. “A Compact Guide to Lex & Yacc ”. Portland, Oregon. 18 April 2010 *Levine, John.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
The Interpreter Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Parsing and Code Generation Set 24. Parser Construction Most of the work involved in constructing a parser is carried out automatically by a program,
GRAMMARS & PARSING. Parser Construction Most of the work involved in constructing a parser is carried out automatically by a program, referred to as a.
How YACC is constructed. How Yacc works To construct a parsing machine for arithmetic expressions, a special case considered to simplify the account of.
Interpreter By: Mahmoodreza Jahanseir Amirkabir University of Technology Computer Engineering Department Fall 2010.
Formal grammars A formal grammar is a system for defining the syntax of a language by specifying sequences of symbols or sentences that are considered.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
Math Expression Evaluation With RegEx and Finite State Machines.
5. Context-Free Grammars and Languages
Regular Expressions, Backus-Naur Form and Reverse Polish Notation
CS 3304 Comparative Languages
CS510 Compiler Lecture 4.
Function Rules EQ: How do you write algebraic expressions? I will write algebraic expressions.
Stack as an ADT.
Syntax Analysis Chapter 4.
CO4301 – Advanced Games Development Week 2 Introduction to Parsing
PROGRAMMING LANGUAGES
Presentation by Julie Betlach 7/02/2009
Compiler Design 4. Language Grammars
Regular Grammar - Finite Automaton
Lexical and Syntax Analysis
(Slides copied liberally from Ruth Anderson, Hal Perkins and others)
5.1 Combining Functions Perform arithmetic operations on functions
Programming Language Syntax 2
ENERGY 211 / CME 211 Lecture 15 October 22, 2008.
إستراتيجيات ونماذج التقويم
ADTs, Grammars, Parsing, Tree traversals
Finite Automata and Formal Languages
Programming Language Syntax 5
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Interpreter Pattern.
Intermediate Representations Hal Perkins Autumn 2005
CSCE 314: Programming Languages Dr. Dylan Shell
Ben-Gurion University
Context Free Grammars-II
Review for the Midterm. Overview (Chapter 1):
COMPILER CONSTRUCTION
Stacks A stack is an ordered set of elements, for which only the last element placed into the stack is accessible. The stack data type is also known as.
Presentation transcript:

Interpreter CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns Some material taken from:

Interpreter Intent Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language Motivation Interpreting and evaluating expressions governed by a rules of some language is a common programming problem e.g., arithmetic expressions, regular expressions This pattern provides a way to define the grammar for the language, represent sentences, and then interpret those sentences

Design Solution

Participants Client, context, expression Client typically calls an evaluate method on the context object The call, in turn, calls interpret on several expression objects, and culminates in a returned result The expression objects together represent the entire sentence, and hence are usually contained in another object

Examples Roman numeral parsing: From Simple arithmetic expression parsing: Reverse polish notation From

Example: parsing arithmetic expressions Client (InterpreterDemo) creates Parser object using input sentence Client calls evaluate() on the Parser object Parser contains a context object (a Stack) and a list of Expression objects evaluate() calls interpret(context) on each Expression object Desired result is returned via the context object

Related Patterns The expression structure follows the Composite pattern