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

Slides:



Advertisements
Similar presentations
Plt /7/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.2 An Abstraction for Inductive Data Types.
Advertisements

Semantics of PLs via Interpreters: Getting Started CS784: Programming Languages Prabhaker Mateti.
Assignments and Procs w/Params EOPL3 Chapter 4. Expressible vs. Denotable values Expressible Values –the language can express and compute these –represented.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Cs7100(Prasad)L8Interp1 Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications)
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.
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
Environments and Evaluation
SICP Interpretation part 1 Parts of an interpreter Arithmetic calculator Names Conditionals and if Store procedures in the environment.
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
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.
Winter 2003/4Pls – syntax – Catriel Beeri1 SYNTAX Syntax: form, structure The syntax of a pl: The set of its well-formed programs The rules that define.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
EOPL3: Section 3.3 PROC and App B: SLLGEN
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
Lexical Analysis - An Introduction. The Front End The purpose of the front end is to deal with the input language Perform a membership test: code  source.
CS 461 – Oct. 7 Applications of CFLs: Compiling Scanning vs. parsing Expression grammars –Associativity –Precedence Programming language (handout)
Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
Plt /19/ Environment-Passing Interpreters Programming Language Essentials 2nd edition Chapter 3.1 A Simple Interpreter.
CPS 506 Comparative Programming Languages Syntax Specification.
CSE 413 Languages & Implementation Hal Perkins Autumn 2012 Structs, Implementing Languages (credits: Dan Grossman, CSE 341) 1.
Operational Semantics of Scheme
Abstract Syntax cs7100 (Prasad) L7AST.
Chapter 3 – Describing Syntax
A Simple Syntax-Directed Translator
CS510 Compiler Lecture 4.
6.001 SICP Variations on a Scheme
The interpreter.
Introduction to Scheme
Scheme : variant of LISP
Corky Cartwright January 18, 2017
Implementing Recursion
Env. Model Implementation
Original material by Eric Grimson
Semantics of PLs via Interpreters: Getting Started
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
The Metacircular Evaluator
Abstract Syntax Prabhaker Mateti 1.
CSC 4181Compiler Construction Context-Free Grammars
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
CS 3304 Comparative Languages
Lecture 4: Lexical Analysis & Chomsky Hierarchy
Procedures App B: SLLGEN 1.
CS 3304 Comparative Languages
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
Streams, Delayed Evaluation and a Normal Order Interpreter
Abstract Syntax cs7100 (Prasad) L7AST.
2.2.2 Abstract Syntax Recall BNF definition of l-calculus expressions:
3.6 Interpreter: Recursion
6.001 SICP Variations on a Scheme
CSC 4181 Compiler Construction Context-Free Grammars
2.2.2 Abstract Syntax Recall BNF definition of l-calculus expressions:
Chapter 3: Environment-Passing Interpreters
6.001 SICP Interpretation Parts of an interpreter
High-Level Programming Language
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
Presentation transcript:

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

Interpreters Input: Output: Interpreter vs Compiler 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) L8Interp

Simple Expression Language <program> ::= <expression> <expression>::= <number> | <identifier> | <primitive> <operands> <operands> ::= () | (<expression> {,<expression>}*) <primitive> ::= + | - | * | add1 | sub1 E.g., 5 add1(+(3,j)) cs7100(Prasad) L8Interp

Informal Semanics Number Symbolic names Application expression 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) L8Interp

Example (Petite Scheme) > (just-scan "add1(+(1,3))") ((literal-string28 "add1" 1) (literal-string28 "(" 1) (literal-string28 "+" 1) (number 1 1) (literal-string28 "," 1) (number 3 1) (literal-string28 ")" 1) (literal-string28 ")" 1)) Token stream due to scanning > (just-scan "add1(+(1,3))") ((literal-string45 "add1" 1) (literal-string45 "(" 1) (literal-string45 "+" 1) (literal-string45 "(" 1) (number 1 1) (literal-string45 "," 1) (number 3 1) (literal-string45 ")" 1) (literal-string45 ")" 1)) EOPL-specifics : keywords are parsed as strings while identifiers are turned into symbols; unexpected letters ignored; third arg line number ========= > (just-scan "if while") ((literal-string57 "if" 1) (identifier while 1)) > (just-scan ", + ? $") ((literal-string57 "," 1) (literal-string57 "+" 1)) > (just-scan " ! @ # ") () > (just-scan "-----_____") ((literal-string57 "-" 1) (literal-string57 "-" 1) (literal-string57 "-" 1) (literal-string57 "-" 1) (literal-string57 "-" 1)) > (just-scan "abc def ghi") ((identifier abc 1) (identifier def 2) (identifier ghi 3)) cs7100(Prasad) L8Interp

