![Page 1: 1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson](https://reader035.vdocuments.net/reader035/viewer/2022062712/56649c745503460f94927207/html5/thumbnails/1.jpg)
1
Programming Languages (CS 550)
Mini Language Interpreter
Jeremy R. Johnson
![Page 2: 1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson](https://reader035.vdocuments.net/reader035/viewer/2022062712/56649c745503460f94927207/html5/thumbnails/2.jpg)
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.
![Page 3: 1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson](https://reader035.vdocuments.net/reader035/viewer/2022062712/56649c745503460f94927207/html5/thumbnails/3.jpg)
3
Outline
Introduce Mini Language syntax and semanticsEnvironments (Symbol Table)
Abstract syntax tree (more yacc and attribute grammars)
Mini Language InterpreterExercise 1: Modify the Mini Language and
interpreter to support “repeat … until” statement
![Page 4: 1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson](https://reader035.vdocuments.net/reader035/viewer/2022062712/56649c745503460f94927207/html5/thumbnails/4.jpg)
4
Outline
Adding user defined functions to the mini languageparameter 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
![Page 5: 1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson](https://reader035.vdocuments.net/reader035/viewer/2022062712/56649c745503460f94927207/html5/thumbnails/5.jpg)
5
Outline
Discuss assignment 4Add lists to the mini languageValues 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
![Page 6: 1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson](https://reader035.vdocuments.net/reader035/viewer/2022062712/56649c745503460f94927207/html5/thumbnails/6.jpg)
6
Mini Language Syntax
1. < program > → < stmt-list>
2. < stmt-list> → < stmt > ; < stmt-list > | < stmt >
3. < stmt > → < assign-stmt > | < if-stmt > | < while-stmt >
4. < assign-stmt > → < identifier > := < expr >
5. < if-stmt > → if < expr > then < stmt-list > else < stmt-list > fi
6. < while-stmt > → while < expr > do < stmt-list > od
7. < expr > → < expr > + < term > | < expr > - < term > | < term >
8. < term > → < term > * < factor > | < factor >
9. < factor > → ( < expr > ) | < number > | < identifier >
10. < number > → < number > < digit > | < digit >
11. < digit > → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
12. < identifier > → < identifier > < letter > | < letter >
13. < letter > → a | b | c | ... | z
![Page 7: 1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson](https://reader035.vdocuments.net/reader035/viewer/2022062712/56649c745503460f94927207/html5/thumbnails/7.jpg)
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 schemeImplementation in prologImplementation with parser in C++
![Page 8: 1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson](https://reader035.vdocuments.net/reader035/viewer/2022062712/56649c745503460f94927207/html5/thumbnails/8.jpg)
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)
![Page 9: 1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson](https://reader035.vdocuments.net/reader035/viewer/2022062712/56649c745503460f94927207/html5/thumbnails/9.jpg)
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.
![Page 10: 1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson](https://reader035.vdocuments.net/reader035/viewer/2022062712/56649c745503460f94927207/html5/thumbnails/10.jpg)
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?
![Page 11: 1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson](https://reader035.vdocuments.net/reader035/viewer/2022062712/56649c745503460f94927207/html5/thumbnails/11.jpg)
11
Operational Semantics
Define language by describing its actions in terms of operations of an actual or hypothetical machine. Need precise description of machineProgram Control Store
Scheme evaluatorRely on semantics of schemeParsing not required (program is abstract syntax
tree)
![Page 12: 1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson](https://reader035.vdocuments.net/reader035/viewer/2022062712/56649c745503460f94927207/html5/thumbnails/12.jpg)
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
![Page 13: 1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson](https://reader035.vdocuments.net/reader035/viewer/2022062712/56649c745503460f94927207/html5/thumbnails/13.jpg)
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
![Page 14: 1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson](https://reader035.vdocuments.net/reader035/viewer/2022062712/56649c745503460f94927207/html5/thumbnails/14.jpg)
14
Operational Semantics
Define language by describing its actions in terms of operations of an actual or hypothetical machine. Need precise description of machineProgram Control Store
Scheme evaluatorReduction machine
Reduce program to a semantic “value”Reduction rules (logical inference rules)
![Page 15: 1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson](https://reader035.vdocuments.net/reader035/viewer/2022062712/56649c745503460f94927207/html5/thumbnails/15.jpg)
15
Operational Semantics of Mini Language Expressions
(1) ‘0’ 0,…, ‘9’ 9
(2) V’0’ 10*V,…,V’9’ 10*V+9
(3) V1 ‘+’ V2 V1 + V2
(4) V1 ‘+’ V2 V1 + V2
(5) V1 ‘*’ V2 V1 * V2
![Page 16: 1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson](https://reader035.vdocuments.net/reader035/viewer/2022062712/56649c745503460f94927207/html5/thumbnails/16.jpg)
16
Mini Language Expressions
(7) E E1 _____________________________________________________________________
E ‘+’ E2 E1 ‘+’ E2
(8) E E1 _____________________________________________________________________
E ‘-’ E2 E1 ‘-’ E2
(9) E E1 _____________________________________________________________________
E ‘*’ E2 E1 ‘*’ E2
![Page 17: 1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson](https://reader035.vdocuments.net/reader035/viewer/2022062712/56649c745503460f94927207/html5/thumbnails/17.jpg)
17
Mini Language Expressions
(10) E E1 _____________________________________________________________________
V ‘+’ E V ‘+’ E1
(11) E E1 ____________________________________________________________________
V ‘-’ E V ‘-’ E1
(12) E E1 ____________________________________________________________________
V ‘*’ E V ‘*’ E1
(14) E E1, E1 E2 [transitive closure] _____________________________________________________________________
E E2
![Page 18: 1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson](https://reader035.vdocuments.net/reader035/viewer/2022062712/56649c745503460f94927207/html5/thumbnails/18.jpg)
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).
![Page 19: 1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson](https://reader035.vdocuments.net/reader035/viewer/2022062712/56649c745503460f94927207/html5/thumbnails/19.jpg)
19
Environments and Assignment
(7) <E | Env> <E1| Env> ______________________________________________________________________________________________________________________________
<E ‘+’ E2 | Env> < E1 ‘+’ E2 | Env>
(15) Env(I) = V ____________________________________________________________________________
<I | Env> <V | Env>
![Page 20: 1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson](https://reader035.vdocuments.net/reader035/viewer/2022062712/56649c745503460f94927207/html5/thumbnails/20.jpg)
20
Environments and Assignment
(16) <I ‘:=’ V | Env> Env & {I = V}
(17) <E | Env> <E1 | Env> ______________________________________________________________________________________________________________________
<I ‘:=’ E | Env> <I ‘:=’ E1 | Env>
(18) <S | Env> Env1 ______________________________________________________________________________________________
<S ‘;’ L | Env> <L | Env1>
(19) L < L | Env0>
![Page 21: 1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson](https://reader035.vdocuments.net/reader035/viewer/2022062712/56649c745503460f94927207/html5/thumbnails/21.jpg)
21
Implementation in Prolog
Configurations<E | Env> Config(E,Env)
Environments[value(I1,v1),...,value(In,vn)]
Predicate to lookup values Lookup(Env,I,V)
![Page 22: 1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson](https://reader035.vdocuments.net/reader035/viewer/2022062712/56649c745503460f94927207/html5/thumbnails/22.jpg)
22
Implementation in Prolog
Configurations<E | Env> Config(E,Env)
Environments[value(I1,v1),...,value(In,vn)]
Predicate to lookup values Lookup(Env,I,V)
lookup([value(I,V)|_],I,V).
lookup([_|Es],I,V) :- lookup(Es,I,V), !.
![Page 23: 1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson](https://reader035.vdocuments.net/reader035/viewer/2022062712/56649c745503460f94927207/html5/thumbnails/23.jpg)
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)).
![Page 24: 1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson](https://reader035.vdocuments.net/reader035/viewer/2022062712/56649c745503460f94927207/html5/thumbnails/24.jpg)
24
If Statements(20) <E | Env> <E1| Env> __________________________________________________________________________________________________________________________________
<‘if’ E ‘then’ L1 ‘else’ L2 ‘fi’ | Env> <‘if’ E1 ‘then’ L1 ‘else’ L2 ‘fi’ | Env>
(21) V > 0 ______________________________________________________________________________________________________________________________
<‘if’ V ‘then’ L1 ‘else’ L2 ‘fi’ | Env> < L1|Env>
(22) V 0 _____________________________________________________________________
<‘if’ V ‘then’ L1 ‘else’ L2 ‘fi’ | Env> < L2|Env>
![Page 25: 1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson](https://reader035.vdocuments.net/reader035/viewer/2022062712/56649c745503460f94927207/html5/thumbnails/25.jpg)
25
While Statements
(23) <E | Env> <V| Env>, V 0 ________________________________________________________________________________________________________________
<‘while’ E ‘do’ L ‘od’|Env> Env
(24) <E | Env> <V| Env>, V > 0 _____________________________________________________________________________________________________________
<‘while’ E ‘do’ L ‘od’|Env> <L;‘while’ E ‘do’ L ‘od’|Env>
![Page 26: 1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson](https://reader035.vdocuments.net/reader035/viewer/2022062712/56649c745503460f94927207/html5/thumbnails/26.jpg)
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)]
![Page 27: 1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson](https://reader035.vdocuments.net/reader035/viewer/2022062712/56649c745503460f94927207/html5/thumbnails/27.jpg)
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)]
![Page 28: 1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson](https://reader035.vdocuments.net/reader035/viewer/2022062712/56649c745503460f94927207/html5/thumbnails/28.jpg)
28
Implementing the Interpreter
Parser (create abstract syntax tree)Syntax directed semanticsThe 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.
![Page 29: 1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson](https://reader035.vdocuments.net/reader035/viewer/2022062712/56649c745503460f94927207/html5/thumbnails/29.jpg)
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
![Page 30: 1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson](https://reader035.vdocuments.net/reader035/viewer/2022062712/56649c745503460f94927207/html5/thumbnails/30.jpg)
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?
![Page 31: 1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson](https://reader035.vdocuments.net/reader035/viewer/2022062712/56649c745503460f94927207/html5/thumbnails/31.jpg)
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?
![Page 32: 1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson](https://reader035.vdocuments.net/reader035/viewer/2022062712/56649c745503460f94927207/html5/thumbnails/32.jpg)
32
What Next?
Parsing and parser generatorsDynamic memory management and garbage
collectionAssignment: modify mini language and interpreter to
handle dynamic memory management and garbage collection
Functional programmingFunctions as first class objects Introduce proc() … end as a valueAssignment: modify mini language and interpreter to
support this functional programming