Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.

Similar presentations


Presentation on theme: "1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson."— Presentation transcript:

1 1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson

2 2 Theme  This lecture builds an interpreter for the mini language from Chapter 13 of the book Programming Languages by Ken Louden.  First the interpreter is written in scheme and prolog. Then a parser is written that translates the input program into a data structure that can easily be interpreted.  The language is extended to support procedures.

3 3 Outline  Introduce Mini Language syntax and semantics  Environments (Symbol Table)  Abstract syntax tree (more yacc and attribute grammars)  Mini Language Interpreter  Exercise 1: Modify the Mini Language and interpreter to support “repeat … until” statement

4 4 Outline  Adding user defined functions to the mini language  parameter passing  local variables (local environment)  function application  Execute procedure body in local environment with formal parameters bound to actual argument values  return value  recursion  Exercise 2: Modify the extended Mini Language and interpreter to use an explicit return statement

5 5 Outline  Discuss assignment 4  Add lists to the mini language  Values are now lists or ints (modify Environment)  Support list constants and built-in list processing functions  cons( e, L ) - appends element e to the front of list  car( L ) - returns the first element in the list  cdr( L ) - returns the rest of the list (minus the first element)  nullp( L ) - returns 1 if L is null, 0 otherwise  intp( e ) - returns 1 if e is an integer, 0 otherwise  listp( e ) - returns 1 if e is a list, 0 otherwise to allow construction and access to lists.  Provide memory allocator and garbage collection

6 6 Mini Language Syntax 1. → 2. → ; | 3. → | | 4. → := 5. → if then else fi 6. → while do od 7. → + | - | 8. → * | 9. → ( ) | | 10. → | 11. → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | → | 13. → a | b | c |... | z

7 7 Operational Semantics  The meaning of a program is obtained by interpreting the result of each statement in some model of computation.  The meaning of a statement and a program (sequence of statements) is determined by its affect on the environment  Implementation in scheme  Implementation in prolog  Implementation with parser in C++

8 8 Environments  Let an Environment be a map from indentifiers to values = integers  undefined  Mini language programs can be thought of as a map from an initial Environment to a final Environment (assuming it terminates)  The initial environment maps all identifiers to an undefined  Each statement is defined in terms of what it does to the current environment (another mapping)

9 9 Semantics of Mini Language Statements 1. Env: Identifier → Integer Union {undef} 2. (Env and {I = n})(J) = n if J=I, Env(J) otherwise 3. Env_0 = undef for all I 4. for if-stmt, if expr evaluates to value greater than 0, then evaluate stmt-list after then, else evaluate stmt-list after else 5. for while-stmt, as long as expr evaluates to a value greater than 0, stmt-list is repeatedly executed and expr evaluated.

10 10 Example Mini Language Program 1. n := 0 - 5; 2. if n then i := n else i := 0 - n fi; 3. fact := 1; 4. while i do fact := fact * i; i := i - 1 od What is the final environment?

11 11 Operational Semantics  Define language by describing its actions in terms of operations of an actual or hypothetical machine.  Need precise description of machine  Program  Control  Store  Scheme evaluator  Rely on semantics of scheme  Parsing not required (program is abstract syntax tree)

