Chapter 12.  Programs written in imperative programming languages consist of ◦ A program state ◦ Instructions that change the program state  Program.

Slides:



Advertisements
Similar presentations
Agenda Definitions Evolution of Programming Languages and Personal Computers The C Language.
Advertisements

Imperative Programming
Programming Paradigms and languages
Programming Languages and Paradigms
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
PLLab, NTHU Cs2403 Programming Languages Implementation Issues Cs2403 Programming Language Spring 2005 Kun-Yuan Hsieh.
1 ) Definition 2) Note on structured and modular programming, and information hiding 3) Example imperative languages 4) Features of imperative languages.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 12 Imperative Programming I really hate this.
High-Level Programming Languages
Reasons to study concepts of PL
ISBN Chapter 1 Preliminaries. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter 1 Topics Motivation Programming Domains.
1 Lecture 1  Getting ready to program  Hardware Model  Software Model  Programming Languages  The C Language  Software Engineering  Programming.
Programming Languages Structure
Chapter 8 High-Level Programming Languages Nell Dale John Lewis.
ISBN Lecture 01 Preliminaries. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Lecture 01 Topics Motivation Programming.
Chapter 2: Impact of Machine Architectures What is the Relationship Between Programs, Programming Languages, and Computers.
Chapter 8 The Tower of Babel. Chapter Outline Procedural languages Fortran, COBOL, PASCAL, C, Ada Object-oriented programming Special-purpose languages.
Guide To UNIX Using Linux Third Edition
Chapter 1 Program Design
ISBN Chapter 1 Topics Motivation Programming Domains Language Evaluation Criteria Influences on Language Design Language Categories Language.
Programming Language Concepts
CS102 Introduction to Computer Programming
Principles of Programming Chapter 1: Introduction  In this chapter you will learn about:  Overview of Computer Component  Overview of Programming 
Introduction to High-Level Language Programming
BIT Presentation 6. Contents GENERATIONS OF LANGUAGES COMPILERS AND INTERPRETERS VIRTUAL MACHINES OBJECT-ORIENTED PROGRAMMING SCRIPTING LANGUAGES.
Overview. Copyright © 2006 The McGraw-Hill Companies, Inc. Chapter 1 Overview A good programming language is a conceptual universe for thinking about.
CSE 425: Intro to Programming Languages and their Design A Few Key Ideas No particular language is a prerequisite for this course –However you should be.
1.1 1 Introduction Foundations of Computer Science  Cengage Learning.
CS 355 – Programming Languages
C++ Programming. Table of Contents History What is C++? Development of C++ Standardized C++ What are the features of C++? What is Object Orientation?
An Introduction Chapter Chapter 1 Introduction2 Computer Systems  Programmable machines  Hardware + Software (program) HardwareProgram.
COMPUTER SOFTWARE Section 2 “System Software: Computer System Management ” CHAPTER 4 Lecture-6/ T. Nouf Almujally 1.
Imperative Programming
Simple Program Design Third Edition A Step-by-Step Approach
High level & Low level language High level programming languages are more structured, are closer to spoken language and are more intuitive than low level.
An intro to programming. The purpose of writing a program is to solve a problem or take advantage of an opportunity Consists of multiple steps:  Understanding.
CS 363 Comparative Programming Languages
1 Computing Software. Programming Style Programs that are not documented internally, while they may do what is requested, can be difficult to understand.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages.
Computer Concepts 2014 Chapter 12 Computer Programming.
Copyright © 2007 Addison-Wesley. All rights reserved.1-1 Reasons for Studying Concepts of Programming Languages Increased ability to express ideas Improved.
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Guide to Programming with Python Chapter One Getting Started: The Game Over Program.
CE Operating Systems Lecture 3 Overview of OS functions and structure.
Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
1 Text Reference: Warford. 2 Computer Architecture: The design of those aspects of a computer which are visible to the programmer. Architecture Organization.
Theory of Programming Languages Introduction. What is a Programming Language? John von Neumann (1940’s) –Stored program concept –CPU actions determined.
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
Programming in C++ Dale/Weems/Headington Chapter 1 Overview of Programming and Problem Solving.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Chapter 1 Basic Concepts of Operating Systems Introduction Software A program is a sequence of instructions that enables the computer to carry.
Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT,
Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.
Evolution of C and C++ n C was developed by Dennis Ritchie at Bell Labs (early 1970s) as a systems programming language n C later evolved into a general-purpose.
Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the program development.
PROGRAMMING FUNDAMENTALS INTRODUCTION TO PROGRAMMING. Computer Programming Concepts. Flowchart. Structured Programming Design. Implementation Documentation.
Introduction to Computer Programming Concepts M. Uyguroğlu R. Uyguroğlu.
Victoria Ibarra Mat:  Generally, Computer hardware is divided into four main functional areas. These are:  Input devices Input devices  Output.
Computer Organization and Architecture Lecture 1 : Introduction
Concepts of Programming Languages
Concepts of Programming Languages
Why study programming languages?
PROGRAMMING LANGUAGES
CSCI-235 Micro-Computer Applications
Programming Languages 2nd edition Tucker and Noonan
Programming Languages 2nd edition Tucker and Noonan
Introduction to Computer Programming
Principles of Programming Languages
Presentation transcript:

Chapter 12

 Programs written in imperative programming languages consist of ◦ A program state ◦ Instructions that change the program state  Program instructions are “imperative” in the grammatical sense of imperative verbs that express a command

 Commands in an imperative language are similar to the native machine instructions of traditional computer hardware – the von Neumann-Eckley model.  John von Neumann: first person to document the basic concepts of stored program computers.  Famous Hungarian mathematician  Became interested in computers while participating in the development of the hydrogen bomb.  Eckert and Mauchly also considered this approach as early as 1944.

 Consists of the following components:  A memory unit: stores both data & instructions  Random access  Internally, data and instructions are indistinguishable  A CPU, consisting of  A calculating unit (the ALU)  A control unit (the CU)  The I/O subsystem  Components are connected by the system bus  vonNeumann bottleneck: connection betw CPU and memory: small, slow; limits the speed at which work can be done. Can’t fetch instruction & data in parallel.

 It means the computer has an instruction set, as opposed to hard-wired programs ◦ Earlier computers had fixed programs: they were hardwired to do one thing; changing a program meant wiring another board.  And … it meant that instructions could be operated on just like data – von Neumann suggested they could be modified during execution based on program results. ◦ Self-modifying programs are no longer considered a good idea!  “First Draft of a Report on the EDVAC”,June, 1945

 Alternative to von Neumann architecture (sometimes called the Princeton architecture)  Also stored program, but separate memory buses for instructions & data  Avoids the infamous “von Neumann bottleneck” – inability to fetch instructions & data simultaneously.

 First imperative languages: assembly languages  : Fortran (FORmula TRANslator) John Backus developed for IBM 704  Late 1950’s: Algol (ALGOrithmic Language)  1958: Cobol (COmmon Business Oriented Language) Developed by a government committee; Grace Hopper very influential.

 Informally, a language is said to be Turing complete if it can be used to implement any algorithm.  Central to the study of computability  Alan Turing: A British mathematician, logician, and eventually computer scientist.

 Imperative languages are Turing complete if they support integers, basic arithmetic operators, assignment, sequencing, looping and branching.  Modern imperative languages have a more complete set of features, including ◦ Procedures and functions ◦ Error and exception handling ◦ Library support for data structures ◦...

