Presentation is loading. Please wait.

Presentation is loading. Please wait.

Prof. Fateman CS 164 Lecture 181 Language definition by interpreter Lecture 18.

Similar presentations


Presentation on theme: "Prof. Fateman CS 164 Lecture 181 Language definition by interpreter Lecture 18."— Presentation transcript:

1 Prof. Fateman CS 164 Lecture 181 Language definition by interpreter Lecture 18

2 Prof. Fateman CS 164 Lecture 182 Routes to defining a language Formal mathematics –Context free grammar –Mathematical semantics (axioms, theorems, proofs) –Rarely used (the downfall of Algol 68) –Theoretical interest Informal textual –CFG + natural language (Algol 60) –Just natural language (Tiger?) –Almost universally used Operational –Here’s a program that does the job –Metacircular evaluator for Scheme, Lisp –Evaluator/ interpreter for Tiger

3 Prof. Fateman CS 164 Lecture 183 input Typical compiler structure Source program AST Intermediate form output Lex, parse Typecheck, cleanup Assembly lang Object code Machine

4 Prof. Fateman CS 164 Lecture 184 input “Tigrun” structure Source program AST Intermediate form output interpreter Lex, parse Typecheck, cleanup What language is interpreter written in? What machine does it run on?

5 Prof. Fateman CS 164 Lecture 185 Interpreter structure: advantages interpreter Interpreter in a higher level language: source language derives semantics from interpreter code and the semantics of the language of the interpreter (e.g. Lisp). What does EACH STATEMENT MEAN? Exhaustive case analysis What are the boundaries of legal semantics? What exactly is the model of scope, etc..

6 Prof. Fateman CS 164 Lecture 186 Interpreter structure: more advantages interpreter Prototyping / debugging easier Portable intermediate form (Byte code ) intermediate form may be compact Security may be more easily enforced

7 Prof. Fateman CS 164 Lecture 187 Interpreter structure: disadvantages interpreter Typically unable to reach full machine speed Difficult to transcend the limits of the underlying language implementation (not full access to machine) Code depends on presence of infrastructure (all of Lisp??) (if meta-circular) bootstrapping…

8 Prof. Fateman CS 164 Lecture 188 An interpreter compromise (e.g. Java VM) interpreter “Compile” to a hypothetical byte-code stack machine appropriate for Java (etc), easily simulated on most real machines Implement this virtual byte-code stack machine on all real machines When speed is an issue, try Just In Time compiling; convert sections of code to machine language for a specific machine.

9 Prof. Fateman CS 164 Lecture 189 Interpreter to Compiler is a small step Modest modification of an interpreter can become a compiler. For example: Interpreter: To evaluate a sequence {s 1, s 2 }, evaluate s 1 then evaluate s 2, returning the last of these. Compiler: To compile a sequence {s 1, s 2 }, compile s 1 then compile s 2, returning the concatenation of the code for s 1 and the code for s 2. Interpreter: To evaluate a sum (+ A B) evaluate A, evaluate B and add. Compiler: To compile a sum, (+ A B) compile A, compile B, concatenate results, compile + to “add the results of the two previous sections of code”. Program structure is a walk over the intermediate code.

