CSE 341 Section 7. Ethan Shea Autumn Adapted from slides by Nicholas Shahan, Dan Grossman, Tam Dang, and Eric Mullen

Similar documents
CSE 341 Section 7. Eric Mullen Spring Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang

Writing Interpreters Racket eval. CSE341 Section 7. ASTs, Interpreters, MUPL. Sunjay Cauligi. February 21 st, Sunjay Cauligi CSE341 Section 7

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

Typical workflow. CSE341: Programming Languages. Lecture 17 Implementing Languages Including Closures. Reality more complicated

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

CSE341, Spring 2013, Final Examination June 13, 2013

CSE341 Autumn 2017, Final Examination December 12, 2017

CSE341 Spring 2017, Final Examination June 8, 2017

CSE341 Spring 2016, Final Examination June 6, 2016

Concepts of programming languages

CS 360 Programming Languages Interpreters

CSE341 Spring 2017, Final Examination June 8, 2017

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

CSE 341, Spring 2011, Final Examination 9 June Please do not turn the page until everyone is ready.

Case Study: Undefined Variables

Racket. CSE341: Programming Languages Lecture 14 Introduction to Racket. Getting started. Racket vs. Scheme. Example.

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

CS 360: Programming Languages Lecture 12: More Haskell

Formal Semantics. Chapter Twenty-Three Modern Programming Languages, 2nd ed. 1

Pattern Matching for Scheme

CSE341 Section 3. Standard-Library Docs, First-Class Functions, & More

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

The role of semantic analysis in a compiler

Macros & Streams Spring 2018 Discussion 9: April 11, Macros

CS 314 Principles of Programming Languages

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Parsing CSCI-400. Principles of Programming Languages.

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

6.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

CSE341: Programming Languages Lecture 15 Macros. Zach Tatlock Winter 2018

What is a macro. CSE341: Programming Languages Lecture 15 Macros. Example uses. Using Racket Macros. Zach Tatlock Winter 2018

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

Syntax and Grammars 1 / 21

Extensible Pattern Matching

Scheme: Strings Scheme: I/O

CS152 Programming Language Paradigms Prof. Tom Austin, Fall Syntax & Semantics, and Language Design Criteria

Monty Python and the Holy Grail (1975) BBM 101. Introduction to Programming I. Lecture #03 Introduction to Python and Programming, Control Flow

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

Principles of Programming Languages

Functional Programming. Pure Functional Programming

GNU ccscript Scripting Guide IV

CS115 - Module 10 - General Trees

Functions & First Class Function Values

Introduction to Functional Programming in Racket. CS 550 Programming Languages Jeremy Johnson

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Project 5 - The Meta-Circular Evaluator

Semantic Analysis. Lecture 9. February 7, 2018

CSE 505: Concepts of Programming Languages

Introduction to lambda calculus Part 3

Gene Kim 9/9/2016 CSC 2/444 Lisp Tutorial

Intro. Scheme Basics. scm> 5 5. scm>

CS152: Programming Languages. Lecture 2 Syntax. Dan Grossman Spring 2011

Racket: Macros. Advanced Functional Programming. Jean-Noël Monette. November 2013

1 Lexical Considerations

LECTURE 3. Compiler Phases

CSSE 304 Assignment #13 (interpreter milestone #1) Updated for Fall, 2018

Project 5 - The Meta-Circular Evaluator

Case Study: Free Variables

CS 415 Midterm Exam Spring 2002

Introduction to Scheme

Parsing Scheme (+ (* 2 3) 1) * 1

COP4020 Programming Assignment 1 - Spring 2011

Functional Programming Lecture 1: Introduction

(scheme-1) has lambda but NOT define

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Syntax-Directed Translation. Lecture 14

A lexical analyzer generator for Standard ML. Version 1.6.0, October 1994

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal)

Comp 311: Sample Midterm Examination

Lecture 12: Conditional Expressions and Local Binding

Announcement. Overview. LISP: A Quick Overview. Outline of Writing and Running Lisp.

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

CSE Programming Languages Final exam - Autumn 2012

Lecture 16: Static Semantics Overview 1

A simple syntax-directed

SCHEME AND CALCULATOR 5b

Syntax Errors; Static Semantics

CS152: Programming Languages. Lecture 5 Pseudo-Denotational Semantics. Dan Grossman Spring 2011

Bindings & Substitution

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

Semantic actions for expressions

Basic Concepts. Computer Science. Programming history Algorithms Pseudo code. Computer - Science Andrew Case 2

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

CIS 194: Homework 3. Due Wednesday, February 11, Interpreters. Meet SImPL

Warm-up and Memoization

Arrays. Lecture 9 COP 3014 Fall October 16, 2017

CSCC24 Functional Programming Scheme Part 2

CSE 3302 Programming Languages Lecture 2: Syntax

Data Abstraction. An Abstraction for Inductive Data Types. Philip W. L. Fong.

Types and Static Type Checking (Introducing Micro-Haskell)

First Java Program - Output to the Screen

CS422 - Programming Language Design

Principles of Programming Languages 2017W, Functional Programming

Transcription:

CSE 341 Section 7 Ethan Shea Autumn 2018 Adapted from slides by Nicholas Shahan, Dan Grossman, Tam Dang, and Eric Mullen

Outline Interpreting MUPL Assume Correct Syntax Check for Correct Semantics Evaluating the AST MUPL Macros Program/data equivalence Eval, Quote, and Quasiquote Variable Number of Arguments Apply 2

Building an Interpreter We are skipping the parsing phase Do Not Implement Interpreter written in Racket - Racket is the metalanguage MUPL code represented as an AST - AST nodes represented as Racket structs - Allows us to skip the parsing phase Can assume AST has valid syntax Can NOT assume AST has valid semantics 3