Example > (scan&parse "add1(+(1,3))") > (eval-program (a-program (primapp-exp (incr-prim) ((primapp-exp (add-prim) ((lit-exp 1) (lit-exp 3)))))) > (eval-program (scan&parse "add1(+(1,3))")) 5 AST and final evaluation result: View the AST as a labeled root with children, which are either a labeled tree or a list. > (scan&parse "add1(+(1,3))") #(struct:a-program #(struct:primapp-exp #(struct:incr-prim) (#(struct:primapp-exp #(struct:add-prim) (#(struct:lit-exp 1) #(struct:lit-exp 3)))))) List red parenthesis cs7100(Prasad) L8Interp

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)) Variant record: expression Characteristic function: expression? Tags: lit-exp. var-exp, prim-exp Typed Fields: datum number?, id symbol?, … Eventually generated automatically from the grammar. cs7100(Prasad) L8Interp

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)) ) ))) Fixed global environment: R-values, Call by value cs7100(Prasad) L8Interp

(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))) (map (lambda (x) (eval-expression x env)) All operands evaluated in the same environment. cs7100(Prasad) L8Interp

. . . Code for environment manipulation . . . (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 . . . (zero-test-prim () (if (zero? (car args)) 1 0)) ---- needed to test for 0 init-env contains arbitrary bindings for variables Consider the consequence of the “apply-primitive” definition on: Number of arguments allowed. (2) Type of arguments allowed. (3) Robustness issues: errors and exceptions … (4) Flexible use with overloaded meaning of operators cs7100(Prasad) L8Interp

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)) ) Token type - lexeme regular expression - action Whitespace includes newline. cs7100(Prasad) L8Interp

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)) ) Lhs nonterminal - RHS seq of terminals and nonterminals – abstract syntax automatically generated The scanner returns language keywords given in the grammar as strings and other identifiers as symbols. (primitive "(“expression ( arbno ( ",“ expression ) ) ")") cs7100(Prasad) L8Interp

Example (Dr. Racket) #(struct:a-program > (scan&parse "-(v,x)") #(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. AST and final evaluation result cs7100(Prasad) L8Interp

Adding conditional Concrete Syntax Abstract Syntax <expression> ::= if <expression> then <expression> else <expression> Abstract Syntax if-exp (test-exp true-exp false-exp) Addl. Semantic Function (define (true-value? x) (not (zero? x)) ) Recall that conditional expression is a special form and needs to be supported by the interpreter as opposed to simulatable via procedure call. cs7100(Prasad) L8Interp

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

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)) Token stream due to scanning > (just-scan "if while - # abc def") ((literal-string57 "if" 1) (identifier while 1) (literal-string57 "-" 1)) > (just-scan "if while - Abc Def + # pqr") ((literal-string57 "if" 1) (identifier while 1) (literal-string57 "-" 1) (identifier |Abc| 2) (identifier |Def| 3) (literal-string57 "+" 4)) + pqr") ((literal-string45 "if" 1) (literal-string45 "-" 1) (identifier Abc 2) (identifier Def 3) (literal-string45 "+" 4) (identifier pqr 4)) > cs7100(Prasad) L8Interp

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

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

Adding let Concrete Syntax Abstract Syntax <expression> ::= let { <identifier> = <expression> } * in <expression> Abstract Syntax let-exp (ids rands body) cs7100(Prasad) L8Interp

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

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)) ))) Semantic specification: let variable initialization expressions evaluated in the Same surrounding environment --- other choices “let*”-interpretation possible. cs7100(Prasad) L8Interp

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

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

Arithmetic Expressions; Procedure Definitions and Calls 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) L8Interp

Polynomial Calculators To specify/design a programmable polynomial calculator, the object language must contain syntax for creating and manipulating polynomials, and 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. cs7100(Prasad) L8Interp