Interpreters. Prof. Clarkson Fall Today s music: Step by Step by New Kids on the Block

Similar documents
The Substitution Model. Nate Foster Spring 2018

The Substitution Model

Formal Semantics. Prof. Clarkson Fall Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

CMSC 330: Organization of Programming Languages

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

The Environment Model

Lecture 2: Big-Step Semantics

Syntax and Grammars 1 / 21

CS 11 Ocaml track: lecture 7

Functions & First Class Function Values

Syntax-Directed Translation. Lecture 14

Lecture 13 CIS 341: COMPILERS

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

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

Syntax vs. semantics. Detour: Natural deduction. Syntax vs. semantics (cont d) Syntax = grammatical structure Semantics = underlying meaning

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

To figure this out we need a more precise understanding of how ML works

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Introduction to Compiler Design

CSCI 2041: Advanced Language Processing

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

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

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

Parsing. Zhenjiang Hu. May 31, June 7, June 14, All Right Reserved. National Institute of Informatics

Programs as data first-order functional language type checking

Scope and Introduction to Functional Languages. Review and Finish Scoping. Announcements. Assignment 3 due Thu at 11:55pm. Website has SML resources

The Environment Model. Nate Foster Spring 2018

Introduction. Compiler Design CSE Overview. 2 Syntax-Directed Translation. 3 Phases of Translation

Crafting a Compiler with C (II) Compiler V. S. Interpreter

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

ASTs, Objective CAML, and Ocamlyacc

Lecture 14 Sections Mon, Mar 2, 2009

Specifications. Prof. Clarkson Fall Today s music: Nice to know you by Incubus

Variables. Substitution

COP5621 Exam 3 - Spring 2005

Type Inference. Prof. Clarkson Fall Today s music: Cool, Calm, and Collected by The Rolling Stones

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

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

22c:111 Programming Language Concepts. Fall Syntax III

Syntax Errors; Static Semantics

Semantic Analysis. Compiler Architecture

Programming Languages & Translators PARSING. Baishakhi Ray. Fall These slides are motivated from Prof. Alex Aiken: Compilers (Stanford)

Programs as data Parsing cont d; first-order functional language, type checking

To figure this out we need a more precise understanding of how ML works

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

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

CS 360 Programming Languages Interpreters

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

10/18/18. Outline. Semantic Analysis. Two types of semantic rules. Syntax vs. Semantics. Static Semantics. Static Semantics.

Implementing a VSOP Compiler. March 20, 2018

LECTURE 3. Compiler Phases

Parsing. CS321 Programming Languages Ozyegin University

Data Types. Prof. Clarkson Fall Today s music: Pokémon Theme by Jason Paige

Compilers Crash Course

Semantic Analysis. Lecture 9. February 7, 2018

Hard deadline: 3/28/15 1:00pm. Using software development tools like source control. Understanding the environment model and type inference.

A Simple Syntax-Directed Translator

Comp 411 Principles of Programming Languages Lecture 3 Parsing. Corky Cartwright January 11, 2019

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

Implemen'ng a Func'onal Language. A Basic Func2onal Language. Goal: illustrate and understand the features of the run-2me support

A simple syntax-directed

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

Abstract Syntax Trees & Top-Down Parsing

Writing Evaluators MIF08. Laure Gonnord

What is a compiler? var a var b mov 3 a mov 4 r1 cmpi a r1 jge l_e mov 2 b jmp l_d l_e: mov 3 b l_d: ;done

Compiling Regular Expressions COMP360

Behavioral Equivalence

Working of the Compilers

Abstract Syntax Trees Synthetic and Inherited Attributes

Lecture 16: Static Semantics Overview 1

Proofs are Programs. Prof. Clarkson Fall Today s music: Proof by Paul Simon

Extending xcom. Chapter Overview of xcom

COP 3402 Systems Software. Lecture 4: Compilers. Interpreters

Modules, Structs, Hashes, and Operational Semantics

Implementing a VSOP Compiler. March 12, 2018

CS Lecture 7: The dynamic environment. Prof. Clarkson Spring Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

What is a compiler? Xiaokang Qiu Purdue University. August 21, 2017 ECE 573

Lexical Analysis. COMP 524, Spring 2014 Bryan Ward

Semantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis?

Introduction to Parsing. Lecture 8

9/5/17. The Design and Implementation of Programming Languages. Compilation. Interpretation. Compilation vs. Interpretation. Hybrid Implementation

Functions. Nate Foster Spring 2018

