Interpreters - PowerPoint PPT Presentation

About This Presentation
Title:

Interpreters

Description:

Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications) cs784(Prasad) L7Interp * * Token type - lexeme regular ... – PowerPoint PPT presentation

Number of Views:66
Avg rating:3.0/5.0
Slides: 26
Provided by: wrightEdu
Learn more at: http://cecs.wright.edu
Category:

less

Transcript and Presenter's Notes

Title: Interpreters


1
Interpreters
  • Study Semantics of Programming Languages through
    interpreters
  • (Executable Specifications)

2
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.

3
Simple Expression Language
  • ltprogramgt ltexpressiongt
  • ltexpressiongt ltnumbergt
  • ltidentifiergt
  • ltprimitivegt ltoperandsgt
  • ltoperandsgt ()
  • (ltexpressiongt
  • ,ltexpressiongt)
  • ltprimitivegt - add1 sub1
  • E.g., 5
  • add1((3,j))

4
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.

5
Example (Petite Scheme)
  • gt (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))

6
Example
  • gt (scanparse "add1((1,3))")
  • (a-program
  • (primapp-exp
  • (incr-prim)
  • ((primapp-exp (add-prim)
  • ((lit-exp 1)
  • (lit-exp 3))))))
  • gt (eval-program
  • (scanparse "add1((1,3))"))
  • 5

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

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

9
(contd)
  • (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)))

10
(contd)
  • (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 . . .

11
Scanner Specification
  • (define the-lexical-spec
  • '((whitespace (whitespace) skip)
  • (comment ("" (arbno (not \newline)))
    skip)
  • (identifier
  • (letter (arbno (or letter digit "_" "-"
    "?"))) symbol)
  • (number (digit (arbno digit)) number))
  • )

12
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 ("") add-prim)
  • (primitive ("-") subtract-prim)
  • (primitive ("") mult-prim)
  • (primitive ("add1") incr-prim)
  • (primitive ("sub1") decr-prim))
  • )

13
Example (Dr. Scheme)
  • gt (scanparse "-(v,x)")
  • (structa-program
  • (structprimapp-exp
  • (structsubtract-prim)
  • ( (structvar-exp v)
  • (structvar-exp x) )
  • ) )
  • gt (eval-program
  • (scanparse "-(v,x)"))
  • -5
  • Recall that v 5 and x 10 in init-env.

14
Adding conditional
  • Concrete Syntax
  • ltexpressiongt if ltexpressiongt
  • then ltexpressiongt else ltexpressiongt
  • Abstract Syntax
  • if-exp (test-exp true-exp false-exp)
  • Addl. Semantic Function
  • (define (true-value? x)
  • (not (zero? x)) )

15
  • 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.

16
Scanner Details
  • gt (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))

17
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.
  • let x 5
  • in let x 6 x
  • in x x
  • Inner x shadows outer x in nested let-body.

x 5 y11
x 5
x 11
18
  • let x 5
  • in let x 6 x
  • in x x
  • x
  • Introducing let requires passing relevant
    environment to the evaluator.
  • Inner binding overrides the outer one in case of
    conflict. (Example Expression Value 110)

x 5
x 5 x 11
x 5
19
Adding let
  • Concrete Syntax
  • ltexpressiongt
  • let ltidentifiergt
  • ltexpressiongt
  • in ltexpressiongt
  • Abstract Syntax
  • let-exp (ids rands body)

20
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?) )
  • )

21
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 (eoplerror 'eval-expression "Not
    heres" exp))
  • )))

22
Recapitulation
23
(No Transcript)
24
(No Transcript)
25
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.
Write a Comment
User Comments (0)
About PowerShow.com