Procedures. EOPL3: Section 3.3 PROC and App B: SLLGEN
|
|
- Leo Hodges
- 5 years ago
- Views:
Transcription
1 Procedures EOPL3: Section 3.3 PROC and App B: SLLGEN
2 The PROC language Expression ::= proc (Identifier) Expression AST: proc-exp (var body) Expression ::= (Expression Expression) AST: call-exp (rator rand) PROC includes all of LET language Anonymous procedure One parameter always CS7100(pm) 2
3 Semantics of Procedures (This slide is for procedures in general.) Procedure Definition Store formal parameters and body Procedure Invocation Evaluate body in an environment that binds formals to actual argument values Interpretation of free-variables: Two methods Use env at proc definition (lexical/static scoping) Use env at proc call (dynamic scoping) CS7100(pm) 3
4 Scoping and Binding references (f x y) f, x, and y declarations (lambda (x) (+ x 3)) (let ((x (+ y 7))) (+ x 3)) y, and second/right x are refs first/left x is a declaration lexical scoping rules CS7100(pm) 4
5 Kinds of Scope Static or Lexical scope determined by structure of program Scheme, C++, Java, and many compiled languages Dynamic scope determined by path of execution Lisp dialects, Perl, and many interpreted languages Global Scope File scope Local Scope Block Body of a procedure Body of a loop Scope alters the meaning CS7100(pm) 5
6 Example-1 of PROC let f = proc (x) --(x,11) in (f (f 77)) Defines an anonymous procedure with one formal parameter named x. Body of the procedure: --(x,11) Binds the name f to this procedure. Invokes f with actual argument 77. Invokes f again with the result of above. (will use two -- just for visibility) CS7100(pm) 6
7 Example-2 of PROC (proc (f) (f (f 77)) proc (x) --(x,11)) This example is derived from the production Expression ::= (Expression Expression) so is (f (f 77)) so is (f 77) proc (f) (f (f 77)) is the rator. It defines an anonymous procedure with one formal parameter named f. proc (x) --(x,11)) is the rand. It also defines an anonymous procedure with one formal parameter named x. CS7100(pm) 7
8 Example-3 of PROC let x = 200 in let f = proc (z) --(z, x) in let x = 100 in let g = proc (z) --(z, x) in --((f 1), (g 1)) Illustrates scope issues x and z appear four times each. Lexical scoping In --((f 1), (g 1)), the bodies of f and g must be evaluated in the env they were defined. In f, x is bound to 200 In g, x is bound to 100 CS7100(pm) 8
9 Example Programs of PROC Example-1 and -2 produce same result, but different mechanisms. Previous two slides gave semantics informally Watch out: a very seductive approach Next few slides: interpreter based CS7100(pm) 9
10 Example Calc w/ Spec 1 CS7100(pm) 10
11 Example Calc w/ Spec 2 CS7100(pm) 11
12 Example Calc w/ Spec 3 CS7100(pm) 12
13 Example Calc w/ Spec 4 CS7100(pm) 13
14 Recall value-of value-of is an operator with two operands an AST an environment (value-of ast env) PROC = LET + two more productions Bring in all value-of specs from LET Additions are shown on next few slides CS7100(pm) 14
15 additional value-of specs (value-of (proc-exp var body) ρ) = (proc-val (procedure var body ρ)) (value-of (call-exp rator rand) ρ) = (let ( (proc (expval->proc (value-of rator ρ))) (arg (value-of rand ρ))) (apply-procedure proc arg)) To be defined: proc-val, apply-procedure CS7100(pm) 15
16 Spec of apply-procedure (apply-procedure (procedure var body ρ) val) = (value-of body [var=val]ρ ) apply-procedure takes two arguments: an AST of a procedure definition an argument for the parameter of the procedure yields an expressed value CS7100(pm) 16
17 Impl of apply-procedure (define proc? (lambda (pc) (procedure? pc))) (define procedure (lambda (var body env) (lambda (val) (value-of body (extend-env var val (define apply-procedure (lambda (pc val) (pc val))) env))))) procedure? provided from r5rs Names being bound: proc? procedure apply-procedure env is an environment ASTs: body, pc, val, var Use of procedure? is too liberal. procedure is not self-contained; takes three arguments: param name var body AST environment CS7100(pm) 17
18 Alternate impl called Closures (define-datatype proc proc? (procedure (var identifier?) (body expression?) (saved-env environment?))) (define apply-procedure (lambda (pc val) (cases proc pc (procedure (var body saved-env) (value-of body (extend-env var val saved-env)))))) Defining a new data type called proc Has only one variant procedure That has three parts var which must be an id body an expression saved-env an environment apply-procedure takes pc and val. cases proc pc pc is expected to be of type proc code for each variant of proc only one variant procedure here CS7100(pm) 18
19 the data type expval is now (define-datatype expval expval? (num-val (num number?)) (bool-val (bool boolean?)) (proc-val (proc proc?))) CS7100(pm) 19
20 value-of: two new clauses (proc-exp (var body) (proc-val (procedure var body env))) (call-exp (rator rand) (let ( (proc (expval->proc (value-of rator env))) (arg (value-of rand env))) (apply-procedure proc arg))) CS7100(pm) 20
21 Curried procedures In PROC, procedures with multiple arguments can be had as in: let f = proc (x) proc (y)... in ((f 3) 4) proc (x) yields a procedure Named after Haskell Brooks Curry ( ), a combinatory logician. CS7100(pm) 21
22 chapter3/proc-lang/ Two subdirectories chapter3/proc-lang/proc-rep: procedural implementation chapter3/proc-lang/ds-rep: data type based (i.e., closure) Both directories have the following files data-structures.scm drscheme-init.scm environments.scm interp.scm lang.scm tests.scm top.scm CS7100(pm) 22
23 EOPL3 Appendix B SLLGEN (define scanner-spec-1...) (define grammar-1...) (sllgen:make-define-datatypes scanner-spec-1 grammar-1) (define list-the-datatypes (lambda () (sllgen:list-define-datatypes scanner-spec-1 grammar-1))) (define just-scan (sllgen:make-string-scanner scannerspec-1 grammar-1)) (define scan&parse (sllgen:make-string-parser scannerspec-1 grammar-1)) (define read-eval-print (sllgen:make-rep-loop "--> " valueof--program (sllgen:make-stream-parser scannerspec-1 grammar-1))) sllgen:make-definedatatypes: generates a define-datatype for each production of the grammar, for use by cases. sllgen:make-string-scanner takes a scanner spec and a grammar and generates a scanning procedure read-eval-print loop CS7100(pm) 23
24 Lexical Analysis (define the-lexical-spec '((whitespace (whitespace) skip) (comment ("%" (arbno (not #\newline))) skip) (identifier (letter (arbno (or letter digit "_" "-" "?"))) symbol) (number (digit (arbno digit)) number) (number ("-" digit (arbno digit)) number) )) the-lexical-spec from chapter3/ proc-lang/*/lang.scm scanners are specified by reg exp next slide All our languages use this lexical analysis. CS7100(pm) 24
25 SLLGEN Scanner Spec Scanner-spec ::= ({Regexp-and-action} ) Regexp-and-action ::= (Name ({Regexp} ) Action) Name ::= Symbol Regexp ::= String letter digit whitespace any ::= (not Character) (or {Regexp} ) ::= (arbno Regexp) (concat {Regexp} ) Action ::= skip symbol number string A scanner specification in SLLGEN is a list that satisfies the grammar at left CS7100(pm) 25
26 The SLLGEN Parsing System (define the-grammar '((program (expression) a-program) (expression (number) const-exp) (expression ("-" "(" expression "," expression ")") diff-exp) (expression ("zero?" "(" expression ")") zero?-exp) (expression ("if" expression "then" expression "else" expression) if-exp) (expression (identifier) var-exp) (expression ("let" identifier "=" expression "in" expression) let-exp) the-grammar of PROC from chapter3/ proc-lang/*/lang.scm Double-quoted items are terminals/tokens. (expression ("proc" "(" identifier ")" expression) proc-exp) (expression ("(" expression expression ")") call-exp) )) CS7100(pm) 26
27 Specifying Grammars Grammar ::= ({Production} ) Production ::= (Lhs ({Ritem} ) Prodname) Lhs ::= Symbol Ritem ::= Symbol String ::= (arbno {Ritem} ) ::= (separated-list {Ritem} String) Prod-name ::= Symbol A grammar in SLLGEN is a list described by the grammar at left CS7100(pm) 27
28 HW2 Problem (define closure (lambda (ids body env) (let ((freevars (set-diff (free-vars body) ids))) (let ((saved-env (extend-env freevars (map (lambda (v) freevars) (empty-env)))) (lambda (args) (eval-expression body (extend-env ids args saved-env))))))) (apply-env env v)) ses/7100/top/7100-hw2.html In our data-structure representation of procedures, we have kept the entire environment in the closure. But of course all we need are the bindings for the free variables. Modify the representation of procedures to retain only the free variables. flat closure rep shown left consists of exactly one rib of free variables and their values. free-vars: ykwim ;-) set-diff: difference of two sets map provided from r5rs CS7100(pm) 28
Lecture 3: Expressions
CS 7400 September 23 30, 2009 Dr. Wand Readings: EOPL3, Secs. 3.1 3.5 Lecture 3: Expressions Key Concepts: syntax and semantics expressed and denoted values expressions environment specifying the behavior
More informationLecture 12: Conditional Expressions and Local Binding
Lecture 12: Conditional Expressions and Local Binding Introduction Corresponds to EOPL 3.3-3.4 Please review Version-1 interpreter to make sure that you understand how it works Now we will extend the basic
More informationB The SLLGEN Parsing System
B The SLLGEN Parsing System Programs are just strings of characters. In order to process a program, we need to group these characters into meaningful units. This grouping is usually divided into two stages:
More informationLecture 2: Data Types and their Representations; Syntax: Scanning and Parsing
CS 7400 September 16-21, 2009 Dr. Wand Readings: EOPL, Chap. 2 Lecture 2: Data Types and their Representations; Syntax: Scanning and Parsing Key Concepts: Data types The Data Abstraction Principle Interface,
More informationKonzepte von Programmiersprachen
Konzepte von Programmiersprachen Chapter 5: Continuation-Passing Interpreters Stefan Wehr Universität Freiburg 8. Juni 2009 Konzepte von Programmiersprachen 1 / 43 Motivation Continuations: abstraction
More informationMid-Term 2 Grades
Mid-Term 2 Grades 100 46 1 HW 9 Homework 9, in untyped class interpreter: Add instanceof Restrict field access to local class Implement overloading (based on argument count) Due date is the same as for
More informationParsing Scheme (+ (* 2 3) 1) * 1
Parsing Scheme + (+ (* 2 3) 1) * 1 2 3 Compiling Scheme frame + frame halt * 1 3 2 3 2 refer 1 apply * refer apply + Compiling Scheme make-return START make-test make-close make-assign make- pair? yes
More informationFunctional Programming. Pure Functional Programming
Functional Programming Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends only on the values of its sub-expressions (if any).
More information6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson
6.184 Lecture 4 Interpretation Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson 1 Interpretation Parts of an interpreter Arithmetic calculator
More informationLexical Addresses. let x = 1 y = 2 in let f = proc (x) +(x, y) in (f x) let _ = 1 _ = 2 in let _ = proc (_) +(<0,0>, <1,1>) in (<0,0> <1,0>)
Lexical Addresses As we saw in the last lecture, the expression might be compiled to let x = 1 y = 2 in let f = proc (x) +(x, y) in (f x) let _ = 1 _ = 2 in let _ = proc (_) +(, ) in ( )
More informationComp 411 Principles of Programming Languages Lecture 7 Meta-interpreters. Corky Cartwright January 26, 2018
Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters Corky Cartwright January 26, 2018 Denotational Semantics The primary alternative to syntactic semantics is denotational semantics.
More informationLecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.
Lecture 09: Data Abstraction ++ Parsing Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree. program text Parser AST Processor Compilers (and some interpreters)
More informationComp 311: Sample Midterm Examination
Comp 311: Sample Midterm Examination October 29, 2007 Name: Id #: Instructions 1. The examination is closed book. If you forget the name for a Scheme operation, make up a name for it and write a brief
More informationData Abstraction. An Abstraction for Inductive Data Types. Philip W. L. Fong.
Data Abstraction An Abstraction for Inductive Data Types Philip W. L. Fong pwlfong@cs.uregina.ca Department of Computer Science University of Regina Regina, Saskatchewan, Canada Introduction This lecture
More informationScheme in Scheme: The Metacircular Evaluator Eval and Apply
Scheme in Scheme: The Metacircular Evaluator Eval and Apply CS21b: Structure and Interpretation of Computer Programs Brandeis University Spring Term, 2015 The metacircular evaluator is A rendition of Scheme,
More informationCS 342 Lecture 8 Data Abstraction By: Hridesh Rajan
CS 342 Lecture 8 Data Abstraction By: Hridesh Rajan 1 Com S 342 so far So far we have studied: Language design goals, Basic functional programming, Flat recursion over lists, Notion of Scheme procedures
More information6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter
6.037 Lecture 4 Interpretation Interpretation Parts of an interpreter Meta-circular Evaluator (Scheme-in-scheme!) A slight variation: dynamic scoping Original material by Eric Grimson Tweaked by Zev Benjamin,
More informationPrinciples of Programming Languages
Principles of Programming Languages Lesson 14 Type Checking Collaboration and Management Dana Fisman www.cs.bgu.ac.il/~ppl172 1 Type Checking We return to the issue of type safety we discussed informally,
More informationPrinciples of Programming Languages 2017W, Functional Programming
Principles of Programming Languages 2017W, Functional Programming Assignment 3: Lisp Machine (16 points) Lisp is a language based on the lambda calculus with strict execution semantics and dynamic typing.
More informationA LISP Interpreter in ML
UNIVERSITY OF OSLO Department of Informatics A LISP Interpreter in ML Mandatory Assignment 1 INF3110 September 21, 2009 Contents 1 1 Introduction The purpose of this assignment is to write an interpreter,
More informationCSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Autumn 2018 Typical workflow concrete syntax (string) "(fn x => x + x) 4" Parsing Possible errors / warnings
More informationLecture 16: Object Programming Languages
Lecture 16: Object Programming Languages Introduction Corresponds to EOPL 5.1 and 5.2 Goal: to introduce Object Oriented Programming Language (OOPL) concepts using the EOPL extensible language framework
More informationAn Introduction to Functions
Chapter 4 An Introduction to Functions Through the agency of with, we have added identifiers and the ability to name expressions to the language. Much of the time, though, simply being able to name an
More informationCMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics
Recall Architecture of Compilers, Interpreters CMSC 330: Organization of Programming Languages Source Scanner Parser Static Analyzer Operational Semantics Intermediate Representation Front End Back End
More informationStreams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson
Streams, Delayed Evaluation and a Normal Order Interpreter CS 550 Programming Languages Jeremy Johnson 1 Theme This lecture discusses the stream model of computation and an efficient method of implementation
More informationDiscussion 12 The MCE (solutions)
Discussion 12 The MCE (solutions) ;;;;METACIRCULAR EVALUATOR FROM CHAPTER 4 (SECTIONS 4.1.1-4.1.4) of ;;;; STRUCTURE AND INTERPRETATION OF COMPUTER PROGRAMS ;;;from section 4.1.4 -- must precede def of
More informationCMSC 330: Organization of Programming Languages
CMSC 330: Organization of Programming Languages Operational Semantics CMSC 330 Summer 2018 1 Formal Semantics of a Prog. Lang. Mathematical description of the meaning of programs written in that language
More informationLecture 15 CIS 341: COMPILERS
Lecture 15 CIS 341: COMPILERS Announcements HW4: OAT v. 1.0 Parsing & basic code generation Due: March 28 th No lecture on Thursday, March 22 Dr. Z will be away Zdancewic CIS 341: Compilers 2 Adding Integers
More informationHigher-Order Functions (Part I)
Higher-Order Functions (Part I) 1 Why Functions as Values Abstraction is easier with functions as values abstract over add and sub cases filter, map, etc. What are objects? Callbacks? Separate deffun form
More informationFunctions & First Class Function Values
Functions & First Class Function Values PLAI 1st ed Chapter 4, PLAI 2ed Chapter 5 The concept of a function is itself very close to substitution, and to our with form. Consider the following morph 1 {
More informationCOP4020 Programming Assignment 1 - Spring 2011
COP4020 Programming Assignment 1 - Spring 2011 In this programming assignment we design and implement a small imperative programming language Micro-PL. To execute Mirco-PL code we translate the code to
More informationFall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of
1 MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001 Structure and Interpretation of Computer Programs Fall Semester, 1996 Lecture Notes { October 31,
More informationHigher-Order Functions
Higher-Order Functions 1 Why Functions as Values Abstraction is easier with functions as values abstract over add and sub cases filter, map, etc. What are objects? Callbacks? Separate deffun form becomes
More informationCSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)
CSE 413 Languages & Implementation Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341) 1 Goals Representing programs as data Racket structs as a better way to represent
More informationCS 360 Programming Languages Interpreters
CS 360 Programming Languages Interpreters Implementing PLs Most of the course is learning fundamental concepts for using and understanding PLs. Syntax vs. semantics vs. idioms. Powerful constructs like
More informationCS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find
CS1622 Lecture 15 Semantic Analysis CS 1622 Lecture 15 1 Semantic Analysis How to build symbol tables How to use them to find multiply-declared and undeclared variables. How to perform type checking CS
More informationProgramming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421) #3: Closures, evaluation of function applications, order of evaluation #4: Evaluation and Application rules using symbolic rewriting Madhusudan Parthasarathy
More informationIntroduction to Scheme
How do you describe them Introduction to Scheme Gul Agha CS 421 Fall 2006 A language is described by specifying its syntax and semantics Syntax: The rules for writing programs. We will use Context Free
More informationHofl, a Higher-order Functional Language. 1 An Overview of Hofl. 2 Abstractions and Function Applications
CS251 Programming Languages Handout # 1 Prof. Lyn Turbak May 09, 2018 Wellesley College Hofl, a Higher-order Functional Language Hofl (Higher Order Functional Language) is a language that extends Valex
More informationCompiling and Interpreting Programming. Overview of Compilers and Interpreters
Copyright R.A. van Engelen, FSU Department of Computer Science, 2000 Overview of Compilers and Interpreters Common compiler and interpreter configurations Virtual machines Integrated programming environments
More informationFOFL and FOBS: First-Order Functions
CS251 Programming Languages Handout # 35 Prof. Lyn Turbak April 14, 2005 Wellesley College Revised April 24, 2005 FOFL and FOBS: First-Order Functions Revisions: Apr 24 : In the interpreters for Fofl and
More informationEssentials of Programming Languages Language
Essentials of Programming Languages Language Version 5.3 August 6, 2012 The Essentials of Programming Languages language in DrRacket provides a subset of functions and syntactic forms of racket mostly
More informationEssentials of Programming Languages Language
Essentials of Programming Languages Language Version 6.90.0.26 April 20, 2018 The Essentials of Programming Languages language in DrRacket provides a subset of functions and syntactic forms of racket mostly
More informationCS251 Programming Languages Handout # 36 Prof. Lyn Turbak April 4, 2007 Wellesley College Revised April 14, Scoping in Hofl
CS251 Programming Languages Handout # 36 Prof. Lyn Turbak April 4, 2007 Wellesley College Revised April 14, 2007 Scoping in Hofl April 10 Added a new section on the handling of bindrec in the substitution
More informationBuilding up a language SICP Variations on a Scheme. Meval. The Core Evaluator. Eval. Apply. 2. syntax procedures. 1.
6.001 SICP Variations on a Scheme Scheme Evaluator A Grand Tour Techniques for language design: Interpretation: eval/appl Semantics vs. snta Sntactic transformations Building up a language... 3. 1. eval/appl
More informationCMSC 330: Organization of Programming Languages. Lambda Calculus
CMSC 330: Organization of Programming Languages Lambda Calculus 1 Turing Completeness Turing machines are the most powerful description of computation possible They define the Turing-computable functions
More informationCS 415 Midterm Exam Spring 2002
CS 415 Midterm Exam Spring 2002 Name KEY Email Address Student ID # Pledge: This exam is closed note, closed book. Good Luck! Score Fortran Algol 60 Compilation Names, Bindings, Scope Functional Programming
More informationCSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures
CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures Dan Grossman Fall 2014 Hi! I m not Hal J I love this stuff and have taught
More informationCMSC 330: Organization of Programming Languages. Operational Semantics
CMSC 330: Organization of Programming Languages Operational Semantics Notes about Project 4, Parts 1 & 2 Still due today (7/2) Will not be graded until 7/11 (along with Part 3) You are strongly encouraged
More informationTurtles All The Way Down
Turtles All The Way Down Bertrand Russell had just finished giving a public lecture on the nature of the universe. An old woman said Prof. Russell, it is well known that the earth rests on the back of
More information1. For every evaluation of a variable var, the variable is bound.
7 Types We ve seen how we can use interpreters to model the run-time behavior of programs. Now we d like to use the same technology to analyze or predict the behavior of programs without running them.
More informationCSE341, Spring 2013, Final Examination June 13, 2013
CSE341, Spring 2013, Final Examination June 13, 2013 Please do not turn the page until 8:30. Rules: The exam is closed-book, closed-note, except for both sides of one 8.5x11in piece of paper. Please stop
More informationCSSE 304 Assignment #13 (interpreter milestone #1) Updated for Fall, 2018
CSSE 304 Assignment #13 (interpreter milestone #1) Updated for Fall, 2018 Deliverables: Your code (submit to PLC server). A13 participation survey (on Moodle, by the day after the A13 due date). This is
More informationCode example: sfact SICP Explicit-control evaluator. Example register machine: instructions. Goal: a tail-recursive evaluator.
6.001 SICP Explicit-control evaluator Big ideas: how to connect evaluator to machine instructions how to achieve tail recursion Obfuscation: tightly optimized instruction sequence Background eval-dispatch
More informationCS 275 Name Final Exam Solutions December 16, 2016
CS 275 Name Final Exam Solutions December 16, 2016 You may assume that atom? is a primitive procedure; you don t need to define it. Other helper functions that aren t a standard part of Scheme you need
More informationSemantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End
Outline Semantic Analysis The role of semantic analysis in a compiler A laundry list of tasks Scope Static vs. Dynamic scoping Implementation: symbol tables Types Static analyses that detect type errors
More informationTypical workflow. CSE341: Programming Languages. Lecture 17 Implementing Languages Including Closures. Reality more complicated
Typical workflow concrete synta (string) "(fn => + ) 4" Parsing CSE341: Programming Languages abstract synta (tree) Lecture 17 Implementing Languages Including Closures Function Constant + 4 Var Var Type
More informationAssigning to a Variable
What is the result of this program? Is it 0 or 1? Assigning to a Variable let f = proc(x) set x = 1 in let y = 0 in { (f y); y } 1 Assigning to a Variable let f = proc(x) set x = 1 in let y = 0 in { (f
More informationHofl: First-class Functions and Scoping
CS251 Programming Languages Handout # 34 Prof. Lyn Turbak April 21, 2005 Wellesley College Hofl: First-class Functions and Scoping This is a preliminary version that does not include a discussion of bindrec.
More informationcs173: Programming Languages Midterm Exam
cs173: Programming Languages Midterm Exam Fall 2002 Please read this page of instructions before you turn the page! This exam is worth 181 points. We will assign partial credit to partial responses, provided
More informationCVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions
CVO103: Programming Languages Lecture 5 Design and Implementation of PLs (1) Expressions Hakjoo Oh 2018 Spring Hakjoo Oh CVO103 2018 Spring, Lecture 5 April 3, 2018 1 / 23 Plan Part 1 (Preliminaries):
More informationThe role of semantic analysis in a compiler
Semantic Analysis Outline The role of semantic analysis in a compiler A laundry list of tasks Scope Static vs. Dynamic scoping Implementation: symbol tables Types Static analyses that detect type errors
More informationProject 2: Scheme Interpreter
Project 2: Scheme Interpreter CSC 4101, Fall 2017 Due: 12 November 2017 For this project, you will implement a simple Scheme interpreter in C++ or Java. Your interpreter should be able to handle the same
More informationTail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)
CMSC 330: Organization of Programming Languages Tail Calls A tail call is a function call that is the last thing a function does before it returns let add x y = x + y let f z = add z z (* tail call *)
More informationTyped Recursion {with {mk-rec : (((num -> num) -> (num -> num)) -> (num -> num)) {fun {body : ((num -> num) -> (num -> num))} {{fun {fx :
Recursion {with {mk-rec {fun {body} {{fun {fx} {fx fx}} {fun {fx} {{fun {f} {body f}} {fun {x} {{fx fx} x}}}}}}} {with {fib {mk-rec {fun {fib} {fun {n} {if0 n 1 {if0 {- n 1} 1 {+ {fib {- n 1}} {fib {-
More informationName EID. (calc (parse '{+ {with {x {+ 5 5}} {with {y {- x 3}} {+ y y} } } z } ) )
CS 345 Spring 2010 Midterm Exam Name EID 1. [4 Points] Circle the binding instances in the following expression: (calc (parse '+ with x + 5 5 with y - x 3 + y y z ) ) 2. [7 Points] Using the following
More informationEnvironments
Environments PLAI Chapter 6 Evaluating using substitutions is very inefficient To work around this, we want to use a cache of substitutions. We begin evaluating with no cached substitutions, then collect
More informationCMSC 330: Organization of Programming Languages
CMSC 330: Organization of Programming Languages Type Systems, Names and Binding CMSC 330 - Spring 2013 1 Topics Covered Thus Far! Programming languages Ruby OCaml! Syntax specification Regular expressions
More informationCOP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher
COP4020 ming Languages Compilers and Interpreters Robert van Engelen & Chris Lacher Overview Common compiler and interpreter configurations Virtual machines Integrated development environments Compiler
More informationFundamentals of Artificial Intelligence COMP221: Functional Programming in Scheme (and LISP)
Fundamentals of Artificial Intelligence COMP221: Functional Programming in Scheme (and LISP) Prof. Dekai Wu Department of Computer Science and Engineering The Hong Kong University of Science and Technology
More informationCMSC 330: Organization of Programming Languages. Lambda Calculus
CMSC 330: Organization of Programming Languages Lambda Calculus 1 100 years ago Albert Einstein proposed special theory of relativity in 1905 In the paper On the Electrodynamics of Moving Bodies 2 Prioritätsstreit,
More informationRuby: Introduction, Basics
Ruby: Introduction, Basics Computer Science and Engineering College of Engineering The Ohio State University Lecture 3 Ruby vs Java: Similarities Imperative and object-oriented Classes and instances (ie
More informationWriting a Lexer. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, February 6, Glenn G.
Writing a Lexer CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, February 6, 2017 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks
More information9 Objects and Classes
9 Objects and Classes Many programming tasks require the program to manage some piece of state through an interface. For example, a file system has internal state, but we access and modify that state only
More informationn n Try tutorial on front page to get started! n spring13/ n Stack Overflow!
Announcements n Rainbow grades: HW1-6, Quiz1-5, Exam1 n Still grading: HW7, Quiz6, Exam2 Intro to Haskell n HW8 due today n HW9, Haskell, out tonight, due Nov. 16 th n Individual assignment n Start early!
More informationBasics of Using Lisp! Gunnar Gotshalks! BLU-1
Basics of Using Lisp BLU-1 Entering Do Lisp work Exiting Getting into and out of Clisp % clisp (bye)» A function with no arguments» CTRL d can also be used Documentation files are in the directory» /cse/local/doc/clisp
More informationScala : an LLVM-targeted Scala compiler
Scala : an LLVM-targeted Scala compiler Da Liu, UNI: dl2997 Contents 1 Background 1 2 Introduction 1 3 Project Design 1 4 Language Prototype Features 2 4.1 Language Features........................................
More informationTopics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited
CMSC 330: Organization of Programming Languages Type Systems, Names & Binding Topics Covered Thus Far Programming languages Syntax specification Regular expressions Context free grammars Implementation
More informationThe Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.
The Compiler So Far CSC 4181 Compiler Construction Scanner - Lexical analysis Detects inputs with illegal tokens e.g.: main 5 (); Parser - Syntactic analysis Detects inputs with ill-formed parse trees
More informationCS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)
CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square) Introduction This semester, through a project split into 3 phases, we are going
More informationThe Environment Model. Nate Foster Spring 2018
The Environment Model Nate Foster Spring 2018 Review Previously in 3110: Interpreters: ASTs, evaluation, parsing Formal syntax: BNF Formal semantics: dynamic: small-step substitution model static semantics
More informationFunctional Programming. Pure Functional Languages
Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure
More informationEvery language has its own scoping rules. For example, what is the scope of variable j in this Java program?
Lexical Binding There are two ways a variable can be used in a program: As a declaration As a "reference" or use of the variable Scheme has two kinds of variable "declarations" -- the bindings of a let-expression
More informationCA Compiler Construction
CA4003 - Compiler Construction Semantic Analysis David Sinclair Semantic Actions A compiler has to do more than just recognise if a sequence of characters forms a valid sentence in the language. It must
More informationLECTURE 3. Compiler Phases
LECTURE 3 Compiler Phases COMPILER PHASES Compilation of a program proceeds through a fixed series of phases. Each phase uses an (intermediate) form of the program produced by an earlier phase. Subsequent
More informationFunctional Programming
Functional Programming CS331 Chapter 14 Functional Programming Original functional language is LISP LISt Processing The list is the fundamental data structure Developed by John McCarthy in the 60 s Used
More informationCS 314 Principles of Programming Languages
CS 314 Principles of Programming Languages Lecture 16: Functional Programming Zheng (Eddy Zhang Rutgers University April 2, 2018 Review: Computation Paradigms Functional: Composition of operations on data.
More informationCSE 401 Midterm Exam Sample Solution 2/11/15
Question 1. (10 points) Regular expression warmup. For regular expression questions, you must restrict yourself to the basic regular expression operations covered in class and on homework assignments:
More informationA lexical analyzer generator for Standard ML. Version 1.6.0, October 1994
A lexical analyzer generator for Standard ML. Version 1.6.0, October 1994 Andrew W. Appel 1 James S. Mattson David R. Tarditi 2 1 Department of Computer Science, Princeton University 2 School of Computer
More informationPrinciples of Programming Languages
Principles of Programming Languages www.cs.bgu.ac.il/~ppl172 Lesson 6 - Defining a Programming Language Bottom Up Collaboration and Management - Elements of Programming Dana Fisman 1 What we accomplished
More informationFunctional Programming. Big Picture. Design of Programming Languages
Functional Programming Big Picture What we ve learned so far: Imperative Programming Languages Variables, binding, scoping, reference environment, etc What s next: Functional Programming Languages Semantics
More informationImplementing Continuations
Implementing Continuations sk and dbtucker 2002-10-18 1 Changing Representations Now that we ve seen how continuations work, let s study how to implement them in an interpreter. For this lecture onward,
More informationComputer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing
Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs Lexical addressing The difference between a interpreter and a compiler is really two points on a spectrum of possible
More informationFunctions and Recursion. Dr. Philip Cannata 1
Functions and Recursion Dr. Philip Cannata 1 10 High Level Languages This Course Java (Object Oriented) Jython in Java Relation ASP RDF (Horn Clause Deduction, Semantic Web) Dr. Philip Cannata 2 let transformation,
More informationC311 Lab #3 Representation Independence: Representation Independent Interpreters
C311 Lab #3 Representation Independence: Representation Independent Interpreters Will Byrd webyrd@indiana.edu February 5, 2005 (based on Professor Friedman s lecture on January 29, 2004) 1 Introduction
More informationAgenda. CS301 Session 9. Abstract syntax: top level. Abstract syntax: expressions. The uscheme interpreter. Approaches to solving the homework problem
CS301 Session 9 The uscheme interpreter Agenda Approaches to solving the homework problem 1 2 Abstract syntax: top level datatype toplevel = EXP of exp DEFINE of name * lambda VAL of name * exp USE of
More informationCS 342 Lecture 6 Scheme Procedures and Closures By: Hridesh Rajan
CS 342 Lecture 6 Scheme Procedures and Closures By: Hridesh Rajan 1 Reading Little Schemer Chapter 8 SICP 1, 3.2 2 Lecture Overview In this lecture, we will cover scheme procedures in a bit more detail
More informationCMSC 330: Organization of Programming Languages
CMSC 330: Organization of Programming Languages Lambda Calculus CMSC 330 Summer 2017 1 100 years ago Albert Einstein proposed special theory of relativity in 1905 In the paper On the Electrodynamics of
More informationDecaf Language Reference Manual
Decaf Language Reference Manual C. R. Ramakrishnan Department of Computer Science SUNY at Stony Brook Stony Brook, NY 11794-4400 cram@cs.stonybrook.edu February 12, 2012 Decaf is a small object oriented
More informationLecture 13 CIS 341: COMPILERS
Lecture 13 CIS 341: COMPILERS Announcements HW4: OAT v. 1.0 Parsing & basic code generation Due: March 28 th START EARLY! Midterm Exam Grades Available on Gradescope Zdancewic CIS 341: Compilers 2 Midterm
More information