Prof. Fateman CS 164 Lecture 181 Language definition by interpreter Lecture 18.

Slides:



Advertisements
Similar presentations
Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
Advertisements

C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Prof Fateman CS 164 Lecture 371 Review: Programming Languages and Compilers CS AM MWF 10 Evans.
Metacircular Evaluation SICP Chapter 4 Mark Boady.
Prof. Fateman CS 164 Lecture 261 Functional Tiger, Calling variations Lecture 26.
1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation.
Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu.
Prof. Fateman CS 164 Lecture 151 Language definition by interpreter, translator, continued Lecture 15.
Prof. Fateman CS164 Lecture 241 Other Control Flow ideas: Throw, Catch, Continuations and Call/CC Lecture 24.
CSE 341 Lecture 19 parsing / Homework 7 slides created by Marty Stepp
Prof. Fateman CS 164 Lecture 141 Language definition by interpreter Lecture 14.
Reference Book: Modern Compiler Design by Grune, Bal, Jacobs and Langendoen Wiley 2000.
Prof. Fateman CS 164 Lecture 131 Types Lecture 13.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Introduction and Syntax. Course objectives Discuss features of programming languages. Discuss how the features are implemented in a simple computer architecture.
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
Prof. Fateman CS 164 Lecture 161 Properly Tail Recursive Interpreter. Some timings of compilation vs interpretation Lecture 16.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
INF5110: Mandatory Exercise 2 Eyvind W. Axelsen @eyvindwa Slides are partly based on.
Chapter 1 Algorithm Analysis
High-Level Programming Languages: C++
COP4020 Programming Languages
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CMSC 330: Organization of Programming Languages
Prof. Fateman CS 164 Lecture 111 Syntax  Simple Semantics Lecture 11.
Allegro CL Certification Program Lisp Programming Series Level I Session Basic Lisp Development in the IDE.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
Programming Languages
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
CS5205Semantics1 CS5205: Foundation in Programming Languages Semantics Static Semantics Dynamic Semantics Operational Semantics Big-step Small-Step Denotational.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
Programming Languages Concepts Chapter 1: Programming Languages Concepts Lecture # 4.
Introduction to Computer Programming Concepts M. Uyguroğlu R. Uyguroğlu.
Software Engineering Algorithms, Compilers, & Lifecycle.
Language-Based Security: Overview of Types Deepak Garg Foundations of Security and Privacy October 27, 2009.
Functional Programming
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2017.
System Software Unit-1 (Language Processors) A TOY Compiler
PROGRAMMING LANGUAGES
Overview of Compilation The Compiler Front End
Overview of Compilation The Compiler Front End
CS 363 – Chapter 1 What is a programming language? Kinds of languages
Syntax Simple Semantics
Basic Program Analysis: AST
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
The Metacircular Evaluator
The Metacircular Evaluator
CMP 131 Introduction to Computer Programming
The Metacircular Evaluator (Continued)
6.001 SICP Variations on a Scheme
Introduction to Intermediate Code, virtual machine implementation
6.001 SICP Interpretation Parts of an interpreter
Functional MiniJava, Calling variations
Programming Languages
Lisp.
Programming language translators
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2019.
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

Prof. Fateman CS 164 Lecture 181 Language definition by interpreter Lecture 18

Prof. Fateman CS 164 Lecture 182 Routes to defining a language Formal mathematics –Context free grammar –Mathematical semantics (axioms, theorems, proofs) –Rarely used (the downfall of Algol 68) –Theoretical interest Informal textual –CFG + natural language (Algol 60) –Just natural language (Tiger?) –Almost universally used Operational –Here’s a program that does the job –Metacircular evaluator for Scheme, Lisp –Evaluator/ interpreter for Tiger

Prof. Fateman CS 164 Lecture 183 input Typical compiler structure Source program AST Intermediate form output Lex, parse Typecheck, cleanup Assembly lang Object code Machine

Prof. Fateman CS 164 Lecture 184 input “Tigrun” structure Source program AST Intermediate form output interpreter Lex, parse Typecheck, cleanup What language is interpreter written in? What machine does it run on?

Prof. Fateman CS 164 Lecture 185 Interpreter structure: advantages interpreter Interpreter in a higher level language: source language derives semantics from interpreter code and the semantics of the language of the interpreter (e.g. Lisp). What does EACH STATEMENT MEAN? Exhaustive case analysis What are the boundaries of legal semantics? What exactly is the model of scope, etc..

Prof. Fateman CS 164 Lecture 186 Interpreter structure: more advantages interpreter Prototyping / debugging easier Portable intermediate form (Byte code ) intermediate form may be compact Security may be more easily enforced

Prof. Fateman CS 164 Lecture 187 Interpreter structure: disadvantages interpreter Typically unable to reach full machine speed Difficult to transcend the limits of the underlying language implementation (not full access to machine) Code depends on presence of infrastructure (all of Lisp??) (if meta-circular) bootstrapping…

Prof. Fateman CS 164 Lecture 188 An interpreter compromise (e.g. Java VM) interpreter “Compile” to a hypothetical byte-code stack machine appropriate for Java (etc), easily simulated on most real machines Implement this virtual byte-code stack machine on all real machines When speed is an issue, try Just In Time compiling; convert sections of code to machine language for a specific machine.