12 Scheme Evaluator (define (eval prog) (let (env (initial-environment)) (if (stmtlist? prog) (eval-stmtlist prog env) (error "illegal program"))) (define (eval-stmtlist stmtlist env) (if (null? stmtlist) env (eval-stmtlist (cdr stmtlist) (eval-stmt (car stmtlist) env))))) (define (eval-stmt stmt env) (cond ((assign-stmt? stmt) (eval-assign stmt env)) ((if-stmt? stmt) (eval-if stmt env)) ((while-stmt? stmt) (eval-while stmt env)) ) 12

13 Scheme Evaluator (define (eval-assign stmt env) (let ((var (cadr stmt)) (expr (caddr stmt))) (insert-binding var (eval-expr expr env) env)) (define (eval-if stmt env) (let ((expr (cadr stmt)) (S1 (caddr stmt)) (S2 (cadddr stmt))) (if (eval-expr expr) (eval-stmtlist S1) (eval-stmtlist S2)))) (define (eval-while stmt env) (define (loop expr S env) (if (eval-expr expr) (loop expr S (eval-stmtlist S env)) env)) (let ((expr (cadr stmt)) (S (caddr stmt))) (loop expr S env))) 13

14 14 Operational Semantics  Define language by describing its actions in terms of operations of an actual or hypothetical machine.  Need precise description of machine  Program  Control  Store  Scheme evaluator  Reduction machine  Reduce program to a semantic “value”  Reduction rules (logical inference rules)

15 15 Operational Semantics of Mini Language Expressions (1) ‘0’  0,…, ‘9’  9 (2) V’0’  10*V,…,V’9’  10*V+9 (3) V 1 ‘+’ V 2  V 1 + V 2 (4) V 1 ‘+’ V 2  V 1 + V 2 (5) V 1 ‘*’ V 2  V 1 * V 2

16 16 Mini Language Expressions (7) E  E 1 _____________________________________________________________________ E ‘+’ E 2  E 1 ‘+’ E 2 (8) E  E 1 _____________________________________________________________________ E ‘-’ E 2  E 1 ‘-’ E 2 (9) E  E 1 _____________________________________________________________________ E ‘*’ E 2  E 1 ‘*’ E 2

17 17 Mini Language Expressions (10) E  E 1 _____________________________________________________________________ V ‘+’ E  V ‘+’ E 1 (11) E  E 1 ____________________________________________________________________ V ‘-’ E  V ‘-’ E 1 (12) E  E 1 ____________________________________________________________________ V ‘*’ E  V ‘*’ E 1 (14) E  E 1, E 1  E 2 [transitive closure] _____________________________________________________________________ E  E 2

18 18 Implementation in Prolog % reduce_all(times(plus(2,3),minus(5,1)),V). % V = 20 ? reduce(plus(E,E2),plus(E1,E2)) :- reduce(E,E1). reduce(minus(E,E2),minus(E1,E2)) :- reduce(E,E1). reduce(times(E,E2),times(E1,E2)) :- reduce(E,E1). reduce(plus(V,E),plus(V,E1)) :- reduce(E,E1). reduce(minus(V,E),minus(V,E1)) :- reduce(E,E1). reduce(times(V,E),times(V,E1)) :- reduce(E,E1). reduce(plus(V1,V2),R) :- integer(V1), integer(V2), !, R is V1+V2. reduce(minus(V1,V2),R) :- integer(V1), integer(V2), !, R is V1-V2. reduce(times(V1,V2),R) :- integer(V1), integer(V2), !, R is V1*V2. reduce_all(V,V) :- integer(V), !. reduce_all(E,E2) :- reduce(E,E1), reduce_all(E1,E2).

19 19 Environments and Assignment (7)  ______________________________________________________________________________________________________________________________  (15) Env(I) = V ____________________________________________________________________________ 

20 20 Environments and Assignment (16)  Env & {I = V} (17)  ______________________________________________________________________________________________________________________  (18)  Env 1 ______________________________________________________________________________________________  (19) L 

21 21 Implementation in Prolog  Configurations   Config(E,Env)  Environments  [value(I 1,v 1 ),...,value(I n,v n )]  Predicate to lookup values  Lookup(Env,I,V)

22 22 Implementation in Prolog  Configurations   Config(E,Env)  Environments  [value(I 1,v 1 ),...,value(I n,v n )]  Predicate to lookup values  Lookup(Env,I,V) lookup([value(I,V)|_],I,V). lookup([_|Es],I,V) :- lookup(Es,I,V), !.

23 23 Implementation in Prolog % reduce_value(config(times(plus(x,3),minus(5,y)),[value(x,2),value(y,1)]),V). % V = config(20,[value(x,2),value(y,1)]) ? reduce(config(plus(E,E2),Env),config(plus(E1,E2),Env)) :- reduce(config(E,Env),config(E1,Env)). reduce(config(I,Env),config(V,Env)) :- atom(I), lookup(Env,I,V). reduce_all(config(V,Env),config(V,Env)) :- integer(V), !. reduce_all(config(E,Env),config(E2,Env)) :- reduce(config(E,Env),config(E1,Env)), reduce_all(config(E1,Env),config(E2,Env)). reduce_value(config(E,Env),V) :- reduce_all(config(E,Env),config(V,Env)).

24 24 If Statements (20)  __________________________________________________________________________________________________________________________________  (21) V > 0 ______________________________________________________________________________________________________________________________  (22) V  0 _____________________________________________________________________ 

25 25 While Statements (23) , V  0 ________________________________________________________________________________________________________________  Env (24) , V > 0 _____________________________________________________________________________________________________________  

26 26 Implementation in Prolog % Test cases: % reduce_exp_all(config(plus(times(2,5),minus(2,5)),[]),V). % V = config(7,[]) % reduce_exp_all(config(plus(times(x,5),minus(2,y)),[value(x,2),value(y,5)]),V). % V = config(7,[value(x,2),value(y,5)]) % reduce_all(config(seq(assign(x,3),assign(y,4)),[]),Env). % Env = [value(x,3),value(y,4)] % reduce(config(if(3,assign(x,3),assign(x,4)),[]),Env). % Env = [value(x,3)] % reduce(config(if(0,assign(x,3),assign(x,4)),[]),Env). % Env = [value(x,4)] % reduce_all(config(if(n,assign(i,0),assign(i,1)),[value(n,3)]),Env). % Env = [value(n,3),value(i,0)]

27 27 Implementation in Prolog % reduce_all(config(while(x,assign(x,minus(x,1))),[value(x,3)]),Env). % Env = [value(x,0)] % reduce_all(config( % seq(assign(n,minus(0,3)), % seq(if(n,assign(i,n),assign(i,minus(0,n))), % seq(assign(fact,1), % while(i,seq(assign(fact,times(fact,i)),assign(i,minus(i,1))))))) %,[]),Env). % Env = [value(n,-3),value(i,0),value(fact,6)]

28 28 Implementing the Interpreter  Parser (create abstract syntax tree)  Syntax directed semantics  The interpreter is implemented by creating a class, with an evaluate method, for each syntactic category. Use inheritance to derive specialized statements from more general categories of statements. When the parser detects a syntactic category the corresponding constructor is called. A map is used to store the environment and the program is executed by calling all of the evaluate methods of the statements in the program.

29 29 Adding Functions  In this implementation we will insist that all functions are closed. I.E. they only communicate with the calling environment through parameter passing and their meaning is determined soley from the statements in their definition and the parameter values.  parameter passing  local variables (local environment)  separate function table  function application  Execute procedure body in local environment with formal parameters bound to actual argument values  return value  recursion

30 30 Example Mini Language Procedure define add proc(n) i := n; s := 0; while i do s := s + i; i := i-1 od; return := s end; n := 5; s := add(n) What is the final environment?

31 31 Example Recursive Mini Language Procedure define addr proc(n) if n then return := n + addr(n-1) else return := 0 fi end; n := 5; s := addr(n) What is the final environment?

32 32 What Next?  Parsing and parser generators  Dynamic memory management and garbage collection  Assignment: modify mini language and interpreter to handle dynamic memory management and garbage collection  Functional programming  Functions as first class objects  Introduce proc() … end as a value  Assignment: modify mini language and interpreter to support this functional programming


Download ppt "1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson."

Similar presentations


Ads by Google