Correct Syntax Examples Using these Racket structs (struct int (num) #:transparent) (struct add (e1 e2) #:transparent) (struct ifnz (e1 e2 e3) #:transparent) we can interpret these MUPL programs: (int 34) (add (int 34) (int 30)) (ifnz (add (int 5) (int 7)) (int 12) (int 1)) 4

Incorrect Syntax Examples While using these Racket structs (struct int (num) #:transparent) (struct add (e1 e2) #:transparent) (struct ifnz (e1 e2 e3) #:transparent) we can assume we won t see MUPL programs like: (int dan then dog ) (int (int 5)) (int (ifnz (int 0) (int 5) (int 7))) (add (int 8) #t) (add 5 4) Illegal input ASTs may crash the interpreter - this is OK 5

Racket vs. MUPL Structs in Racket, when defined to take an argument, can take any Racket value: (struct int (num) #:transparent) (struct add (e1 e2) #:transparent) (struct ifnz (e1 e2 e3) #:transparent) But in MUPL, we restrict int to take only an integer value, add to take two MUPL expressions, and so on (int dan then dog ) (int (ifnz (int 0) (int 5) (int 7))) (add (int 8) #t) (add 5 4) Illegal input ASTs may crash the interpreter - this is OK 6

Racket vs. MUPL Structs in Racket, when defined to take an argument, can take any Racket value: (struct int (num) #:transparent) (struct add (e1 e2) #:transparent) (struct ifnz (e1 e2 e3) #:transparent) So this is valid Racket syntax, but invalid MUPL syntax: (int dan then dog ) (int (ifnz (int 0) (int 5) (int 7))) (add (int 8) #t) (add 5 4) Illegal input ASTs may crash the interpreter - this is OK 7

Evaluating the AST eval-exp should return a MUPL value MUPL values all evaluate to themselves Otherwise, we haven t interpreted far enough ; evaluates to (int 7) (eval-exp (int 7)) ; evaluates to (int 7) (eval-exp (add (int 3) (int 4))) 8

Check for Correct Semantics What if the program is a legal AST, but evaluation of it tries to use the wrong kind of value? For example, add an integer and a function You should detect this and give an error message that is not in terms of the interpreter implementation We need to check that the type of a recursive result is what we expect No need to check if any type is acceptable 9

Check for Correct Semantics These expressions have correct syntax, but incorrect semantics. ; Fails when evaluated (eval-exp (add (int 7) (function id x (var x )) (eval-exp (first (int 4)) 10

Macros Review Extend language syntax (allow new constructs) Written in terms of existing syntax Expanded before language is actually interpreted or compiled 11

MUPL Macros Interpreting MUPL using Racket as the metalanguage MUPL is made up of Racket structs In Racket, these are just data types Why not write a Racket function that returns MUPL ASTs? 12

MUPL Macros If our MUPL Macro is a Racket function (define (++ exp) (add (int 1) exp)) Then the MUPL code (++ (int 7)) Expands to (add (int 1) (int 7)) when evaluated in Racket (at runtime) 13

quote Syntactically, Racket statements can be thought of as lists of tokens (+ 3 4) is a plus sign, a 3, and a 4 quote-ing a parenthesized expression produces a list of tokens 14

quote Examples (+ 3 4) ; 7 (quote (+ 3 4)) ; '(+ 3 4) (quote (+ 3 #t)) ; '(+ 3 #t) (+ 3 #t) ; Error You may also see the single quote character used as syntactic sugar 15

quasiquote Inserts evaluated tokens into a quote Convenient for generating dynamic token lists Use unquote to escape a quasiquote back to evaluated Racket code A quasiquote and quote are equivalent unless we use an unquote operation 16

quasiquote Examples (quasiquote (+ 3 (unquote(+ 2 2)))) ; '(+ 3 4) (quasiquote (string-append "I love CSE" (number->string (unquote (+ 3 338))))) ; '(string-append "I love CSE" (number->string 341)) You may also see the backtick ` character used as syntactic sugar for quasiquote The comma character, is used as syntactic sugar for unquote 17

Self Interpretation Many languages provide an eval function or something similar Performs interpretation or compilation at runtime Needs full language implementation during runtime It's useful, but there's usually a better way Makes analysis, debugging difficult 18

eval Racket's eval operates on lists of tokens Like those generated from quote and quasiquote Treat the input data as a program and evaluate it 19

eval examples (define quoted (quote (+ 3 4))) (eval quoted) ; 7 (define bad-quoted (quote (+ 3 #t))) (eval bad-quoted) ; Error (define qquoted (quasiquote (+ 3 (unquote(+ 2 2))))) (eval qquoted) ; 7 (define big-qquoted (quasiquote (string-append "I love CSE" (number->string (unquote (+ 3 338)))))) (eval big-qquoted) ; I love CSE341 20

apply Applies a list of values as the arguments to a function in order by position (define fn-any (lambda xs ; any number of args (print xs))) (apply fn-any (list 1 2 3 4)) (apply + (list 1 2 3 4)) ; 10 (apply max (list 1 2 3 4)) ; 4 21

Variable Number of Arguments Some functions (like +) can take a variable number of arguments There is syntax that lets you define your own (define fn-any (lambda xs ; any number of args (print xs))) (define fn-1-or-more (lambda (a. xs) ; at least 1 arg (begin (print a) (print xs)))) (define fn-2-or-more (lambda (a b. xs) ; at least 2 args (begin (print a) (print a) (print xs)))) 22

RackUnit Unit testing is built into the standard library http://docs.racket-lang.org/rackunit/ Built in test functions to make testing your code easier Test for equality, check-eq? Test for True, check-true Test for raised exception, check-exn and many more 23