CS164: Midterm I. Fall 2003

A LISP Interpreter in ML

Project 2 Interpreter for Snail. 2 The Snail Programming Language

Introduction to Syntax Analysis Recursive-Descent Parsing

Program Assignment 2 Due date: 10/20 12:30pm

Undergraduate Compilers in a Day

Lexical Analysis. Introduction

Program Analysis ( 软件源代码分析技术 ) ZHENG LI ( 李征 )

Semantic Analysis Attribute Grammars

Lists. Prof. Clarkson Fall Today s music: "Blank Space" by Taylor Swift

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

10/26/17. Attribute Evaluation Order. Attribute Grammar for CE LL(1) CFG. Attribute Grammar for Constant Expressions based on LL(1) CFG

Begin at the beginning

Behavioral Equivalence

Programming Languages & Compilers. Programming Languages and Compilers (CS 421) Programming Languages & Compilers. Major Phases of a Compiler

Syntax. A. Bellaachia Page: 1

CS4120/4121/5120/5121 Spring /6 Programming Assignment 4

Transcription:

Interpreters Prof. Clarkson Fall 2017 Today s music: Step by Step by New Kids on the Block

Review Previously in 3110: functional programming modular programming data structures Today: new unit of course: interpreters small-step interpreter for tiny language

COMPILERS AND INTERPRETERS

Compilation Source program Compiler Target program code as data: the compiler is code that operates on data; that data is itself code

Compilation Source program Compiler Input Target program Output the compiler goes away; not needed to run the program

Interpretation Source program Input Interpreter Output the interpreter stays; needed to run the program

Compilation vs. interpretation Compilers: primary job is translation typically lead to better performance of program Interpreters: primary job is execution typically lead to easier implementation of language maybe better error messages and better debuggers

Mixed compilation and interpretation Source program Compiler Intermediate program Input Virtual machine Output the VM is the interpreter; needed to run the program; Java and OCaml can both work this way

Architecture Two phases: Front end: translate source code into abstract syntax tree (AST) Back end: translate AST into machine code Front end of compilers and interpreters largely the same: Lexical analysis with lexer Syntactic analysis with parser Semantic analysis

Front end Character stream: if x=0 then 1 else fact(x-1) Lexer Token stream: if x = 0 then 1 else fact ( x - 1 )

Front end Token stream: if x = 0 then 1 else fact ( x - 1 ) Abstract syntax tree: Parser if-then-else = 1 apply x 0 fact - x 1

Front end Abstract syntax tree: if-then-else = 1 apply x 0 fact - x 1 Semantic analysis accept or reject program decorate AST with types etc.

After the front end Interpreter begins executing code using the abstract syntax tree (AST) Compiler begins translating code into machine language Might involve translating AST into a simpler intermediate representation (IR) Eventually produce object code

Implementation Functional languages are well-suited to implement compilers and interpreters Code easily represented by tree data types Compilation passes easily defined pattern matching on trees Semantics naturally implemented with language constructs

EXPRESSION INTERPRETER

Arithmetic expressions Goal: write an interpreter for expressions involving integers and addition Path to solution: let's assume lexing and parsing is already done need to take in AST and interpret it intuition: an expression e takes a single step to a new expression e' expression keeps stepping until it reaches a value

AST type expr = Int of int Add of expr * expr e.g. Int 5 represents the source expression 5 Add (Int 5) (Add (Int 6) (Int 7)) represents 5+(6+7)

Evaluation by stepping (* A single step of evaluation: * exactly 1 step *) val step : expr -> expr (* Take as many steps as possible until * a value is reached. Could be 0 * or more steps. *) val eval : expr -> expr

Eval let rec eval e = if is_value e then e else eval (step e) (* [is_value e] is whether * [e] is a syntactic value *) let is_value = function Int _ -> true Add _ -> false

Question Given (4+5)+(6+7), what should the first step be? A. 9+(6+7) B. (4+5)+13

Question Given (4+5)+(6+7), what should the first step be? A. 9+(6+7) B. (4+5)+13 Answer: It doesn't matter! (especially in the absence of side effects) But we have to make an implementation choice...

Step, Choice A let rec step = function Int n -> failwith "Does not step" Add(e1, e2) -> Add(step e1, e2)

Step, Choice A let rec step = function Int n -> failwith "Does not step" Add(e1, e2) -> Add(step e1, e2) Add(Int n1, e2) -> Add(Int n1, step e2)

