Cs7100(Prasad)L8Interp1 Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications)

Slides:



Advertisements
Similar presentations
Semantics of PLs via Interpreters: Getting Started CS784: Programming Languages Prabhaker Mateti.
Advertisements

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.
3.6 Interpreter: Recursion Recall Scheme's letrec (recursive let ): > (letrec ((fact (lambda (x) (if (zero? x) 1 (* x (fact (- x 1))))) (fact 6)) 720 Question:
Metacircular Evaluation SICP Chapter 4 Mark Boady.
Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many.
1 The Metacircular Evaluator Chapter 4 Section 4.1 We will not cover every little detail in the lectures, so you MUST read Section 4.1 in full.
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).
SICP Variations on a Scheme Scheme Evaluator – A Grand Tour Techniques for language design: Interpretation: eval/apply Semantics vs. syntax Syntactic.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
The environment model evaluator and compiler 1 The env model evaluator Motivation In one word: Efficiency Saves repeated renaming and substitution: Using.
EOPL3: Section 3.3 PROC and App B: SLLGEN
Environment: Data type associating symbols with values { a: 4s:'foof: x.x } Interface: (empty-env) = [ Ø ] (apply-env [f] s ) = f(s) (extend-env '( s 1...
Cs784(tk)1 Implementing Recursion. Recap: Goals Experimenting with PL design alternatives Scoping, parameter passing, arrays,... Runnable prototype of.
PrasadL145OOL1 Managing Environments An Exercise in the Design, Analysis, Specification, and Implementation of the Core of an OOP Language. Object-Oriented.
Cs7100(Prasad)L8Proc1 Procedures. cs7100(Prasad)L8Proc2 Primitive procedures  etc User-defined procedures –Naming a sequence of operations.
Plt /12/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.3 Representation Strategies for Data Types.
3.5 Procedures Recall procedures (functions) in Scheme: (let ((f (lambda(y z) (+ y (- z 5))) (f 2 28)) We would like something similar in our toy language:
4.2 Type Checking (type-of-expression > tenv ) = bool (type-of-expression > tenv ) = x ( type-of-expression if > tenv ) = x Recall type of if statement:
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
1 Objects and types Typed languages = define a set of types in the language and assign a type to each expression in the program Type checking = how can.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
Plt /17/ Environment-Passing Interpreters Programming Language Essentials 2nd edition Chapter 3.8 Parameter-Passing Variations.
Plt /19/ Environment-Passing Interpreters Programming Language Essentials 2nd edition Chapter 3.1 A Simple Interpreter.
CSE 413 Languages & Implementation Hal Perkins Autumn 2012 Structs, Implementing Languages (credits: Dan Grossman, CSE 341) 1.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
Compiler (javac) vs. Interpreter (drscheme): Chapter 3: Environment-Passing Interpreters Front End Interpreter program textsyntax tree answer > ((lambda.
Inductively Defined Data Concrete and Abstract syntax Karl Lieberherr.
3.3 Conditional Evaluation --> if 1 then 2 else 3 2 Want to support if/then/else We'll represent false as 0, true as 1: Syntax (BNF): ::= if then else.
Objects and Classes Gul Agha CS 421 Spring /11/2001CS 322 Fall Characteristics of OOP Object-based  encapsulate state  provide interface.
Cs784 (Prasad)L6AST1 Abstract Syntax. cs784 (Prasad)L6AST2 Language of -expressions ::= | (lambda ( ) ) | ( ) E.g., concrete syntax Scheme S-expressions.
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Winter 2013.
3.2 The Front End > (a-program ; add1(+(3,x)) (primapp-exp (incr-prim) ((primap-exp (add-prim) ((lit-exp 3) (var-exp x)))))) Want to avoid having to specify.
Operational Semantics of Scheme
Abstract Syntax cs7100 (Prasad) L7AST.
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2017.
The interpreter.
Introduction to Scheme
Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications) cs7100(Prasad) L8Interp.
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Implementing Recursion
Env. Model Implementation
Original material by Eric Grimson
Assignment 3 Solution Background
Semantics of PLs via Interpreters: Getting Started
Mini Language Interpreter Programming Languages (CS 550)
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Autumn 2018.
The Metacircular Evaluator
Abstract Syntax Prabhaker Mateti 1.
The Metacircular Evaluator
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Zach Tatlock Winter 2018.
Procedures App B: SLLGEN 1.
3.4 Local Binding Recall Scheme's let: > (let ((x 5)‏ (y 6))
The Metacircular Evaluator (Continued)
6.001 SICP Further Variations on a Scheme
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2016.
Abstract Syntax cs7100 (Prasad) L7AST.
3.6 Interpreter: Recursion
6.001 SICP Variations on a Scheme
Chapter 3: Environment-Passing Interpreters
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Autumn 2017.
6.001 SICP Interpretation Parts of an interpreter
Assignments and Procs w/Params
topics interpreters meta-linguistic abstraction eval and apply
Recursive Procedures and Scopes
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2019.
Presentation transcript:

cs7100(Prasad)L8Interp1 Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications)

cs7100(Prasad)L8Interp2 Interpreters Input: –Representation of a program (AST) Output: –“Meaning” of the program ; Interpreter vs Compiler Interpreter carries out the meaning of a program, while a compiler transforms a program in one language into a program in a lower-level language preserving the meaning.

cs7100(Prasad)L8Interp3 Simple Expression Language ::= | ::= () | ( {, }*) ::= + | - | * | add1 | sub1 E.g., 5 add1(+(3,j))

cs7100(Prasad)L8Interp4 Informal Semanics Number same as what Scheme associates with numerals. (internal to the entity) Symbolic names value bound to it in the environment (external to the entity) Application expression recursively evaluate operator and operands. primitive operators interpreted by Scheme.

cs7100(Prasad)L8Interp5 Example (Petite Scheme) > (just-scan "add1(+(1,3))") ((literal-string28 "add1" 1) (literal-string28 "(" 1) (literal-string28 "+" 1) (literal-string28 "(" 1) (number 1 1) (literal-string28 "," 1) (number 3 1) (literal-string28 ")" 1) (literal-string28 ")" 1))

cs7100(Prasad)L8Interp6 Example > (scan&parse "add1(+(1,3))") (a-program (primapp-exp (incr-prim) ((primapp-exp (add-prim) ((lit-exp 1) (lit-exp 3)))))) > (eval-program (scan&parse "add1(+(1,3))")) 5

cs7100(Prasad)L8Interp7 The Abstract Syntax (define-datatype program program? (a-program (exp expression?))) (define-datatype expression expression? (lit-exp (datum number?)) (var-exp (id symbol?)) (primapp-exp (prim primitive?) (rand (list-of expression?))) ) (define-datatype primitive primitive? (add-prim) (subtract-prim) (mult-prim) (incr-prim) (decr-prim))

cs7100(Prasad)L8Interp8 The evaluator (define eval-program (lambda (pgm) (cases program pgm (a-program (body) (eval-expression body (init-env)))))) (define eval-expression (lambda (exp env) (cases expression exp (lit-exp (datum) datum) (var-exp (id) (apply-env env id) ) (primapp-exp (prim rands) (let ((args (eval-rands rands env))) (apply-primitive prim args)) ) )))

cs7100(Prasad)L8Interp9 (cont’d) (define eval-rands (lambda (rands env) (map (lambda (x)(eval-rand x env)) rands))) (define eval-rand (lambda (rand env) (eval-expression rand env))) (define eval-rands (lambda (rands env) (map (lambda (x) (eval-expression x env)) rands)))

cs7100(Prasad)L8Interp10 (cont’d) (define apply-primitive (lambda (prim args) (cases primitive prim (add-prim () (+ (car args) (cadr args)) ) (subtract-prim () (- (car args) (cadr args)) ) (mult-prim () (* (car args) (cadr args)) ) (incr-prim () (+ (car args) 1) ) (decr-prim () (- (car args) 1) ) ))) (define init-env (lambda () (extend-env '(i v x) '(1 5 10) (empty-env))))... Code for environment manipulation...

cs7100(Prasad)L8Interp11 Scanner Specification (define the-lexical-spec '((whitespace (whitespace) skip) (comment ("%" (arbno (not #\newline))) skip) (identifier (letter (arbno (or letter digit "_" "-" "?"))) symbol) (integer (digit (arbno digit)) number)) )

cs7100(Prasad)L8Interp12 Parser Specification (define the-grammar '((program (expression) a-program) (expression (integer) lit-exp) (expression (identifier) var-exp) (expression (primitive "(" (separated-list expression ",") ")") primapp-exp) (primitive ("+") add-prim) (primitive ("-") subtract-prim) (primitive ("*") mult-prim) (primitive ("add1") incr-prim) (primitive ("sub1") decr-prim)) )

cs7100(Prasad)L8Interp13 Example (Dr. Racket) > (scan&parse "-(v,x)") #(struct:a-program #(struct:primapp-exp #(struct:subtract-prim) ( #(struct:var-exp v) #(struct:var-exp x) ) ) ) > (eval-program (scan&parse "-(v,x)")) -5 Recall that v = 5 and x = 10 in init-env.

cs7100(Prasad)L8Interp14 Adding conditional Concrete Syntax ::= if then else Abstract Syntax if-exp (test-exp true-exp false-exp) Addl. Semantic Function (define (true-value? x) (not (zero? x)) )

cs7100(Prasad)L8Interp15 Addl. Interpreter Clause (if-exp (test-exp true-exp false-exp) ( (if (true-value? (eval-expression test-exp env)) (eval-expression true-exp env) ) (eval-expression false-exp env)) ) Defined language vs Defining language Inductively defined data structure naturally leads to recursively defined function.

cs7100(Prasad)L8Interp16 Scanner Details > (just-scan " if while - Abc Def + # pqr") ((literal-string45 "if" 1) (identifier while 1) (literal-string45 "-" 1) (identifier Abc 2) (identifier Def 3) (literal-string45 "+" 4))

cs7100(Prasad)L8Interp17 let x = 5 in let y = 6 + x in x + y; Sub-expressions may be evaluated in different contexts/environments. let x = 5 in let x = 6 + x in x + x; Inner x shadows outer x in nested let -body. x = 11 x = 5 y=11 x = 5 Local Bindings : Issues

cs7100(Prasad)L8Interp18 let x = 5 x in let x = 6 + x xx in x + x * x; Introducing let requires passing relevant environment to the evaluator. 110Inner binding overrides the outer one in case of conflict. (Example Expression Value: 110) x = 5 x = 11 x = 5

cs7100(Prasad)L8Interp19 Adding let Concrete Syntax ::= let { = } * in Abstract Syntax let-exp (ids rands body)

cs7100(Prasad)L8Interp20 Introducing if and let expressions ( define-datatype expression expression?... (if-exp (test-exp expression?) (true-exp expression?) (false-exp expression?)) (let-exp (ids (list-of symbol?)) (rands (list-of expression?)) (body expression?) ) )

cs7100(Prasad)L8Interp21 Introducing if and let into the evaluator (define eval-expression (lambda (exp env) (cases expression exp... (if-exp (test-exp true-exp false-exp) (if (true-value? (eval-expression test-exp env)) (eval-expression true-exp env) (eval-expression false-exp env)) ) (let-exp (ids rands body) (let ((args (eval-rands rands env))) ( eval-expression body (extend-env ids args env)) ) ) (else (eopl:error 'eval-expression "Not here:~s" exp)) )))

cs7100(Prasad)L8Interp22 Recapitulation Evaluator Variable-free Arithmetic Expressions (Integers, +, *,…) Integer syntax (program) semantics structure (Scheme) Semantics (meaning) CALCULATOR

cs7100(Prasad)L8Interp23 Evaluator Arithmetic Expressions Environment; (Integers, +, *,…) Integer syntax (program) semantic structure (Scheme) Semantics (meaning) CALCULATOR WITH MEMORY

cs7100(Prasad)L8Interp24 Evaluator Arithmetic Expressions; Procedure Definitions and Calls Environment; (Integers, +, *,…); Addl. Scheme Support Integer; Procedures syntax (program) semantic structure (Scheme) Semantics (meaning) PROGRAMMABLE CALCULATOR

cs7100(Prasad)L8Interp25 Polynomial Calculators 9 To specify/design a programmable polynomial calculator, the object language must contain syntax for creating and manipulating polynomials, and 9 the meta-language (Scheme) must provide suitable semantic structure to map variables to polynomials (environment). to interpret operations on polynomials (using corresponding Scheme code). –Meta-language support is analogous to hardware support. ; The semantics of the ADT Polynomials can be specified through algebraic techniques.