Prof. Fateman CS 164 Lecture 189 Interpreter to Compiler is a small step Modest modification of an interpreter can become a compiler. For example: Interpreter: To evaluate a sequence {s 1, s 2 }, evaluate s 1 then evaluate s 2, returning the last of these. Compiler: To compile a sequence {s 1, s 2 }, compile s 1 then compile s 2, returning the concatenation of the code for s 1 and the code for s 2. Interpreter: To evaluate a sum (+ A B) evaluate A, evaluate B and add. Compiler: To compile a sum, (+ A B) compile A, compile B, concatenate results, compile + to “add the results of the two previous sections of code”. Program structure is a walk over the intermediate code.

Prof. Fateman CS 164 Lecture 1810 AST for merge.tig ;;; -*- Mode: Lisp; Syntax: Common-Lisp; package: tiger -*- (in-package :tiger) (defparameter merge-ast '(LetExp (1. 3) (DecList (TypeDec (3. 9) any (FieldList (FieldDescriptor (3. 16) any int))) (VarDec (4. 4) buffer nil (CallExp (4. 22) (getchar (4. 22)) (ExpList))) (FunctionDec (6. 16) readint (ExpList (IdType (6. 20) any any)) (int (6. 32)) (LetExp (7. 4) (DecList (VarDec (7. 8) i nil (IntExp (7. 15) 0)) (FunctionDec (8. 21) isdigit (ExpList (IdType (8. 23) s string)) ;Explist or FieldList? (int (8. 39)) (IfExp (9. 12) (OpExp (9. 12) (>= (9. 12)) (CallExp (9. 7) (ord (9. 7)) (ExpList (SimpleVar (9. 9) s))) (CallExp (9. 15) (ord (9. 15)) (ExpList (StringExp (9. 19) "0")))) (OpExp (9. 31) (<= (9. 31)) (CallExp (9. 26) (ord (9. 26)) (ExpList (SimpleVar (9. 28) s))); which line/col#? (CallExp (9. 34) (ord (9. 34)) (ExpList (StringExp (9. 38) "9")))) (IntExp (9. 12) 0)))…….

Prof. Fateman CS 164 Lecture 1811 Intermediate form (cleanup) for merge.ast ;;; -*- Mode: Lisp; Syntax: Common-Lisp; package: tiger -*- (in-package :tiger) (defparameter merge-cleaned ;; the line numbers removed; other excess fluff removed. '(LetExp (DecList (TypeDec any (FieldList (FieldDescriptor any int))) (VarDec buffer nil (getchar)) (FunctionDec readint (ExpList (IdType any any)) (int);; ? (int) or int? (LetExp (DecList (VarDec i nil 0) (FunctionDec isdigit (ExpList (IdType s string)) (int) (IfExp (>= (ord s) (ord "0")) (<= (ord s) (ord "9")) 0)) (FunctionDec skipto (ExpList) nil (WhileExp (IfExp (= buffer " ") 1 (= buffer " ")) (AssignExp buffer (getchar))))) (ExpList (skipto) (AssignExp (FieldVar any any) (isdigit buffer)) (WhileExp (isdigit buffer) (ExpList (AssignExp i (- (+ (* i 10) (ord buffer)) (ord "0"))) (AssignExp buffer (getchar))))

Prof. Fateman CS 164 Lecture 1812 Start of the interpreter (defun tig-interp (x env); x= intermediate code, env=environment of bindings ;; first deal with interp of trivial or error cases which might occur. (cond ((null x) nil) ((eq x 'VOID) 'VOID) ;;or (error "attempt to evaluate VOID") ((symbolp x) (get-var x env)) ((atom x) x);; integers and strings are atoms. (t (tig-interp-list x env)))) ;; the real business is in lisp lists

Prof. Fateman CS 164 Lecture 1813 Start of the interpreter: tig-interp-list (defun tig-interp-list (x env); environment of bindings (case (first x) (ExpList;; a sequence of expressions. (last1 (mapcar #'(lambda(r)(tig-interp r env)) (cdr x)))) (AssignExp (set-var (elt x 1)(elt x 2) env)) (IfExp (tig-interp-if (elt x 1)(elt x 2) (elt x 3) env)) ;; see next slide (LetExp (tig-interp-let (elt x 1)(elt x 2) env)) (WhileExp (tig-interp-while (elt x 1)(elt x 2) env)) (ArrayExp (make-array (tig-interp (elt x 2) env); size :initial-element (tig-interp (elt x 3) env))) (RecordExp (tig-interp-rec x env)) (FieldVar (cadr (assoc (elt x 2) (tig-interp (elt x 1) env)))) (BreakExp (throw 'tig-break nil)) (SubscriptVar (aref (get-var (elt x 1) env); get the array (tig-interp (elt x 2)env))) (ForExp (tig-interp-for (elt x 1)(elt x 2) (elt x 3)(elt x 4) env)) (t;; a CallExp ;; get the procedure ;; evaluate the arguments (tig-call (get-fun (car x) env) (mapcar #'(lambda(p) (tig-interp p env)) (cdr x)) env ))))))

Prof. Fateman CS 164 Lecture 1814 Next time: all the functions + cast of supporting operations All the functions All the supporting data structures