Principles of Programming Languages Lecture 22

Similar documents
Iterative Statements. Iterative Statements: Examples. Counter-Controlled Loops. ICOM 4036 Programming Languages Statement-Level Control Structure

Chapter 8 Statement-Level Control Structures

Control Structures. Outline. In Text: Chapter 8. Control structures Selection. Iteration. Gotos Guarded statements. One-way Two-way Multi-way

Chapter 8. Statement-Level Control Structures

CPSC 3740 Programming Languages University of Lethbridge. Control Structures

Chapter 7. - FORTRAN I control statements were based directly on IBM 704 hardware

Chapter 8. Statement-Level Control Structures

Chapter 8. Statement-Level Control Structures

Chapter 8. Statement-Level Control Structures ISBN

Statement level control structures

Lecture 15: Iteration and Recursion

Relational Expressions. Boolean Expressions. Boolean Expressions. ICOM 4036 Programming Languages. Boolean Expressions

LECTURE 18. Control Flow

Expressions vs statements

Ch. 7: Control Structures

G Programming Languages - Fall 2012

Flow of Control Execution Sequence

CSCI 3136 Principles of Programming Languages

Chapter 8 Statement-Level Control Structure

Concepts of Programming Languages

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

Principles of Programming Languages Lecture 7

LECTURE 3. Compiler Phases

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

CMSC 330: Organization of Programming Languages

Introduction. A. Bellaachia Page: 1

Principles of Programming Languages. Lecture Outline

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5-1

LECTURE 5 Control Structures Part 2

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Compiler Theory. (GCC the GNU Compiler Collection) Sandro Spina 2009

Organization of Programming Languages (CSE452) Why are there so many programming languages? What makes a language successful?

IA010: Principles of Programming Languages

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

COP4020 Programming Languages. Control Flow Prof. Robert van Engelen

UNIT I Programming Language Syntax and semantics. Kainjan Sanghavi

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

SKILL AREA 304: Review Programming Language Concept. Computer Programming (YPG)

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

CS A331 Programming Language Concepts

Chapter 5. Names, Bindings, and Scopes

CS 241 Control Structures. Christopher A. Gantz. SPS Undergraduate Program Regis University

Why are there so many programming languages? Why do we have programming languages? What is a language for? What makes a language successful?

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

Programmiersprachen (Programming Languages)

Software II: Principles of Programming Languages. Why Expressions?

Compiler Construction

Chapter 7 Control I Expressions and Statements

int foo() { x += 5; return x; } int a = foo() + x + foo(); Side-effects GCC sets a=25. int x = 0; int foo() { x += 5; return x; }

Ordering Within Expressions. Control Flow. Side-effects. Side-effects. Order of Evaluation. Misbehaving Floating-Point Numbers.

Early computers (1940s) cost millions of dollars and were programmed in machine language. less error-prone method needed

Control Flow COMS W4115. Prof. Stephen A. Edwards Fall 2006 Columbia University Department of Computer Science

Chapter 1. Preliminaries

Programming Languages

Algorithm: 1. Introduction

Definition Checklist for Source Statement Counts

1 Lexical Considerations

Chapter 5: Loops and Files

Chapter 5: Prefix vs. Postfix 8/19/2018. The Increment and Decrement Operators. Increment and Decrement Operators in Program 5-1

L o o p s. for(initializing expression; control expression; step expression) { one or more statements }

Programming Languages 2nd edition Tucker and Noonan"

Computer Programming I - Unit 5 Lecture page 1 of 14

List of lectures. Lecture content. Imperative Programming. TDDA69 Data and Program Structure Imperative Programming and Data Structures

NOTE: Answer ANY FOUR of the following 6 sections:

Why are there so many programming languages?

COP4020 Programming Assignment 1 - Spring 2011

MIDTERM EXAM (Solutions)

DM550 Introduction to Programming part 2. Jan Baumbach.

General Concepts. Abstraction Computational Paradigms Implementation Application Domains Influence on Success Influences on Design

Run-time Environments

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Run-time Environments

Continuations provide a novel way to suspend and reexecute

CS 314 Principles of Programming Languages

The role of semantic analysis in a compiler

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

CSC 326H1F, Fall Programming Languages. What languages do you know? Instructor: Ali Juma. A survey of counted loops: FORTRAN

Programming Languages, Summary CSC419; Odelia Schwartz

Example: Haskell algebraic data types (1)

Lecture 13: Expression Evaluation

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

CS111: PROGRAMMING LANGUAGE II

CSC108: Introduction to Computer Programming. Lecture 1

