Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 10 Function Implementation In theory, there.

Slides:



Advertisements
Similar presentations
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 11 Memory Management C makes it easy to shoot.
Advertisements

CPSC 388 – Compiler Design and Construction
Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth.
Intermediate Code Generation
1 Compiler Construction Intermediate Code Generation.
Types and Arithmetic Operators
Slide 1 Vitaly Shmatikov CS 345 Functions. slide 2 Reading Assignment uMitchell, Chapter 7 uC Reference Manual, Chapters 4 and 9.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Introduction to Stacks What are Stacks? Stack implementation using arrays. Stack application.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 12 Imperative Programming I really hate this.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 10 Function Implementation In theory, there.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Chapter 6: Type Systems Fall 2009.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 6 Type Systems I was eventually persuaded.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 7 Semantics Ismael: “Surely all this is not.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 13 Object-Oriented Programming I am surprised.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 4 Names The first step toward wisdom is calling.
Programming Languages 2nd edition Tucker and Noonan Chapter 6 Types I was eventually persuaded of the need to design programming notations so as to maximize.
CSC321: Programming Languages1 Programming Languages Tucker and Noonan Chapter 9: Functions 9.1 Basic Terminology 9.2 Function Call and Return 9.3 Parameters.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 3 Lexical and Syntactic Analysis Syntactic.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 9 Functions It is better to have 100 functions.
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
Operator Overloading in C++
Copyright © 2003 Pearson Education, Inc. Slide 2-1 Problem Solving with Java™ Second Edition Elliot Koffman and Ursula Wolz Copyright © 2003 Pearson Education,
Imperative Programming
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
Chapter 2 part #4 Operator
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
Chapter 9 Functions It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures. A. Perlis.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Class Example - Rationals Rational numbers are represented by the ratio of two integers, a numerator and a denominator, e.g., 2/3. This is opposed to irrational.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Dr. Philip Cannata 1 Functions and Recursion. Dr. Philip Cannata 2 10 Java (Object Oriented) ASP RDF (Horn Clause Deduction, Semantic Web) Relation Jython.
Dr. Philip Cannata 1 Lexical and Syntactic Analysis Chomsky Grammar Hierarchy Lexical Analysis – Tokenizing Syntactic Analysis – Parsing Hmm Concrete Syntax.
Semantics. Semantics is a precise definition of the meaning of a syntactically and type-wise correct program. Ideas of meaning: –Operational Semantics.
Programming Languages 2nd edition Tucker and Noonan Chapter 6 Type Systems I was eventually persuaded of the need to design programming notations so as.
Copyright © 2006 The McGraw-Hill Companies, Inc. Basic Terminology Value-returning functions: –known as “non-void functions/methods” in C/C++/Java –called.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Syntax (2).
Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in.
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R T W O Syntax.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Fall 2008Programming Development Techniques 1 Topic 17 Assignment, Local State, and the Environment Model of Evaluation Section 3.1 & 3.2.
Dr. Philip Cannata 1 Functions and Recursion Programming Languages.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 8 Semantic Interpretation To understand a.
1 Sections 3.1 – 3.2a Basic Syntax and Semantics Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Types Type Errors Static and Dynamic Typing Basic Types NonBasic Types
Functions.
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Programming Languages 2nd edition Tucker and Noonan
Programming Languages 2nd edition Tucker and Noonan
Introduction to Abstract Data Types
Programming Languages 2nd edition Tucker and Noonan
Programming Languages 2nd edition Tucker and Noonan
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 2/23/2019.
Operator King Saud University
Programming Languages 2nd edition Tucker and Noonan
Corresponds with Chapter 5
Presentation transcript:

Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 10 Function Implementation In theory, there is no difference between theory and practice, but not in practice. Anonymous

Copyright © 2006 The McGraw-Hill Companies, Inc. Contents 10.1 Function Declaration and Call in Clite 10.2 Completing the Clite Type System 10.3 Semantics of Call and Return 10.4 Formal Treatment of Types and Semantics

Copyright © 2006 The McGraw-Hill Companies, Inc Function Declaration and Call Example Clite Program Fig 10.1 int h, i; void B(int w) { int j, k; i = 2*w; w = w+1; } void A(int x, int y) { bool i, j; B(h); } int main() { int a, b; h = 5; a = 3; b = 2; A(a, b); }

Copyright © 2006 The McGraw-Hill Companies, Inc Concrete Syntax Functions and Globals (new elements underlined) Program  { Type Identifier FunctionOrGlobal } MainFunction Type  int | boolean | float | char | void FunctionOrGlobal  ( Parameters ) { Declarations Statements } | Global Parameters  [ Parameter {, Parameter } ] Global  {, Identifier } ; MainFunction  int main ( ) { Declarations Statements }

Copyright © 2006 The McGraw-Hill Companies, Inc. Concrete Syntax (cont’d) Function Calls (new elements underlined) Statement  ; | Block | Assignment | IfStatement | WhileStatement | CallStatement | ReturnStatement CallStatement  Call ; ReturnStatement  return Expression ; Factor  Identifier | Literal | ( Expression ) | Call Call  Identifier ( Arguments ) Arguments  [ Expression {, Expression } ]