10 Prof. Fateman CS 164 Lecture 1810 AST for merge.tig ;;; -*- Mode: Lisp; Syntax: Common-Lisp; package: tiger -*- (in-package :tiger) (defparameter merge-ast '(LetExp (1. 3) (DecList (TypeDec (3. 9) any (FieldList (FieldDescriptor (3. 16) any int))) (VarDec (4. 4) buffer nil (CallExp (4. 22) (getchar (4. 22)) (ExpList))) (FunctionDec (6. 16) readint (ExpList (IdType (6. 20) any any)) (int (6. 32)) (LetExp (7. 4) (DecList (VarDec (7. 8) i nil (IntExp (7. 15) 0)) (FunctionDec (8. 21) isdigit (ExpList (IdType (8. 23) s string)) ;Explist or FieldList? (int (8. 39)) (IfExp (9. 12) (OpExp (9. 12) (>= (9. 12)) (CallExp (9. 7) (ord (9. 7)) (ExpList (SimpleVar (9. 9) s))) (CallExp (9. 15) (ord (9. 15)) (ExpList (StringExp (9. 19) "0")))) (OpExp (9. 31) (<= (9. 31)) (CallExp (9. 26) (ord (9. 26)) (ExpList (SimpleVar (9. 28) s))); which line/col#? (CallExp (9. 34) (ord (9. 34)) (ExpList (StringExp (9. 38) "9")))) (IntExp (9. 12) 0)))…….

11 Prof. Fateman CS 164 Lecture 1811 Intermediate form (cleanup) for merge.ast ;;; -*- Mode: Lisp; Syntax: Common-Lisp; package: tiger -*- (in-package :tiger) (defparameter merge-cleaned ;; the line numbers removed; other excess fluff removed. '(LetExp (DecList (TypeDec any (FieldList (FieldDescriptor any int))) (VarDec buffer nil (getchar)) (FunctionDec readint (ExpList (IdType any any)) (int);; ? (int) or int? (LetExp (DecList (VarDec i nil 0) (FunctionDec isdigit (ExpList (IdType s string)) (int) (IfExp (>= (ord s) (ord "0")) (<= (ord s) (ord "9")) 0)) (FunctionDec skipto (ExpList) nil (WhileExp (IfExp (= buffer " ") 1 (= buffer " ")) (AssignExp buffer (getchar))))) (ExpList (skipto) (AssignExp (FieldVar any any) (isdigit buffer)) (WhileExp (isdigit buffer) (ExpList (AssignExp i (- (+ (* i 10) (ord buffer)) (ord "0"))) (AssignExp buffer (getchar))))

12 Prof. Fateman CS 164 Lecture 1812 Start of the interpreter (defun tig-interp (x env); x= intermediate code, env=environment of bindings ;; first deal with interp of trivial or error cases which might occur. (cond ((null x) nil) ((eq x 'VOID) 'VOID) ;;or (error "attempt to evaluate VOID") ((symbolp x) (get-var x env)) ((atom x) x);; integers and strings are atoms. (t (tig-interp-list x env)))) ;; the real business is in lisp lists

13 Prof. Fateman CS 164 Lecture 1813 Start of the interpreter: tig-interp-list (defun tig-interp-list (x env); environment of bindings (case (first x) (ExpList;; a sequence of expressions. (last1 (mapcar #'(lambda(r)(tig-interp r env)) (cdr x)))) (AssignExp (set-var (elt x 1)(elt x 2) env)) (IfExp (tig-interp-if (elt x 1)(elt x 2) (elt x 3) env)) ;; see next slide (LetExp (tig-interp-let (elt x 1)(elt x 2) env)) (WhileExp (tig-interp-while (elt x 1)(elt x 2) env)) (ArrayExp (make-array (tig-interp (elt x 2) env); size :initial-element (tig-interp (elt x 3) env))) (RecordExp (tig-interp-rec x env)) (FieldVar (cadr (assoc (elt x 2) (tig-interp (elt x 1) env)))) (BreakExp (throw 'tig-break nil)) (SubscriptVar (aref (get-var (elt x 1) env); get the array (tig-interp (elt x 2)env))) (ForExp (tig-interp-for (elt x 1)(elt x 2) (elt x 3)(elt x 4) env)) (t;; a CallExp ;; get the procedure ;; evaluate the arguments (tig-call (get-fun (car x) env) (mapcar #'(lambda(p) (tig-interp p env)) (cdr x)) env ))))))

14 Prof. Fateman CS 164 Lecture 1814 Next time: all the functions + cast of supporting operations All the functions All the supporting data structures


Download ppt "Prof. Fateman CS 164 Lecture 181 Language definition by interpreter Lecture 18."

Similar presentations


Ads by Google