Step, Choice A let rec step = function Int n -> failwith "Does not step" Add(e1, e2) -> Add(step e1, e2) Add(Int n1, e2) -> Add(Int n1, step e2) Stop: we already have a bug How will 5+(6+7) step?

Step, Choice A let rec step = function Int n -> failwith "Does not step" Add(Int n1, e2) -> Add(Int n1, step e2) Add(e1, e2) -> Add(step e1, e2)

Step, Choice A let rec step = function Int n -> failwith "Does not step" Add(Int n1, Int n2) -> Int (n1+n2) Add(Int n1, e2) -> Add(Int n1, step e2) Add(e1, e2) -> Add(step e1, e2)

Step, Choice A let rec step = function Int n -> failwith "Does not step" Add(Int n1, Int n2) -> Int (n1+n2) Add(Int n1, e2) -> Add(Int n1, step e2) Add(e1, e2) -> Add(step e1, e2) Finished!

Step, Choice B let rec step = function Int n -> failwith "Does not step" Add(Int n1, Int n2) -> Int (n1+n2) Add(e1, Int n2) -> Add(step e1, Int n2) Add(e1, e2) -> Add(e1, step e2)

EXTENDED EXPRESSION INTERPRETER

Arithmetic expressions Goal: extend interpreter to let expressions Path to solution: extend AST with a variant for let and for variables add branches to step to handle those that requires substitution...

let expressions [from lec 4] let x = e1 in e2 Evaluation: Evaluate e1 to a value v1 Substitute v1 for x in e2, yielding a new expression e2 Evaluate e2 to v Result of evaluation is v

Substitution Notation: e{v/x} means e with v substituted for x e.g., (x+5){4/x} means (x+5) with 4 substituted for x which would be (4+5) In let semantics: Instead of: "Substitute v1 for x in e2, yielding a new expression e2 ; Evaluate e2 to v" Could now write: "Evaluate e2{v1/x} to v"

Extended AST type expr = Int of int Add of expr * expr Var of string Let of string * expr * expr e.g. Var "x" represents the source expression x Let "x" (Int 5) (Add (Var "x") (Int 1)) represents let x = 5 in x+1

Eval let rec eval e = if is_value e then e else eval (step e) let is_value = function Int _ -> true Add _ Var _ Let _ -> false

Step let rec step = function Int n -> failwith "Does not step" Add(Int n1, Int n2) -> Int (n1 + n2) Add(Int n1, e2) -> Add (Int n1, step e2) Add(e1, e2) -> Add (step e1, e2)

Step let rec step = function Int n -> failwith "Does not step" Add(Int n1, Int n2) -> Int (n1 + n2) Add(Int n1, e2) -> Add (Int n1, step e2) Add(e1, e2) -> Add (step e1, e2) Var _ -> failwith "Unbound variable" Why? Equivalent to just typing "x;;" into fresh utop session

Step let rec step = function Int n -> failwith "Does not step" Add(Int n1, Int n2) -> Int (n1 + n2) Add(Int n1, e2) -> Add (Int n1, step e2) Add(e1, e2) -> Add (step e1, e2) Var _ -> failwith "Unbound variable" Let(x, e1, e2) -> Let (x, step e1, e2)

Step let rec step = function Int n -> failwith "Does not step" Add(Int n1, Int n2) -> Int (n1 + n2) Add(Int n1, e2) -> Add (Int n1, step e2) Add(e1, e2) -> Add (step e1, e2) Var _ -> failwith "Unbound variable" Let(x, Int n, e2) -> e2{(int n)/x} Let(x, e1, e2) -> Let (x, step e1, e2)

Substitution (* [subst e v x] is e{v/x}, that is, * [e] with [v] substituted for [x]. *) let rec subst e v x = match e with Var y -> if x=y then v else e Int n -> Int n Add(el,er) -> Add(subst el v x, subst er v x) Let(y,ebind,ebody) -> let ebind' = subst ebind v x in if x=y then Let(y, ebind', ebody) else Let(y, ebind', subst ebody v x)

Step let rec step = function Int n -> failwith "Does not step" Add(Int n1, Int n2) -> Int (n1 + n2) Add(Int n1, e2) -> Add (Int n1, step e2) Add(e1, e2) -> Add (step e1, e2) Var _ -> failwith "Unbound variable" Let(x, Int n, e2) -> subst e2 (Int n) x Let(x, e1, e2) -> Let (x, step e1, e2)

Upcoming events [Mon] MS0 due [Wed] A3 due This is open to interpretation. THIS IS 3110