Interpreters Study Semantics of Programming Languages through interpreters
- Slides: 25
Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications) cs 784(Prasad) L 7 Interp 1
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. cs 784(Prasad) L 7 Interp 2
Simple Expression Language <program> : : = <expression>: : = <number> | <identifier> | <primitive> <operands> : : = () | (<expression> {, <expression>}*) <primitive> : : = E. g. , cs 784(Prasad) + | - | * | add 1 | sub 1 5 add 1(+(3, j)) L 7 Interp 3
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. cs 784(Prasad) L 7 Interp 4
Example (Petite Scheme) > (just-scan "add 1(+(1, 3))") ((literal-string 28 (number 1 1) (literal-string 28 (number 3 1) (literal-string 28 cs 784(Prasad) "add 1" 1) "(" 1) "+" 1) "(" 1) ", " 1) ")" 1)) L 7 Interp 5
Example > (scan&parse "add 1(+(1, 3))") (a-program (primapp-exp (incr-prim) ((primapp-exp (add-prim) ((lit-exp 1) (lit-exp 3)))))) > (eval-program (scan&parse "add 1(+(1, 3))")) 5 cs 784(Prasad) L 7 Interp 6
The Abstract Syntax (define-datatype program? (a-program (exp expression? ))) (define-datatype expression? (lit-exp (datum number? )) (var-exp (id symbol? )) (primapp-exp (primitive? ) (rand (list-of expression? ))) ) (define-datatype primitive? (add-prim) (subtract-prim) (mult-prim) (incr-prim) (decr-prim)) cs 784(Prasad) L 7 Interp 7
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) (var-exp (id) (apply-env id) ) (primapp-exp (prim rands) (let ((args (eval-rands env))) (apply-primitive prim args)) ) ))) cs 784(Prasad) L 7 Interp 8
(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))) cs 784(Prasad) L 7 Interp 9
(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. . . cs 784(Prasad) L 7 Interp 10
Scanner Specification (define the-lexical-spec '((whitespace) (comment ("%" (arbno (not #newline))) skip) (identifier (letter (number (arbno (or letter digit "_" "-" "? "))) (digit (arbno digit)) symbol) number)) ) cs 784(Prasad) L 7 Interp 11
Parser Specification (define the-grammar '((program (expression) a-program) (expression (number) lit-exp) (expression (identifier) var-exp) (expression (primitive "(" (separated-list expression ", ") ")") primapp-exp) (primitive (primitive ("+") ("-") ("*") ("add 1") ("sub 1") add-prim) subtract-prim) mult-prim) incr-prim) decr-prim)) ) cs 784(Prasad) L 7 Interp 12
Example (Dr. Scheme) > (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 cs 784(Prasad) x = 10 in init-env. L 7 Interp 13
Adding conditional • Concrete 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)) ) cs 784(Prasad) L 7 Interp 14
• 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. cs 784(Prasad) L 7 Interp 15
Scanner Details > (just-scan "if while Abc Def + # pqr") ((literal-string 45 "if" 1) (identifier while 1) (literal-string 45 "-" 1) (identifier Abc 2) (identifier Def 3) (literal-string 45 "+" 4)) cs 784(Prasad) L 7 Interp 16
Local Bindings : Issues let x = 5 in let y = 6 + x in x + y; x=5 y=11 • Sub-expressions may be evaluated in different contexts/environments. let x = 5 in let x = 6 + x in x + x; x=5 x = 11 • Inner x shadows outer x in nested let-body. cs 784(Prasad) L 7 Interp 17
x=5 let x = 5 in let x = 6 + x in x + x x=5 x = 11 * x; x=5 • Introducing let requires passing relevant environment to the evaluator. • Inner binding overrides the outer one in case of conflict. (Example Expression Value: 110) 110 cs 784(Prasad) L 7 Interp 18
Adding let • Concrete Syntax <expression> : : = let { <identifier> = <expression> } * in <expression> • Abstract Syntax let-exp cs 784(Prasad) (ids rands L 7 Interp body) 19
Introducing if and let expressions (define-datatype expression? . . . (if-exp (test-exp expression? ) (true-exp expression? ) (false-exp expression? )) (let-exp (ids (list-of symbol? )) (rands (list-of expression? )) (body expression? ) ) ) cs 784(Prasad) L 7 Interp 20
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 env))) (eval-expression body (extend-env ids args env)) ) ) (else (eopl: error 'eval-expression "Not here: ~s" exp)) ))) cs 784(Prasad) L 7 Interp 21
Recapitulation Variable-free Arithmetic Expressions syntax (program) Evaluator semantics structure (Integers, +, *, …) (Scheme) Semantics (meaning) Integer CALCULATOR cs 784(Prasad) L 7 Interp 22
Arithmetic Expressions syntax (program) Evaluator semantic structure (Scheme) Semantics (meaning) Environment; (Integers, +, *, …) Integer CALCULATOR WITH MEMORY cs 784(Prasad) L 7 Interp 23
Arithmetic Expressions; Procedure Definitions and Calls syntax (program) Evaluator semantic structure (Scheme) Semantics (meaning) Environment; (Integers, +, *, …); Addl. Scheme Support Integer; Procedures PROGRAMMABLE CALCULATOR cs 784(Prasad) L 7 Interp 24
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. cs 784(Prasad) L 7 Interp 25
- Compare procedural semantics and declarative semantics.
- Real-time systems and programming languages
- Cs 421 programming languages and compilers
- Multithreading program in java
- Cxc it
- Introduction to programming languages
- Plc programming languages
- Procedural programming languages
- Comparative programming languages
- Alternative programming languages
- Strongly typed vs weakly typed
- Transmission programming languages
- Adam doupe cse 340
- Integral data type
- Xenia programming languages
- Advantages of application software
- Mainstream programming languages
- Vineeth kashyap
- Programming languages
- Programming languages
- Programming languages
- Programming languages
- Tiny programming language
- Brief history of programming languages
- Taxonomy of programming languages
- Real-time systems and programming languages