Upload
vivian-astin
View
219
Download
3
Embed Size (px)
Citation preview
1
Programming Languages (CS 550)
Mini Language Interpreter
Jeremy R. Johnson
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
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
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
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
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
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++
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
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
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
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)
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
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
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)
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
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
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
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
Environments and Assignment
(7) <E | Env> <E1| Env> ______________________________________________________________________________________________________________________________
<E ‘+’ E2 | Env> < E1 ‘+’ E2 | Env>
(15) Env(I) = V ____________________________________________________________________________
<I | Env> <V | Env>
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>
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)
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), !.
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
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>
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>
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
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
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.
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
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
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
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