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

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

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

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

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

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

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

Concepts of programming languages

CS 360 Programming Languages Interpreters

CSE341, Spring 2013, Final Examination June 13, 2013

CSE341 Spring 2016, Final Examination June 6, 2016

CSE341 Spring 2017, Final Examination June 8, 2017

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

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

CSE341 Autumn 2017, Final Examination December 12, 2017

Case Study: Undefined Variables

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

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

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

CSE341 Spring 2017, Final Examination June 8, 2017

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

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

Syntax and Grammars 1 / 21

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

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

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

Pattern Matching for Scheme

CSE 505: Concepts of Programming Languages

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

Parsing CSCI-400. Principles of Programming Languages.

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

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

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

The role of semantic analysis in a compiler

CS 415 Midterm Exam Spring 2002

1 Lexical Considerations

Project 5 - The Meta-Circular Evaluator

GNU ccscript Scripting Guide IV

CSE 3302 Programming Languages Lecture 2: Syntax

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

CS115 - Module 10 - General Trees

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Syntax-Directed Translation. Lecture 14

Extensible Pattern Matching

Project 5 - The Meta-Circular Evaluator

COP4020 Programming Assignment 1 - Spring 2011

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

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

Semantic Analysis. Lecture 9. February 7, 2018

CS 360: Programming Languages Lecture 12: More Haskell

Functional Programming. Pure Functional Programming

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

Lecture 16: Static Semantics Overview 1

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

A simple syntax-directed

Scheme: Strings Scheme: I/O

CS 314 Principles of Programming Languages

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

Warm-up and Memoization

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

Semantic actions for expressions

Functional Programming Lecture 1: Introduction

Introduction to Scheme

Arrays. Lecture 9 COP 3014 Fall October 16, 2017

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

LECTURE 3. Compiler Phases

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

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

CS Exam #1-100 points Spring 2011

Should you know scanf and printf?

Case Study: Free Variables

Introduction to lambda calculus Part 3

CS1 Recitation. Week 1

Principles of Programming Languages

A Small Interpreted Language

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

Semantic actions for declarations and expressions

Chapter 4. Abstract Syntax

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages

CSCE 314 Programming Languages

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

(scheme-1) has lambda but NOT define

QUIZ. What is wrong with this code that uses default arguments?

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

Semantic actions for declarations and expressions. Monday, September 28, 15

Syntax Errors; Static Semantics

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

Encodings. Using the minimal λ-calculus language we get. functions. local binding. booleans. numbers... and recursive functions?

Racket Pattern Matching

Lecture 4: The Declarative Sequential Kernel Language. September 5th 2011

Compiler Theory. (Semantic Analysis and Run-Time Environments)

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Modules, Structs, Hashes, and Operational Semantics

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

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

6.945 Adventures in Advanced Symbolic Programming

Lecture 12: Conditional Expressions and Local Binding

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Basics

Syntactic Sugar: Using the Metacircular Evaluator to Implement the Language You Want

Transcription:

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

Outline Interpreting LBI (Language Being Implemented) Assume Correct Syntax Check for Correct Semantics Evaluating the AST LBI Macros Eval, Quote, and Quasiquote Variable Number of Arguments Apply 2

Building an LBI Interpreter We are skipping the parsing phase Do Not Implement Interpreter written in Racket - Racket is the metalanguage LBI 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 LBI 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 LBI programs like: (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 5

Racket vs. LBI 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 LBI, we restrict int to take only an integer value, add to take two LBI 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. LBI 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 LBI 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 LBI value LBI values all evaluate to themselves Otherwise, we haven t interpreted far enough (int 7) ; evaluates to (int 7) (add (int 3) (int 4)) ; evaluates to (int 7) 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

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

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

LBI Macros If our LBI Macro is a Racket function (define (++ exp) (add (int 1) exp)) Then the LBI code (++ (int 7)) Expands to (add (int 1) (int 7)) 12

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 13

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 14

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 15

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 16

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 17

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 18

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 19

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)))) 21

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 22