Presentation is loading. Please wait.

Presentation is loading. Please wait.

Semantics of PLs via Interpreters: Getting Started

Similar presentations


Presentation on theme: "Semantics of PLs via Interpreters: Getting Started"— Presentation transcript:

1 Semantics of PLs via Interpreters: Getting Started
CS7100: Programming Languages Prabhaker Mateti 1

2 Study … EOPL3 Chapter 3: 3.1, 3.2 source code: chapter3/let-lang/*
1722 data-structures.scm 4415 drscheme-init.scm 1450 environments.scm 2057 interp.scm 1501 lang.scm 1857 tests.scm 2007 top.scm CS7100 (PM) 2

3 EOPL3 Source Code All the code in the book, as a zip archive (211 Kb).
tested under PLT Scheme versions 372 and 4.2. It should run unchanged in Racket at the top of the file instead. (NOT #lang racket -- the syntax for require/provide is slightly different). CS7100 (PM) 3

4 Interpreter vs Compiler
Input: Abstract Syntax of a program (AST) Output:“Meaning” of the program An interpreter carries out the meaning of a program. A compiler transforms a program in one language into a program in a lower-level language preserving the meaning. CS7100 (PM) 4 4

5 Fig 3.2 The LET language Concrete/Abstract Syntax
Comments on the syntax Six kinds of expressions. Assume Scheme syntax for id and number. Minus is used as a function name. Uses std math notation for apply. then-body and else-body are single expressions. exp1 is the first expression and body is the second expression. Beware: minus is used as a function name, and uses std math notation for apply. CS7100 (PM) 5 5

6 Examples in the LET language
let x = 5 in -(x,3) in -(z, -(x,y)) % here x = 4 in -(-(x,8), y) CS7100 (PM) 6 6

7 scan&parse (scan&parse "-(55, -(x,11))") #(struct:a-program
#(struct:diff-exp #(struct:const-exp 55) #(struct:var-exp x) #(struct:const-exp 11)))) CS7100 (PM) 7 7

8 Values Of The Language Expressed values are the values of exp.
Denoted values are bound to variables. Not always the same, in general. In LET, they are the same: ExpVal = Int + Bool DenVal = Int + Bool CS7100 (PM) 8 8

9 Functions in our LET-Interpreter
constructors const-exp : Int →Exp zero?-exp : Exp → Exp if-exp : Exp × Exp × Exp → Exp diff-exp : Exp × Exp → Exp var-exp : Var → Exp let-exp : Var × Exp × Exp → Exp observer value-of : Exp × Env → ExpVal CS7100 (PM) 9 9

10 Spec of value-of (value-of (const-exp n) ρ) = (num-val n)
= (apply-env ρ var) (expval->num (value-of exp2 ρ)))) Three more remaining. Note the Scheme minus. CS7100 (PM) 10 10

11 Notation Notation Denotes  env [] Empty env [ var = val ] 
(extend-env var val ) «exp» AST for expression exp n (num-val n), val (expval->num val). Note: [[n]] =n CS7100 (PM) 11 11

12 Figure 3.3 An example calculation
CS7100 (PM) 12 12

13 Specifying Programs initial environment [i=1, v=5, x=10]
= (value-of exp [i=[1],v=[5], x= [10]]) CS7100 (PM) 13 13

14 Spec of value-of (value-of exp3 ρ)) [var = (value-of exp1 ρ)] ρ)
CS7100 (PM) 14 14

15 Fig 3.4 A conditional expression
CS7100 (PM) 15 15

16 Eval of an example let CS7100 (PM) 16 16

17 Eval of an example let (contd)
CS7100 (PM) 17 17

18 Fig 3.6 LET lang AST def (exp1 expression?)))
(define-datatype expression expression? (const-exp (num number?)) (diff-exp (exp1 expression?) (exp2 expression?)) (zero?-exp (exp1 expression?)) (if-exp (exp1 expression?) (exp2 expression?) (exp3 expression?)) (var-exp (var identifier?)) (let-exp (var identifier?) (body expression?))) CS7100 (PM) 18 18

19 (init-env) = [i= 1,v= 5,x=10] (define init-env (lambda () (extend-env
'i (num-val 1) 'v (num-val 5) 'x (num-val 10) (empty-env)))))) CS7100 (PM) 19 19

20 Fig 3.7a Expressed values (bool boolean?))) CS7100 (PM) 20 20

21 Fig 3.7b Expressed values ’num val))))) CS7100 (PM) 21 21

22 Fig 3.7c Expressed values (else (report-expval-extractor-error `bool val))))) CS7100 (PM) 22 22

23 value-of: Exp × Env → ExpVal
CS7100 (PM) 23 23

24 run, value-of-program CS7100 (PM) 24 24

25 (value-of (zero?-exp exp1) env)
CS7100 (PM) 25 25

26 (value-of (if-exp e1 e2 e3) env)
CS7100 (PM) 26 26

27 (value-of (let-exp …) env)
CS7100 (PM) 27 27


Download ppt "Semantics of PLs via Interpreters: Getting Started"

Similar presentations


Ads by Google