CMSC 330: Organization of Programming Languages

Slides:



Advertisements
Similar presentations
Closures & Environments CS153: Compilers Greg Morrisett.
Advertisements

AST Generation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 9.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
Writing functions in OCaml. Defining a simple function # let add (x, y) = x + y;; val add : int * int -> int = Notice what this says: –add is a value.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
1 Scheme Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition In Scheme, a function is defined.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
The Scheme Programming Language History and Significance Dmitry Nesvizhsky CIS24 Professor Danny Kopec.
SICP Interpretation part 1 Parts of an interpreter Arithmetic calculator Names Conditionals and if Store procedures in the environment.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office.
Functional Programing Referencing material from Programming Language Pragmatics – Third Edition – by Michael L. Scott Andy Balaam (Youtube.com/user/ajbalaam)
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Domain Specific Languages in Erlang Dennis Byrne
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 5.
Dr. Philip Cannata 1 Functions and Recursion. Dr. Philip Cannata 2 10 Java (Object Oriented) ASP RDF (Horn Clause Deduction, Semantic Web) Relation Jython.
Abstraction: Procedures as Parameters CMSC Introduction to Computer Programming October 14, 2002.
CSE 413 Languages & Implementation Hal Perkins Autumn 2012 Structs, Implementing Languages (credits: Dan Grossman, CSE 341) 1.
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
Programming Languages: Design, Specification, and Implementation G Rob Strom September 21, 2006.
PZ03BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03BX –Recursive descent parsing Programming Language.
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Scope: What’s in a Name? CMSC Introduction to Computer Programming October 16, 2002.
Interpreters and Higher-Order Functions CSE 413 Autumn 2008 Credit: CSE341 notes by Dan Grossman.
CPSC 388 – Compiler Design and Construction Parsers – Syntax Directed Translation.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
CMSC 330: Organization of Programming Languages Operational Semantics.
Visitor-Based HMM Israel Perez, Fayz Rahman, Chinedu Egboh.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office Functional Programming.
1 Vectors, binary search, and sorting. 2 We know about lists O(n) time to get the n-th item. Consecutive cons cell are not necessarily consecutive in.
CS314 – Section 5 Recitation 9
Functional Programming
Lecture 4: Metacircles Eval Apply David Evans
CS314 – Section 5 Recitation 10
Functional Programming
Functional Programming
Constructing Precedence Table
Programming Languages Dan Grossman 2013
Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications) cs7100(Prasad) L8Interp.
CS 326 Programming Languages, Concepts and Implementation
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2013.
Original material by Eric Grimson
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2018.
FP Foundations, Scheme In Text: Chapter 14.
Functional Programming
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2017.
Bindings, Scope, and Extent
The Metacircular Evaluator (Continued)
Explicit Application of Procedures, and Explicit Evaluation
6.001 SICP Variations on a Scheme
CSE 3302 Programming Languages
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2016.
The Recursive Descent Algorithm
6.001 SICP Interpretation Parts of an interpreter
topics interpreters meta-linguistic abstraction eval and apply
More Scheme CS 331.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2019.
Presentation transcript:

CMSC 330: Organization of Programming Languages Project 4 - Scheme Parser & Interpreter

Project 4 Overview Scheme programming Scheme interpreter Scheme parser Write some functions in Scheme Scheme interpreter Given Scheme AST Evaluate AST Scheme parser Write recursive descent parser Build Scheme AST CMSC 330

Scheme Functional programming language Features Steele & Sussman @ MIT, 1975 Based on LISP Lots of Idiotic Stupid Parentheses But uses lexical scoping Resembles lambda calculus Features Higher order functions – lambda (x) (…) Builds lists using cons cells - cons, car, cdr CMSC 330 3 3

Scheme Examples Function evaluation (+ 1 2) evaluates to 3 CMSC 330 4 4

Scheme Examples Booleans (= 1 2) evaluates to #f (= 1 1) evaluates to #t (bool? #t) evaluates to #t (bool? 6) evaluates to #f CMSC 330 5 5

Scheme Examples Global bindings Creating functions (define three 3) (+ three 4) evaluates to 7 Creating functions (define add-two (lambda (n) (+ n 2))) (add-two 5) evaluates to 7 CMSC 330 6 6

Scheme Examples Recursive functions (define fact (lambda (n) (if (= n 0) 1 (* n (fact (- n 1)))))) (fact 3) evaluates to 6 (fact 5) evaluates to 120 CMSC 330 7 7

Note lexical scoping for x in foo Scheme Examples Higher order functions (define x 52) (define foo (lambda (x) (lambda (y) (+ x y)))) (define x 25) ((foo 3) 4) evaluates to 7 Note lexical scoping for x in foo CMSC 330 8 8

Use car / cdr to deconstruct lists Scheme Examples Lists (define x (cons 3 2)) (car x) evaluates to 3 (cdr x) evaluates to 2 (define y (cons 4 x)) (car y) evaluates to 4 (cdr y) evaluates to (3 2) Use cons to build lists Use car / cdr to deconstruct lists CMSC 330 9 9

Starting OCaml Code – scheme.ml Type ast Represents Scheme abstract syntax tree type ast = Id of string | Num of int | Bool of bool | String of string | List of ast list CMSC 330 10 10

Starting OCaml Code – scheme.ml Type value Represents Scheme values type value = Val_Num of int | Val_Bool of bool | Val_String of string | Val_Nil | Val_Cons of value * value You will need to add new fields (e.g., closure) But do not modify existing fields! CMSC 330 11 11

Starting OCaml Code – scheme.ml Type token Represents Scheme tokens type token = TId of string | TNum of int | TString of string | TTrue | TFalse | TLParen | TRParen CMSC 330 12 12

Project 4 – Part 1 Scheme programming Gain experience with Scheme programs Write simple recursive functions double x → two times x powof2 x → true (#t) iff x is a power of 2 sum l → sum of the integer list l map f l → list containing elements of l with f applied to them CMSC 330 13 13

Project 4 – Part 2 Scheme interpreter Given Scheme AST Evaluate AST to produce value define, values, lambda, identifiers, function calls, primitives Can test interpreter without parser Using manually constructed Scheme AST CMSC 330 14 14

Project 4 – Part 3 Scheme parser Given scanner Converts input strings into sequence of tokens Write recursive descent parser Convert list of tokens into Scheme AST CMSC 330 15 15

Project 4 Notes Project files Testing basic.scm → your scheme code for part 1 scheme.ml → your code. Make all your edits here main.ml → interpreter using code from scheme.ml sample.output → example input / output (public test) Testing ocaml scheme.ml → test for syntax / type errors ocaml main.ml → run scheme interpreter ocaml public_eval1.ml → run 1st eval public test Etc… CMSC 330 16 16