Programming Languages

Slides:



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

Senem Kumova Metin Introduction to Programming CS 115 Introduction to Computing PART I : Computer Basics PART II: Introduction to Computing/Programming.
Programming Languages and Paradigms
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
The Assembly Language Level
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
CPSC Compiler Tutorial 9 Review of Compiler.
COSC 120 Computer Programming
Engineering Problem Solving With C++ An Object Based Approach Fundamental Concepts Chapter 1 Engineering Problem Solving.
High-Level Programming Languages
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Computers: Tools for an Information Age
Chapter 8 High-Level Programming Languages Nell Dale John Lewis.
CSE S. Tanimoto Syntax and Types 1 Representation, Syntax, Paradigms, Types Representation Formal Syntax Paradigms Data Types Type Inference.
Guide To UNIX Using Linux Third Edition
C++ fundamentals.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
CSE 341, S. Tanimoto Concepts 1- 1 Programming Language Concepts Formal Syntax Paradigms Data Types Polymorphism.
Chapter 1: Introduction to Visual Basic.NET: Background and Perspective Visual Basic.NET Programming: From Problem Analysis to Program Design.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Chapter 8 High-Level Programming Languages (modified by Erin Chambers)
High-Level Programming Languages: C++
CIS Computer Programming Logic
Introduction Computer science is the discipline that seeks to build a scientific foundation for a variety of topics. Computer science provides the underpinnings.
Chapter 1: Introduction to Computers and Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Programming Languages: History & Traditional Concepts CSC 2001.
Software Overview. Why review software? Software is the set of instructions that tells hardware what to do The reason for hardware is to execute a program.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages.
Chapter 1 Introduction to Computers and C++ Programming Goals: To introduce the fundamental hardware and software components of a computer system To introduce.
Computer Concepts 2014 Chapter 12 Computer Programming.
Chapter 9 Programming Languages
Chapter 6 Programming Languages. © 2005 Pearson Addison-Wesley. All rights reserved 6-2 Chapter 6: Programming Languages 6.1 Historical Perspective 6.2.
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Chapter 6 Programming Languages (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Lecture on Programming Languages
Computer Programs and Programming Languages What are low-level languages and high-level languages? High-level language Low-level language Machine-dependent.
Chapter 6 Programming Languages © 2007 Pearson Addison-Wesley. All rights reserved.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Copyright © 2015 Pearson Education, Inc. Chapter 6: Programming Languages.
1. 2 Preface In the time since the 1986 edition of this book, the world of compiler design has changed significantly 3.
Overview of Previous Lesson(s) Over View  A program must be translated into a form in which it can be executed by a computer.  The software systems.
Theory of Programming Languages Introduction. What is a Programming Language? John von Neumann (1940’s) –Stored program concept –CPU actions determined.
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
Chapter 8 High-Level Programming Languages. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Chapter 9 ProgrammingLanguages.  Describe the evolution of programming languages from machine language to high-level languages.  Understand how a program.
1 Asstt. Prof Navjot Kaur Computer Dept PRESENTED BY.
PROGRAMMING FUNDAMENTALS INTRODUCTION TO PROGRAMMING. Computer Programming Concepts. Flowchart. Structured Programming Design. Implementation Documentation.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
PROGRAMMING (1) LECTURE # 1 Programming and Languages: Telling the Computer What to Do.
Victoria Ibarra Mat:  Generally, Computer hardware is divided into four main functional areas. These are:  Input devices Input devices  Output.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
CSCI-235 Micro-Computer Applications
Ch. 7 Programming Languages
Representation, Syntax, Paradigms, Types
Chapter 6: Programming Languages
Representation, Syntax, Paradigms, Types
High Level Programming Languages
Chapter 6: Programming Languages
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Presentation transcript:

Programming Languages Chapter 6 Programming Languages

Chapter 6: Programming Languages 6.1 Historical Perspective 6.2 Traditional Programming Concepts 6.3 Procedural Units 6.4 Language Implementation 6.5 Object Oriented Programming 6.6 Programming Concurrent Activities 6.7 Declarative Programming

Programming Languages Complex software system like OS would be developed impossibly using machine instructions directly. Programming languages are designed for humans to express the algorithm easily, yet are convertible to the machine instructions on the target platform. As such, the details of registers, memory addresses, and even machine cycles are out of our focus. We may then concentrate on the properties of the problems in question.

Historical Perspective Evolution of computer languages:􀂄 Micro-instructions: Inside CPU, they implement each machine instruction.􀂄 Machine language: binary form direct controls the hardware.􀂄 E.g., “4032”: R3⇐R2, very hard to read & write the programs.􀂄 Assembly language: mnemonic form of the machine language.􀂄 E.g., “MV R3 R2”is used to refer to “4032”.􀂄 Descriptive name like “Price” is used to represent some memory address.􀂄 “LD R5, Price”(156C) load the content in the address “Price”0X6C to R5.􀂄 Mnemonic ADDI & ADDF refer to integer & floating-point additions.􀂄

Historical Perspective This is a major syntax of revolution in programming.􀂄 Assemblers are the programs which translate the program written in an assembly language to the target machine language.􀂄 Shortages: still machine-dependent, and too naïve to develop large-scaled programs.

Historical Perspective High-level language: English like language, for more productive development.􀂄 FORTRAN (Formula Translator) for scientific and engineering applications.􀂄 COBOL (Common Business-Oriented Language) from the U.S. Navy.􀂄 The approach is to identify a collection of high-level programming primitives to construct the software blocks. [in essentially the same spirit with pseudo codes]􀂄 Translators or compilers(initiated by Grace Hopper) are used to compile several machine instructions in the target machines to simulate the activity represented by a single high-level primitive. [interpreters]

Historical Perspective Interpreters—similar to translators except they executed the instructions as they were translated i.e. rather than producing a machine language copy of a program that would be executed later, an interpreter actually executed a program from its high level form Programming languages are constructed around a small set of primitives that could be expressed in a variety of natural languages with only simple modification to the translators. Natural language(English, German,.) evloved over time without formal grammatical analysis Formal language(programming language)—are precisely defined by grammars

Figure 6.1 Generations of programming languages

Second-generation: Assembly language A mnemonic system for representing programs Mnemonic names for op-codes Names for all registers Identifiers = descriptive names for memory locations, chosen by the programmer

Assembly language characteristics One-to-one correspondence between machine instructions and assembly instructions Programmer must think like the machine Inherently machine-dependent Converted to machine language by a program called an assembler

Assembly language example Machine language 156C 166D 5056 30CE C000 Assembly language LD R5, Price LD R6, ShippingCharge ADDI R0, R5 R6 ST R0, TotalCost HLT

Third generation language Uses high-level primitives Similar to our pseudocode in Chapter 5 Machine independent (mostly) Examples: FORTRAN, COBOL Each primitive corresponds to a short sequence of machine language instructions Converted to machine language by a program called a compiler

Machine Independence and Beyond Through compiling, programs can be portedeasily to any other machine.􀂄 The efficiency of compilers also determines the quality of executable programs.􀂄 A programming language may have several dialects over different machines.􀂄 Standardization: ANSI and ISO have adopted and published several standards for several popular programming languages. [to improve compatibility]􀂄 Human Centric: To achieve true machine independence, the machines need to be taught to ultimately understand the abstract concepts used in humans.􀂄 Creatively, the machines can learn to automate the algorithm discovery process.

Figure 6.2 The evolution of programming paradigms

Software Development Paradigms Functional: construct “black boxes”from elementary functions.􀂄 Goal: decompose the entire transformation with initial input and final output into nested complexes of simpler functions. [modular approach, support OO]� �LISP: Average ≡(Divide (Sum Numbers) (Count Numbers)); Min ≡(First (Sort List)).􀂄 Object-oriented: OOP uses active objects ≡data + methods.􀂄 Merits: modular design, message passing (good for CORBA), client/server (distributed) over the networks.􀂄 Imperative(procedural): the traditional approach to expand the CPU cycle for a more powerful sequence of commands (code blocks = macro instructions).􀂄 Goal: find an algorithm for the problem.􀂄

Software Development Paradigms 􀂄 Declarative: emphasizes more on what the problem is than how to solve it.􀂄 Goal: discover/implement a general problem-solving algorithm, and develop/write a precise statement of the problem. [SQL, formal logic, logic programming]

Figure 6.3 A function for checkbook balancing constructed from simpler functions

Software Development Paradigms �Which of the following is an example of a language that is based on the object-oriented paradigm?   A. LISP B. PROLOG C. C D. C++ Most machine languages are based on the   A. Imperative paradigm B. Declarative paradigm C. Functional paradigm D. Object-oriented paradigm Which of the following is not a type of statement found in a typical high-level imperative programming language? A. Imperative statement B. Exclamatory statement C. Declarative statement D. Comment statement

Traditional Programming Concepts �Generally, programming languages contains:􀂄 Declarative statements: define customized names to be used later in the program.􀂄 Imperative statements: describe the steps in the underlying algorithms.􀂄 Comments: enhance the readability of a program for handy explanation.

Traditional Programming Concepts �Declarative statements: 􀂄 Variables are identifiers or customized names defined to refer some memory locations.􀂄 Literals are fixed, predetermined values.􀂄 Constants are customized names initiated with fixed values. (meaningful literals)􀂄 Data type: variables or constants are defined with both names and data types. 􀂄 Primitive data types: integer, real, character, Boolean. (predefined in languages)􀂄 Derived data types: are customized data types, combinations of primitive ones.􀂄

Traditional Programming Concepts �Declarative statements: 􀂄 Homogeneous– a block of values of the same types heterogeneous –a block of data in which different elements can have different data type array (structure), queues, objects, or the hybrid

Traditional Programming Concepts �

Figure 6.4 The composition of a typical imperative program or program unit

Constants and literals �EffectiveAlt <- alitmeter + 645; In C const int Airaltitude = 645; In Java final int Airaltitude = 645; EffectiveAlt <- alitmeter + Airaltitude;

Figure 6.5 Variable declarations in C, C++, C#, and Java

Figure 6.6 A two-dimensional array with two rows and nine columns

Figure 6.7 Declaration of heterogeneous array

Figure 6.8 Control structures and their representations in C, C++, C#, and Java

Figure 6.9 The for loop structure and its representation in C++, C#, and Java

Traditional Programming Concepts Which of the following does not require a Boolean structure?   A. If-then-else statement B. While loop statement C. Assignment statement D. For loop statement Which of the following is not a control statement?

Procedural Unit A program = code blocks in a concatenated or hierarchical fashion.􀂄 Procedures are some code blocks (or programming units). Procedure’s header = the procedure name + other details:􀂄 Declaration statements to declare local variables used within the procedure.With the same names, local variables in different procedures are distinct.􀂄 Global variables in the parent block are unique in all children blocks.􀂄

Procedural Unit Imperative statements to describe the steps performed in the procedure.􀂄 Invoking the procedure:􀂄FORTRAN: “CALL GetNames”; in Ada/C/C++/Java/Pascall, “GetNames;”.

Figure 6.10 The flow of control involving a procedure

Figure 6.11 The procedure Project Population written in the programming language C

Procedural Unit Parameters are additional information passed to the procedure upon invocation.􀂄 Callers use the actual parameters, each of whose values mapped orderly to the corresponding one of formal parameters in the procedure.􀂄 “Passed by value”-copy: protect the data in callers, but inefficient if data is large.􀂄The changes on formal parameters do not reflect on actual parameters.􀂄 “Passed by reference”: the same data of callers are used directly in the procedure.􀂄It is favorable to sort the long list in place.

Procedure Demo(Formal) {Formal<-Formal +1 print formal} actual = 5 apply Demo to actual print actual Figure 6.12 Executing the procedure Demo and passing parameters by value

Figure 6.13 Executing the procedure Demo and passing parameters by reference

Procedural Unit Function—behaves like variables --similar to a procedure except that a value is transferred back to the calling program init as “ the value of the function”.

Figure 6.14 The function CylinderVolume written in the programming language C

Procedural Unit Input/Output Statements: call the default procedures for the program I/O In Pascal, “ readln(Value) ”retrieves a value from keyboard to the variable Value;“ writeln(Value) ”outputs the value of the variable Value to the monitor In C, “ scanf ”& “ printf(“The price is %d.\n”, Price);”functions are used instead In C++, ready-made “ cin >> value ”& “ cout<< Price; ”objects represent the I/O devices.

Language Implementation The code blocks are ultimately translated into the sequence of machine instructions. Translation Process:􀂄 The source program is the program in the original programming language.􀂄 The object program is the translated version resulted from the translation process:

Figure 6.15 The translation process

Language Implementation Lexical analysis by lexical analyzer: Recognize which strings of symbols representing a single entity E.X. const alt = 153; Classify entity by numeric values, words, arithmetic operators, etc Generate a bit pattern known as tokens􀂄 The entire program byte streams are recognized as a series of tokens of various types.( if a < b then a++; else b++;)�

Language Implementation Parsing by parser:􀂄 Tokens regarding to comments are skipped.Parser groups tokens together into statements according to the grammatical rules(syntax).􀂄 Early programming languages (fixed-format languages) insist the particular positions for statements; most languages today are free-format using delimiters like semicolons, key words, reserved words, etc. to delineate statements.􀂄 Grammatical rules can be illustrated by syntax diagrams.􀂄 Code generation by code generator:

Figure 6.16 A syntax diagram of our if-then-else pseudocode statement

Figure 6.19 Two distinct parse trees for the statement if B1 then if B2 then S1 else S2

Ambiguities Avoidance Ambiguities rise when the syntax rules yields two or more distinct parse trees for the same series of tokens.􀂄 The single statement “if B1 then ifB2 thenS1 elseS2”reveals such flaw. Avoidance: (uniqueness)􀂄 Using parentheses to specify the precise join. [programmers]􀂄 Adjusting the syntax rules for default join (nearest). [languagedesigners]

Figure 6.17 Syntax diagrams describing the structure of a simple algebraic expression

Figure 6.18 The parse tree for the string x + y x z based on the syntax diagrams in Figure 6.17

Code Generation To analyze imperative statements, the parser first processes the declarative statements and records each user-defined identifier in the symbol table. For variables, their data types and data structures are booked.􀂄 For procedures, their return types and lists of formal parameters are stored. 􀂄�

Code Generation As an example, “Total ←Price + Tax”is translated into integer or floating-point additions according to the types of variables Price and Tax.􀂄 An error is raised or additional type conversion codes are prefixed when both data types are different(incompatible or compatible). Such implicit conversion is called coercion.􀂄 Strongly typed languages disallow coercion and treat them as errors.

Figure 6.20 An object-oriented approach to the translation process

Linking and Loading One or more object programs need to be connected together as an executable.􀂄It is done by the linker to produce the executable file (load module).􀂄 Object programs can be developed by another teams, system library, or any third party.

Figure 6.21 The complete program preparation process

Objects and Classes Object = active program unit containing both data and procedures Class = a template for all objects of the same type An Object is often called an instance of the class.

Components of an object Instance variable = variable within an object Method = function or procedure within an object Can manipulate the object’s instance variables

Figure 6.22 The structure of a class describing a laser weapon in a computer game

Class & Object Create an object of “type” and assign it to Laser 1. LaserClass Laser1 = new LaserClass(); --declare variable Laser1 to be of the type LaserClass but also creates a new object using the LaserClass template Activate the approriate methods Laser1.fire();

Constructor = special method to initialize a new object instance LaserClass Laser1(50), Laser2(100);

Encapsulation Encapsulation = a way of restricting access to the internal components of an object Private—only object itself can access it Public—accessible from outside of objects

Figure 6.24 Our LaserClass definition using encapsulation as it would appear in a Java or C# program

Additional object-oriented concepts Inheritance: allows new classes to be defined in terms of previously defined classes Class RechargeableLaser extends LaserClass { void fire () } RechargeableLaser Laser3, Laser4; Polymorphism: allows method calls to be interpreted by the object that receives the call Laser3.fire();

Programming concurrent activities Parallel or concurrent processing = simultaneous execution of multiple processes True concurrent processing requires multiple CPUs Can be simulated using time-sharing with a single CPU To facilitate such demands, modern programming languages provide syntax for expressing the semantic structures.􀂄 In Ada, an activation is called a task, while in Java, it is a thread.

Figure 6.25 Spawning processes

Interaction between processes Mutual exclusion = a method for ensuring that data can be accessed by only one process at a time Approach:From process side Monitor = a data item augmented with the ability to control access to itself

Declarative programming provides a general problem-solving algorithm around which declarative programming can be constructed over the logic deduction. Resolution = combining two or more statements to produce a new, logically equivalent statement Example: (P OR Q) AND (R OR Q) resolves to (P OR R) Resolvent = a new statement deduced by resolution Clause form = statement whose elementary components are connected by the Boolean operation OR E.X.: P OR Q P->Q  Q OR ~P

Figure 6.26 Resolving the statements (P OR Q) and (R OR ¬Q) to produce (P OR R) If the original statement is true, then the resolvent must also Be true. If Q is true, then R must be true, but if Q is false then P must be true. Regardless Q is true or false, either P or R must Be true

Figure 6.27 Resolving the statements (P OR Q), (R OR ¬Q), ¬R, and ¬P

Prolog(Programming in Logic) Fact = predicateName(arguments). Example: parent(bill, mary). Rule = conclusion :- premise. :- means “if” Example: wise(X) :- old(X). old(x)-> wise(x) Example: faster(X,Z) :- faster(X,Y), faster(Y,Z). ((faster(x,y) and faster(Y,z))-> faster(x,z) All statements must be fact or rules.