Flowchart Used to model imperative programs Based on the three control statements that are essential to have Turing machine capability Precursor of UML and other modern techniques

 Nicholas Wirth described [imperative] programs as being “algorithms plus data structures”.  Algorithms become programs through use of procedural abstraction and stepwise refinement. (functions = procedures)  Libraries of reusable functions support the process  Imperative programming + procedures = procedural programming.

 In general, abstraction allows us to think about what is being done, not how it is implemented.  Procedural abstraction allows the programmer to be concerned mainly with the interface between a function (procedure) and what it computes, ignoring the details of how the computation is accomplished.  Postpone details of solution.

 Stepwise refinement (also called functional decomposition) uses procedural abstraction to develop an algorithm from its most general form into a specific implementation through successive layers of abstraction.  Start with a description of what the program should do; repeatedly break the problem into smaller parts until the sub-problems can be expressed in terms of the primitive operations and data types in the language.

 The Problem: read in a set of values and compute the average. The number of input values is not known.  First set of steps: ◦ Declare & initialize variables ◦ Input the values ◦ Compute sum & average  Refinements???

 A disciplined approach to imperative program design.  Uses procedural abstraction and top-down design to identify program components (also called modules or structures)  Program control flow is based on decisions, sequences, loops, but…  Does not use goto statements  Modules are developed, tested separately and then integrated into the whole program.

 A consequence of procedural abstraction  Existing components can be integrated into new programs; focus only on the interface  In imperative programs, reusable components are usually data structures + functions to operate on them. ◦ Typically collected into libraries.

 Statements are commands ◦ Command order is critical to correct execution; ◦ Programmers control all aspects: algorithm specification, memory management, variable declarations, etc  They work by modifying program state (program variables+ registers + temp variables …)  Statements closely reflect machine language instructions.

 They are usually "typed” - either statically or dynamically.  Statement types: ◦ Declarations, Assignment, Conditionals, Loops.. ◦ I/O and error handling mechanisms.  A method of grouping all of the above into a complete program - (program composition); e.g., main program + functions

 Destructive assignment statements: state changing operations  General format: target = expression  Assignment operators : = or :=  Based on machine operations such as MOV or STO

 Format: target = expression  Evaluate expression to get a single value  Copy the expression value to the target.  Pure imperative programs implement copy semantics (as opposed to reference semantics)

 Expressions represent a value and have a type.  Issues: operator precedence, operator overloading, casting and type conversion, etc.  These issues are covered in Chapter 5 & 7.  Simple arithmetic expressions include machine language (ML) arithmetic operators  Logical operators are based on similar ML instructions

 Machine language supports data types indirectly through different instructions: integer add/sub/mul/div versus floating point fadd/fsub/fmul/fdiv, for example.  Internally, no visible way to distinguish integers from floats from characters from instructions.

 Imperative programming is the oldest programming paradigm  It is based on the von Neumann-Eckley model of a computer  It works by changing the program state through assignment statements  Procedural abstraction & structured programming are its design techniques.

 Focus on procedural abstraction  Realization that data is important too  How to combine data and procedures?  Abstract Data Types  Increasing emphasis on information hiding, encapsulation, etc.  Result: Object-oriented languages: imperative operations plus OO design emphasis

 C  Ada  Perl  Python

 “ C was originally designed for and implemented on the UNIX operating system on the DEC PDP- 11, by Dennis Ritchie. The operating system, the C compiler, and essentially all UNIX applications programs (including all of the software used to prepare this book) are written in C.... C is not tied to any particular hardware or system, however, and it is easy to write programs that will run without change on any machine that supports C. ” The C Programming Language, Kernigan & Ritchie, 1978  (Italics mine)

 Multics, an advanced OS being from Bell Labs; ran on specialized CPU hardware.  Ken Thompson and Thomas Ritchie developed UNIX as a simpler, platform-independent OS.  C was invented and used to write UNIX, as well as most of the accompanying utilities, including the C compiler.

 UNIX was targeted toward minicomputers – 16 bit machines with limited memory.  Required: good, efficient code, small compiler  Solution: make C relatively low-level (closer to assembly language than other high-level languages)

 Many C features were adapted directly from hardware instructions ◦ Examples: ++ and --  Although C++ and Java have replaced C in many areas it is still important as a language for writing operating systems, systems with memory or power limitations, and systems that value small code and efficiency over other factors; e.g., embedded systems. Usually ranks #1,2,3 in the list of most-used languages.