Copyright © 2006 The McGraw-Hill Companies, Inc Abstract Syntax Program = Declarations globals; Functions functions Functions = Function* Function = Type t; String id; Declarations params, locals; Block body Type = int | boolean | float | char | void Statement = Skip | Block | Assignment | Conditional | Loop | Call | Return Call = String name; Expressions args Expressions = Expression* Return = Variable target; Expression result Expression = Variable | Value | Binary | Unary | Call

Copyright © 2006 The McGraw-Hill Companies, Inc. Abstract Syntax for a Clite Program Fig 10.4 globals body Program Functions Declarations Function int main void A void B params locals body h i w A(a,b);a b i j x y w = w + 1;i = 2 * w; j k Block Call

Copyright © 2006 The McGraw-Hill Companies, Inc Completing the Clite Type System Type Rule 10.1 Every function and global id must be unique. E.g., h, i, A, B, and main are unique. Type Rule 10.2 Every function’s params and locals must have mutually unique id’s. E.g., function A’s locals and params have id’s x, y, i, and j. Type Rule 10.3 Every statement in the body of each function must be valid with respect to the function’s locals, params, and visible globals. Note: Combine with type rules in Chapter 6 (type systems) for the different statement types.

Copyright © 2006 The McGraw-Hill Companies, Inc. Type Rules for Call and Return Type Rule 10.4 A non-void function (except main) must have a Return statement, whose Expression must be the same type as the function. Type Rule 10.5 A void function cannot have a Return. Type Rule 10.6 Every Call Statement must identify a void function, and every Call Expression must identify a non-void function. Type Rule 10.7 Every Call must have the same number of args as the number of params in the function it identifies. Each such arg must have the same type as its corresponding param, reading from left to right. Type Rule 10.8 Every Call to a non-void function has the type of that function. The Expression in which the Call appears must be valid according to Type Rules 6.5 and 6.6.

Copyright © 2006 The McGraw-Hill Companies, Inc. Type Rules for Sample Program Type Rule 10.4 No return statement appears in main. Type Rule 10.5 No return statement appears in A or B. Type Rule 10.6 The Call Statement A(a,b) identifies the function void A(int x, int y) Type Rule 10.7 The Call Statement A(a,b) has two arguments, whose types ( int ) are the same as the parameters x and y. Type Rule 10.8 is not applicable.

Copyright © 2006 The McGraw-Hill Companies, Inc. Example with Non-void Functions int fibonacci (int n) { int fib0, fib1, temp, k; fib0 = 0; fib1 = 1; k = n; while (k > 0) { temp = fib0; fib0 = fib1; fib1 = fib0 + temp; k = k - 1; } return fib0; } int main () { int answer; answer = fibonacci(8); } Computing a Fibonacci Number Fig 10.5

Copyright © 2006 The McGraw-Hill Companies, Inc. Type Rules for Fibonacci program Type Rule 10.4 return fib0; appears in int fibonacci (int n) Type Rule 10.5 is not applicable Type Rule 10.6 The Call fibonacci(8) identifies the non-void function int fibonacci (int n) Type Rule 10.7 The Call fibonacci(8) has one argument, whose type ( int ) is the same as that of the parameter n. Type Rule 10.8 The Expression in which the Call fibonacci(8) appears is valid.

Copyright © 2006 The McGraw-Hill Companies, Inc Semantics of Call and Return Meaning Rule 10.1 The meaning of a Call c to Function f has the following steps: 1.Make an activation record and add f’s params and locals to it. 2.Evaluate c’s args and assign those values to f’s corresponding params. 3.If f is non-void, add a result variable identical with f’s name and type. 4.Push the activation record onto the run-time stack. 5.Interpret f’s body. 6.Pop the activation record from the stack. 7.If f is non-void, return the value of the result variable to the Expression where c appears.

Copyright © 2006 The McGraw-Hill Companies, Inc. Example Program Trace (Fig 10.6) Calling Returning Visible State main,,, A,,,, B,,,, A,,,, main,,,

Copyright © 2006 The McGraw-Hill Companies, Inc Non-void Functions Meaning Rule 10.2 The meaning of a Return is the result of assigning the value of the result Expression to the result variable. Meaning Rule 10.3 The meaning of a Block is the aggregated meaning of its Statements when applied to the current state, up to and including the point where the first Return is encountered. Note: The first Return encountered terminates the body of a called function.

Copyright © 2006 The McGraw-Hill Companies, Inc. Example Program Trace (Fig 10.7) Calling Returning Visible State main fibonacci,,,,, fibonacci,,,,, main

Copyright © 2006 The McGraw-Hill Companies, Inc Side-Effects Revisited Side-effects can occur in Clite. –E.g., in Figure 10.1, a call to B alters the value of global variable i. –If B had been non-void, this side-effect could have subtly affected an expression like B(h)+i. –In the semantics of Clite, evaluation of operands in a Binary is left-to-right. Thus, the result of B(h)+i is always the same, and always different from i+B(h). Side-effects often create distance between mathematics and programming. –E.g., Clite expressions with + are clearly not commutative, while in mathematics they are.