CSE 452: Programming Languages. Outline of Today s Lecture. Expressions. Expressions and Control Flow

Scala : an LLVM-targeted Scala compiler

8/27/17. CS-3304 Introduction. What will you learn? Semester Outline. Websites INTRODUCTION TO PROGRAMMING LANGUAGES

CS 342 Lecture 7 Syntax Abstraction By: Hridesh Rajan

Com S 541. Programming Languages I

Programming Languages Semantics

Test 1 Summer 2014 Multiple Choice. Write your answer to the LEFT of each problem. 5 points each 1. Preprocessor macros are associated with: A. C B.

Lecture 7 Tao Wang 1

Key Differences Between Python and Java

9/5/17. The Design and Implementation of Programming Languages. Compilation. Interpretation. Compilation vs. Interpretation. Hybrid Implementation

Control Flow. Stephen A. Edwards. Fall Columbia University

Lecture 01 & 02 Computer Programming

Introduction to Programming Using Java (98-388)

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

Transcription:

Principles of Programming Languages Lecture 22 Wael Aboulsaadat wael@cs.toronto.edu http://portal.utoronto.ca/ Acknowledgment: parts of these slides are based on material by Diane Horton & Eric Joanis @ UoT References: Scheme by Dybvig PL Concepts and Constructs by Sethi Concepts of PL by Sebesta ML for the Working Prog. By Paulson Prog. in Prolog by Clocksin and Mellish PL Pragmatics by Scott 1

Components of an Imperative Language Data types Variables, operators & Expressions Iteration construct Branching construct Subprogram construct Container construct 2

Syntactical variations - Prototype - Implementation Container Construct Binary variations Application Library Component 3

