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

Slides:



Advertisements
Similar presentations
Simplifications of Context-Free Grammars
Advertisements

JavaCUP JavaCUP (Construct Useful Parser) is a parser generator
Chapter 1 The Study of Body Function Image PowerPoint
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Programming Language Concepts
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append.
Chapter 2-2 A Simple One-Pass Compiler
Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Lilian Blot TO PROGRAMMING & PYTHON Introduction Autumn 2012 TPOP 1.
Plt /7/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.2 An Abstraction for Inductive Data Types.
1 Lecture 16: Tables and OOP. 2 Tables -- get and put.
Semantic Analysis and Symbol Tables
VOORBLAD.
8 VM code generation Aspects of code generation Address allocation
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
© 2012 National Heart Foundation of Australia. Slide 2.
Procedures. 2 Procedure Definition A procedure is a mechanism for abstracting a group of related operations into a single operation that can be used repeatedly.
Understanding Generalist Practice, 5e, Kirst-Ashman/Hull
C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
25 seconds left…...
Chapter 9 Interactive Multimedia Authoring with Flash Introduction to Programming 1.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
PSSA Preparation.
Claus Brabrand, UFPE, Brazil Aug 11, 2010DATA-FLOW ANALYSIS Claus Brabrand ((( ))) Associate Professor, Ph.D. ((( Programming, Logic, and.
1 Decidability continued…. 2 Theorem: For a recursively enumerable language it is undecidable to determine whether is finite Proof: We will reduce the.
Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method.
4/11/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Semantics Static semantics Dynamic semantics attribute grammars
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
1 The metacircular evaluator Names Extend the calculator to store intermediate results as named values (define x (+ 4 5)) store result as x (+ x.
Metacircular Evaluation SICP Chapter 4 Mark Boady.
1 The Metacircular Evaluator Chapter 4 Section 4.1 We will not cover every little detail in the lectures, so you MUST read Section 4.1 in full.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
SICP Interpretation part 1 Parts of an interpreter Arithmetic calculator Names Conditionals and if Store procedures in the environment.
Dec Formal Semantics1 Programming Language Theory Formal Semantics Leif Grönqvist The national Graduate School of Language Technology (GSLT) MSI.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
1 Programming Languages (CS 550) Lecture 9 Summary Introduction to Formal Semantics Jeremy R. Johnson TexPoint fonts used in EMF. Read the TexPoint manual.
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
Louden’s Simple Language for Describing Formal Semantics program → stmt-list stmt-list → stmt ‘;’ stmt-list | stmt stmt → assign-stmt | if-stmt | while-stmt.
Operational Semantics of Scheme
CS 326 Programming Languages, Concepts and Implementation
The interpreter.
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Env. Model Implementation
Original material by Eric Grimson
Nondeterministic Evaluation
Mini Language Interpreter Programming Languages (CS 550)
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
Programming Languages (CS 550) Mini Language Semantics
Abstract Syntax Prabhaker Mateti 1.
The Metacircular Evaluator
The Metacircular Evaluator (Continued)
Lecture 26: The Metacircular Evaluator Eval Apply
6.001 SICP Further Variations on a Scheme
Streams, Delayed Evaluation and a Normal Order Interpreter
6.001 SICP Variations on a Scheme
6.001 SICP Interpretation Parts of an interpreter
topics interpreters meta-linguistic abstraction eval and apply
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
*Lecture based on notes from SICP
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

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 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 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 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 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 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 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 machine  Program  Control  Store  Scheme evaluator  Rely on semantics of scheme  Parsing 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 machine  Program  Control  Store  Scheme evaluator  Reduction 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) 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 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 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 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)  ______________________________________________________________________________________________________________________________  (15) Env(I) = V ____________________________________________________________________________ 

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

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 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 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)  __________________________________________________________________________________________________________________________________  (21) V > 0 ______________________________________________________________________________________________________________________________  (22) V  0 _____________________________________________________________________ 

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

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