Mini Language Interpreter Programming Languages (CS 550)

Slides:



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

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.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
Metacircular Evaluation SICP Chapter 4 Mark Boady.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Denotational Semantics ICS.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
Imperative Programming
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
1 Programming Languages (CS 550) Lecture 9 Summary Introduction to Formal Semantics Jeremy R. Johnson TexPoint fonts used in EMF. Read the TexPoint manual.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Chapter 9: Functional Programming in a Typed Language.
Semantics. Semantics is a precise definition of the meaning of a syntactically and type-wise correct program. Ideas of meaning: –Operational Semantics.
CPS 506 Comparative Programming Languages Syntax Specification.
Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Syntax-Directed Definitions and Attribute Evaluation Compiler Design Lecture (02/18/98) Computer Science Rensselaer Polytechnic.
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.
Chap. 7, Syntax-Directed Compilation J. H. Wang Nov. 24, 2015.
Louden’s Simple Language for Describing Formal Semantics program → stmt-list stmt-list → stmt ‘;’ stmt-list | stmt stmt → assign-stmt | if-stmt | while-stmt.
Programming Languages Meeting 3 September 9/10, 2014.
Compiler Design Lecture 10 Semantic Analysis. int aintegers int a[2][3]array(2, array(3, integer)) int f(int, float, char) int x float x char  int Int.
1 Introduction to Functional Programming in Racket CS 270 Math Foundations of CS Jeremy Johnson.
1 Proving Properties of Recursive List Functions CS 270 Math Foundations of CS Jeremy Johnson.
1 Programming Languages (CS 550) List Processing, Dynamic Memory Allocation and Garbage Collection Jeremy R. Johnson.
Functional Programming
CS314 – Section 5 Recitation 9
Operational Semantics of Scheme
Lecture 4: Metacircles Eval Apply David Evans
Chapter 3 – Describing Syntax
Describing Syntax and Semantics
Introduction Chapter : Introduction.
User-Written Functions
Names and Attributes Names are a key programming language feature
System Software Unit-1 (Language Processors) A TOY Compiler
Propositional Calculus: Boolean Functions and Expressions
CS 326 Programming Languages, Concepts and Implementation
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Compiler Lecture 1 CS510.
Env. Model Implementation
Introduction to Functional Programming in Racket
Nondeterministic Evaluation
CS 3304 Comparative Languages
Proving Properties of Recursive List Functions
Programming Languages (CS 550) Mini Language Compiler
CSE 3302 Programming Languages
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)
Streams, Delayed Evaluation and a Normal Order Interpreter
6.001 SICP Variations on a Scheme
Introduction to Functional Programming in Racket
6.001 SICP Interpretation Parts of an interpreter
Introduction Chapter : Introduction.
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
*Lecture based on notes from SICP
Programming Languages (CS 360) Mini Language Compiler
Presentation transcript:

Mini Language Interpreter Programming Languages (CS 550) September 4, 1997 Mini Language Interpreter Programming Languages (CS 550) Jeremy R. Johnson

September 4, 1997 Theme This lecture builds an interpreter for the mini language from Louden Chapter 13 of the text. 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.

Outline Go over last week’s practice exercise September 4, 1997 Outline Go over last week’s practice exercise 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

Outline Adding user defined functions to the mini language September 4, 1997 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

Outline Discuss assignment 2 Add lists to the mini language September 4, 1997 Outline Discuss assignment 2 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.

September 4, 1997 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

Operational Semantics September 4, 1997 Operational Semantics The meaning of a program is obtained by interpreting the result of each statement in some model of computation. We use C++ to interpret execution of each statement Louden [chapter 13] uses a reduction machine which we will discuss later in the term

September 4, 1997 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)

Semantics of Mini Language Statements September 4, 1997 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.

Example Mini Language Program September 4, 1997 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?

Implementing the Interpreter September 4, 1997 Implementing the Interpreter 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.

September 4, 1997 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

Example Mini Language Procedure September 4, 1997 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?

Example Recursive Mini Language Procedure September 4, 1997 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?

What Next? Dynamic memory management and garbage collection September 4, 1997 What Next? Dynamic memory management and garbage collection Assignment: modify mini language and interpreter to handle dynamic memory management and garbage collection Mini Language Compiler Functional programming Functions as first class objects Introduce proc() … end as a value Assignment: modify mini language and interpreter to support this functional programming