K&R C: version 1 int *a; // size = no. of elements... a=malloc(sizeof(int) * size); ANSI C: version 2 a=(int*)malloc(sizeof(int) * size); C++: version 3 a = new int[size]; Problems: varying levels of type checking; K&R C doesn’t check to see if type specified in sizeof matches type of a; ANSI C checks the cast, but not sizeof

 Advantages ◦ Relatively small language ◦ Can generate very efficient code ◦ Runs on virtually all platforms  Disadvantages ◦ Cryptic code ◦ Not type safe ◦ Other problems ml ml

 Development began in late 1970’s by DOD  Motivation: to save money on software by having one consistent language  Applications: ◦ Large command and control systems ◦ Embedded real-time systems  Developed by committee, competition  Standardized in 1983

 Very large – compilers started at 250K lines, compared to average of 8K-12K for Pascal, 15K to 25K for Modula. ◦ PCs were just becoming popular – couldn’t host Ada compilers  OO languages were beginning to revolutionize language design/Ada had less powerful ADT features ◦ Added OO capabilities in Ada 95 version

 Why are C/C++ and other languages more popular than Ada for embedded systems?  Ada is a more secure language due to run- time error checking for various errors; e.g., ◦ Out-of-bounds array indexes ◦ Buffer overflow ◦ Accesses to unallocated memory  C/C++ is smaller and faster

 Ada fell out of favor in the 90’s  No longer mandated in DOD projects  According to text, there is currently a resurgence of interest in the language due to ◦ Unreliability of commercial, off-the-shelf software ◦ Development of new versions of Ada  Spark Ada  GNAT (Ada) compiler – part of the GNU collection  Versions to compile Ada to Java byte code or to the Common Language Runtime (CLR) for.NET platforms. ◦ Most lists of popular-frequently used languages don’t rate it very highly.

 Ada packages are a way to encapsulate functions and data into a single unit – a form of ADT similar to a class  A package has a specification and a body  A specification has a private and public part – it includes subprogram definitions, data declarations, constant definitions, etc.  The body contains the implementation of the subprograms

Generic – Fig 12.9 is the specification type element is private; type list is array(natural range <>) of element ; function " > "( a,b: element ) return boolean; package sort_pck is procedure sort (in out a : list); end sort_pck; package integer_sort is new generic_sort( integer, “>”);

package sort_pck is procedure sort (in out a : list) is begin for i in a'first.. a'last - 1 loop for j in i+1.. a'last loop if a(i) > a(j) then declare t : element; begin t := a(i); a(i) := a(j); a(j) := t; end; end if; Fig is the implementation of the previous specification

 More modern than C or Ada  Classified as a scripting language, but is a full-featured general purpose language ◦ Early on, scripting languages automated job control tasks; e.g., UNIX Bourne shell scripts  Most scripting languages are interpreted  Usually interpreted, Perl is sometimes compiled to a form of byte code and then interpreted

 Strength: support for regular expressions  Many irregularities in Perl: for example, regexps are not first class objects, meaning they can’t be assigned to variables or passed as parameters.  First class object: a language element that can be used without restriction – that has all the uses of any other element in that particular language ◦ C and C++ don’t permit the creation of functions at runtime, so functions aren’t considered first-class objects.

 As a scripting language, used to “glue” applications together ◦ take output from one application and reformat into desired input format for a different application. ◦ Useful for system administration functions  Also used for Web applications, graphics processing, & many others. ◦ “The Swiss Army knife of languages”  Comparable languages: Python, Ruby, Tcl  We’ll focus on Python.