Syntactical variations Container Construct - Prototype & implementation - Implementation C/C++ separates between prototype and implementation of code class HelloWorld { public: HelloWorld(); ~HelloWorld(); public: //Methods: void on_button_clicked(); protected: //Attributes Button ok_button; }; hello.h #include "hello.h" HelloWorld::HelloWorld(){ //. } HelloWorld::~HelloWorld(){ //.. } HelloWorld::ok_clicked(){ //. } hello.cpp 4

Container Construct C/C++ separates between prototype and implementation of code Decreases writability: have to maintain 2 files per class/set of functions. class HelloWorld { public: HelloWorld(); ~HelloWorld(); public: //Methods: void printmessage(); protected: //Attributes Button ok_button; }; #include "hello.h" HelloWorld::HelloWorld(){ //. } HelloWorld::~HelloWorld(){ //.. } #include "hello.h" int main(){ HelloWorld::printMessage(){ //. } hello.h hello.cpp App.cpp } HelloWorld hello; hello.printmessage (); 5

Container Construct Languages which separates prototype from implementation often force programmer to handle circular references which will break compilation! App.cpp #include x.h #include y.h" y.h #include x.h" #include y.h" x.h y.cpp #include y.h" #include x.h" x.cpp 6

Container Construct Languages which separates prototype from implementation often force programmer to handle circular references which will break compilation! ifdef ensures that a file is included by the compiler only once #ifndef HELLO #define HELLO class HelloWorld { public: HelloWorld(); ~HelloWorld(); public: //Methods: void printmessage(); protected: //Attributes Button ok_button; }; #endif hello.h 7

Binary variations: Library Container Construct A library is a set of functions packaged in one file. No main function/method. It can only be used from a program. You can t run it! Libraries come in 2 variations: static or dynamic. 8

Binary variations: Library Container Construct Static library program source library 1 library 2 - Windows:.lib Linux:.a (e.g. /usr/local/lib ) - Linked with the program during compilation. - Calls to function in lib is replaced with relative address of func in binary Compiler One executable containing all binaries - Compiler must support generating a static library from a set of functions (with no main function/method) library source Compiler.a/.lib library binary file 9

Binary variations: Library Container Construct Dynamic library - Windows:.dll (c:/windows/system32), Linux:.so (/usr/lib), Mac:.dylib - Loaded during runtime into the program space Executable Memory Total memory for the executable x.dll x.dll library source Harddisk - OS provides function to load library (Windows: LoadLibrary("x.dll")) - Compiler must support a mechanism to do the following: Compiler.so/.dll library binary file 10

Container Construct Binary variations: Component Similar to library with meta information is added to binary to enable reflection Naming convention is enforced to identify set/get methods Examples: Microsoft COM components, JavaSoft JavaBeans Component source Compiler Executable with meta information about code binary metainf Refer to reflection lecture for an example how Java supports this. 11

Container Construct Binary variations: application Can either be native or virtual-machine based Java/Microsoft.Net platform vs. native C/C++/Fortran app Native Application Virtual-Machine Application Native Assembly Code Intermediate assembly-like code E.g. cprog.exe Run-time for platform Y Run-time for platform X E.g. jvm javaprog 12

Components of an Imperative Language Data types Variables, operators & Expressions Iteration construct Branching construct Subprogram construct Container construct 13

Control Statements: iteration The repeated execution of a statement or compound statement is accomplished either by iteration or recursion, here we look at iteration, because recursion is subprogram control. An Iteration statement is one that causes a statement or collection of statements to be executed zero, one or more times. E.g. for( nindex = 0; nindex < 10; nindex++) // Java + C + C++ General issues: How is iteration controlled? Where is the control mechanism in the loop? Types of iteration constructs: Counter controlled loops Logically controlled loops User controlled loops Data structures controlled loops 14

Control Statements: iteration cont d User controlled loops: Test the condition in the middle of the loop and break if false Language must provide break, continue or similar statement, why? E.g. loop // Ada if somevariable < somevalue exit end loop while( true ){ // C++ if(somevariable < somevalue) break; } 15

Control Statements: iteration cont d Logically controlled loops: Execution of loop continues as long as certain logical condition is true Types: Pretest: Test the condition before entering the loop. Might not execute the loop. E.g. while( x < 10 ) do while(x < 10) // Pascal // C/C++/Java Posttest: Test the condition at the end of the loop. Execute loop at least once. E.g. repeat.. until ( x < 10 ); // Pascal do{.. }while (x < 10); // C 16

Control Statements: iteration cont d Counter controlled loops: Execution of loop n times Loop variable: memory location where the count value is maintained Loop parameters: initial, terminal and stepsize E.g. Ada for count in 1..10 loop sum := sum + count; end loop; Pascal for x := 1 to100 do for x := 100 downto 1 do C/C++/Java for (Index = 0 ; nindex < 10; nindex++ ) for (nindex1 = 0, nindex2 = 0; nindex1 < 10 & nindex2 < 10; nindex1++, nindex2++) 17

Control Statements: iteration cont d Data structures controlled loops: Loop is controlled by the number of elements in a data structure Control mechanism is a call to a function that returns the next element in some chosen order The loop variable is assigned the current element in the data structure Clu was the first to introduce for i in from_to_by(first, last, step) do end C copied for ( ptr=header; ptr!= NULL ; ptr=ptr->next ) { // C for-loop can be used to create user defined iterator } head ptr 18

Control Statements: iteration cont d Data structures controlled loops: Most new languages include this type of loop Enhances readability & writeability // Perl @names = ( John, Ted, Lee ); foreach $name(@name) { print $name; } #Python lst = [10,20,30] for num in lst: print num 19

Control Statements: selection Provides the means of choosing between two or more execution paths in a program Issues: What is the form and type of the control expression? What is the selectable segment form? How should the meaning of nested selectors be specified? Types: Single way selection Two-way selection N-way selection 20

Control Statements: selection cont d Single way selectors: If the boolean expression is evaluated to true, do something. E.g. IF (x < 10 ) // Fortran I print x Problem: can select one value only! 21

Control Statements: selection cont d Two way selectors: Pick one out of two execution paths <statement> could be single or compound E.g. if( x < 10 ) then // Pascal <statement> else <statement> 22

Control Statements: selection cont d N-way selectors: Pick one out of n execution paths E.g. Pascal C/C++/Java 23

Control Statements: issues to consider What are the selection statements in the language? What is the form and type of the expression that controls the selection? Can a single statement, a sequence of statements, or a compound statement be selected? How should unrepresented selector expression values be handled, if at all? Is execution flow through the structure restricted to include just a single selectable segment? What are the repetition statements in the language? Type and scope of the loop variable? Value of the loop variable at loop termination? Can the loop variable be modified in the loop body? Should the test for loop completion be at the top or bottom of the loop? Should the loop parameters be evaluated only once, or once for every iteration? 24

What Did We Cover? Logic Language Prolog Functional languages Scheme and ML Principles of Imperative Programming Languages Grammar Data Types Variables, operators & expressions Iteration constructs Branching constructs Subprogram constructs Container constructs 25

Exam 4 Questions (100): Prolog(30), Scheme(20), ML(15), PL Concepts(35) 3 Hours. 15 pages (including cover page, empty page and an aid page at the end!) Examination Type D: Printed lecture slides and textbooks permitted. No other aids are allowed. Sample questions. Office hours before exam. Stay tuned! Review the programming language (s) you have learned before 26