1 / 36

Chapter 5: Programming Languages

Chapter 5: Programming Languages. Just as Operating Systems, Computer Hardware, and Algorithm Discover processes have evolved, so have programming languages

avidan
Download Presentation

Chapter 5: Programming Languages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 5: Programming Languages • Just as Operating Systems, Computer Hardware, and Algorithm Discover processes have evolved, so have programming languages • Here we study the evolution and classification of programming languages, and the constructs that we find in all (or most) programming languages • In classifying the languages, we see different programming paradigms, imperative, functional, object-oriented and declarative

  2. History and Evolution • Programs are represented in Machine Language • instructions are op codes and represented as binary (or hexadecimal) codes • data are represented either as register numbers or main memory locations • First generation computers were most commonly programmed in this fashion • Very difficult -- keeping track of storage locations, knowing proper op codes • Programming required a lot of patience and led to many erroneous programs that either did not run at all or did not work properly

  3. Assembly Language • During the mid 1950’s (still in the 1st generation), programmers realized that they could • substitute mnemonics (abbreviations) for op codes • and use labels for memory locations • Which made it easier to write a program • Mnemonics and labels make up assembly language • A program must now be translated from assembly to machine language via a process called assembling, using a program called an assembler • While assembly was developed during the first generation, it is sometimes erroneously referred to as a second-generation programming language

  4. 1556 166D 5056 306E C000 This is the machine language “add” program from chapter 2 LD R5, PRICE LD R6, TAX ADDI R0, R5, R6 ST R0, TOTAL HLT Notice the use of LD for “load”, ADDI for “integer addition”, ST for “store” and HLT for “halt” Machine vs. Assembly Language

  5. Machine Translation • Assembly language offered programmers an easier way to specify programs • But it also showed that machine translation, from one form to another, was possible • Why not take this idea further? • Converting from Assembly to Machine language is a one-to-one mapping • one Assembly Language mnenomic becomes one Machine Language op code • could we specify a more elaborate instruction that is translated into several Machine Language instructions?

  6. High Level Languages • By the late 1950’s, this more complex translation idea was being explored • FORTRAN -- FORmula TRANslator was developed by IBM as the first high level language, to be used for scientific and mathematical applications • FORTRAN programs are expressed in a mathematical notation using English words and variables • A FORTRAN program is then compiled into assembly language by a compiler followed by assembling to machine language • COBOL -- COmmon Business Oriented Language was developed by the US Navy for business applications

  7. Machine Independence • Another advantage to high level programming languages is that they are machine independent • All you need is a compiler for the language on the given machine and a program written on another computer can be compiled on the given computer and executed • To help promote machine independence, organizations such as ANSI were created to form standardized languages across machine platforms (such as ANSI C) • Unfortunately, while this idea promoted High Level Languages, there are always certain machine dependent features such as I/O, memory management and interrupts, making it so that most high level programming languages are not truly machine independent

  8. While the first high level languages were developed in the first generation, these languages have been dubbed third-generation languages In the 1960’s and through the 1980’s, there have been a great number of languages, each provided for its own purpose We study the evolution of these languages in 3336 A brief look follows Algol -- European fusion of FORTRAN and COBOL PL/I -- IBM fusion of FORTRAN, COBOL and Algol LISP -- for AI research C, C++ -- portable and system programming Pascal and BASIC -- educational languages Ada -- developed for the DoD Simula -- simulation language Smalltalk -- early Object-oriented language, descendent of Simula Prolog -- declarative AI language More High Level Languages

  9. Programming Paradigms • Just as there has been a great proliferation of programming languages, there have been shifts in programming style and development • These shifts are characterized as different programming paradigms • Imperative (or procedural) programming • Functional programming • Object-oriented programming • Declarative programming • With each new paradigm comes new languages that fit that paradigm

  10. Imperative Programming • Basic idea is that code is used to manipulate data stored in memory (or elsewhere) • Follows the fetch-decode-execute cycle • Modular programming was developed as a proper way to perform imperative programming, and so it is often called procedural programming • Most languages fit this paradigm, at least to some extent: • machine, assembly, FORTRAN, COBOL, Algol, PL/I, C, Pascal, C++, Ada, BASIC

  11. Functional Programming • Similar to Imperative programming, but based entirely around the mathematical notion of a function • Every instruction is a function that returns a value to be used by another function • Promotes modularity even more than imperative programming • a function is thought of as a black box and used by other functions without having to know how the function works • LISP is the predominant functional language, but others include ML and Miranda

  12. Object-Oriented Programming • From AI research • it is important to model how things works • need a way to represent objects in the world • an object will contain data and processes to manipulate that data • In C++, these are data members and member functions • In other languages, they might be referred to as attributes (or slots) and methods • OOP languages slowly developed and are now contained in their own paradigm • Languages include C++, Java, Ada 95, Smalltalk, Common Lisp, Visual Basic although only Smalltalk and Java are true OOPLs

  13. Declarative Programming • Rather than having a programmer write a program that manipulates data • Have a programmer declare the things that must take place and have the language do the rest • Sound like magic? It isn’t • Prolog is the most common declarative language and it works because it has two built-in processes, resolution and unification • The programmer declares statements of fact and Prolog uses the two processes to prove other facts • Less a programming language than a tool

  14. Programming Language Concepts • We will concentrate on imperative language concepts here • Features: declarative statements, imperative statements, comments • Declarative statements: variable and type declarations, modules (procedures and functions) • Imperative statements: executable statements that include assignment statements, I/O statements, and control statements (loops, selection, procedure/function calls, recursion) • Comments: non-executed statements

  15. Data Types and Structures • Data types are built-in types and include • real/float (different precisions) • integer • character and stirngs (in some languages) • boolean • See figure 5.4 p. 239 for examples in C, C++, Java, Pascal and FORTRAN • Data Structures are often built on top of the built-in types -- here, the programmer dictates the “shape” of the structure • arrays (homogeneous structures) and records (heterogeneous structures) • See figures 5.5 and 5.6 on pages 240-241

  16. Assignment Statements • One of the most basic imperative statements is to assign a variable a value • This is also used in object-oriented programming to assign a data member a value • Typical form: variable <assignment symbol> expression • In Ada and Pascal, assignment symbol is “:=”, in FORTRAN, C, C++ and Java, the symbol is “=”, APL used “<--” which is more appropriate • Expressions may be arithmetic (numeric), relational or boolean, or may involve characters and strings (such as string concatenation) • Overloading allows symbols to be used by different types of instructions such as + for integer addition, real addition and string concatenation

  17. Control Statements • Because of the fetch-decode-execute cycle, the PC is always incremented so that the next instruction fetched will be the next in sequence • Thus, the built-in control for a program is sequential execution of instructions • Control statements allow the programmer to change the PC to some other, predetermined, location to alter the sequence • Types of control statements: • GO TO, procedure/function calls and recursion, iteration (loops), selection • Here, we will concentrate on the last two

  18. Given the result of a condition, decide where to go One way selection: If-Then statement Two way selection: If-Then-Else statement Multi-way selection: Case/Switch statement See figure 5.7 p. 244 If-Then/If-Then-Else statements use boolean conditions (evaluate to TRUE or FALSE) Multi-way might use boolean conditions, or ordinal expressions depending on language Allows a body of code to be repeated Counter-controlled loops (for loops) Repeat a set number of times In Ada, Pascal, sequence is set before the loop begins and cannot be altered, strictly increasing or decreasing by 1 In C, C++, Java, Algol, sequence can be altered during execution see figure 5.8, p. 245 Logically-controlled loops based on a condition which evaluates to TRUE or FALSE While-Do are pre-test loops Do-While are post-test loops Selection & Iteration Statements

  19. Comments • Comments are discarded during compilation so they have no effect on the program • Comments are denoted through some special character(s) • Pascal: { … } or (* … *) • C, C++, Java: /* … */ or // … • BASIC: rem … • FORTRAN: C (in the 6th column only) • Why comment? • We will see in Chapter 6 that commenting/documenting code is extremely important • Good comments lead to easier modification and maintenance of code

  20. Procedures • The idea behind a procedure is that it is a piece of code written independently of other program units to achieve modularity • There is some dependence in the intended use of the procedure -- why use it and how to use it • How to use it means what parameters to pass • A procedure call is a control statement that causes the current procedure to suspend, transferring control to the procedure until the procedure terminates and control returns to the location in the calling procedure where it left off • See figure 5.9, p. 248

  21. Calling Procedure: program unit that invokes the procedure Procedure Header: definition (using some reserved word) that starts the procedure includes procedure name and parameters (optional) Local Variables: variables declared in the procedure and only accessible in the procedure Global Variables: variables used in the procedure but not defined there or passed as parameters Parameters: variables explicitly passed from calling procedure to called procedure Formal Parameters: parameters in the procedure call list Actual Parameters: parameters in the procedure header These are used as variables in the procedure Parameter Passing Method: programming languages use different mechanisms for passing parameters Pass by value (fig 5.10 p. 251) Pass by reference (fig 5.11 p. 252) Procedure Terminology

  22. Functions • Another unit for modularity • Functions differ from procedures because they return a single value • No return parameters • parameters should only be passed by value, not reference • if a parameter is changed, its known as a side effect and this is permissible in many languages such as Pascal and C, C++ • Often used for mathematical computations • allows for nesting • One drawback of functions in most languages is that they are unable to return structures such as an array or record • in such cases, use procedures • All C, C++ modules are functions, but you can create procedure-like modules by specifying void as the return type for the function

  23. Input/Output Statements • Another sequential operation • like assignment statements • most languages offer standard forms of I/O • input from keyboard • output to monitor • to redirect I/O, you have to alter the statements • Pascal: readln, read, writeln, write • C, C++: scanf, printf; C++ and Java also have cin, cout which are objects instead of built-in instructions

  24. Translation Process • As said earlier, programs written in a high-level language must be translated into machine language before execution • Source program: program in the high-level language • Object program: program compiled into machine language • Translation process goes through 3 steps: • Lexical analysis: breaks program into distinct elements (tokens) • Parser: identifies each instruction and each instruction component • Code generation: generates actual assembly (or machine) code from the parsed components • See figure 5.12 p. 255 • We will now take a closer look at the parsing component

  25. Parsing • Parsing is a syntactic exercise -- identifying the grammatical components of the statement • We do this in natural language, breaking a sentence into noun phrase, verb phrase, verb, auxiliary verbs, prepositional phrases, etc • Complicating the process is that most programming languages are free-format instead of fixed-format • spacing and indentation are not critical and so are ignored • syntax of the language must use other forms of identification: • keywords/reserved words • delineators for instructions (such as the “;” and “{ … }” symbols)

  26. The Parsing Process • Parsing is performed by a parser • a pictorial representation of a parser is given by a syntax diagram, as shown in figures 5.13 , p. 257 and 5.14, p. 258 • A parser is a program that recognizes statements in the language using the language’s grammar • The output of the parser is a structure that represents the statements through non-terminal symbols (categories) and terminal symbols (the actual statement components) • This can be represented pictorially through a parse tree as shown in figures 5.15, p. 259 and 5.16, p. 260

  27. Ambiguity • A language is ambiguous if a statement could generate two or more parses • parse trees or interpretations • “The boy saw the man in the park with a telescope” • was the man in “the park with a telescope” or • did the boy see the “man in the park” by using a telescope? • A programming language must not be ambiguous or else the parser may not interpret an instruction correctly • If (X < Y) If (Y < Z) Writeln(X); Else Writeln(Y); • Which condition does the Else clause go with? • Programming Languages often have special mechanisms for such a case (endif statements) • In Pascal, the rule says that an Else clause is attached to the nearest condition (so in this case, the Else goes with Y<Z instead of X<Y)

  28. Ambiguity in Expressions X + Y * Z Taken strictly Left-to-Right, we would calculate X+Y first, giving us the equivalent of (X+Y)*Z We will explore in 3336 how a parser can properly evaluate such expressions In some languages, they may resort to forcing the programmer to use parentheses to denote the order (precedence) of evaluation, other languages have better parsers Ambiguity in Type The parser must derive the correct type of an expression X = Y + Z Arises if Y and Z are different types This requires type coercion Coercion may result in error Recall the truncation error using floating point arithmetic from chapter 1 A language is strongly typed if all processes involving data can be performed without coercion Java is one of the few strongly typed languages Examples of Ambiguity

  29. Compiling Vs. Optimizing • A large concern in the 1950’s was that compiling would produce poor code • A compiled program would not be as efficient as a program written by a person directly in machine language • However, compilers are usually very good at producing efficient code • Today, as architectures become increasingly complex, it is important that compiled code take advantage of the architectural features • Aside from code generation, some compilers perform code optimization • Using registers carefully, reorganizing instructions • Compiler Optimization is a topic for 6335

  30. A compiled program is not ready for execution! Most software today uses library modules written by others A procedure call in your program (such as cout) could raise a run-time error The linker is software that links several object programs together your compiled program, compiled library modules and Operating System routines this creates the load module, a consolidated program ready for execution The loader is software (usually part of the OS) that loads the load module into memory for execution This seems like a trivial task, but it has many repercussions in a multi-user and multi-tasking environment Must ensure that the loaded module does not enter another process’s memory space This information is stored in registers or in a memory map called a page table Processes may be moved around in memory (called relocation) See figure 5.18 p. 263 for linking/loading process Linking and Loading

  31. Object-Oriented Programming • In Imperative Programming, the idea was to write procedures that manipulate data • In OOP, the idea is to identify the data structures as objects • the data that makes up the objects (data members or attributes) • the relationship between objects (class/subclass structure) • the routines that are carried out on the objects (methods or member functions) • Many languages contain OOP (C++, Common Lisp, Ada 95) although there are few pure OOP languages (Smalltalk, Java)

  32. Encapsulation In 2380 and 3333, you learn of abstract data types and the notion of information hiding When you use a data structure, you should be able to use it without having to know how it is implemented OOP provides the characteristic of encapsulation Details are hidden from view so that other objects do not know how a given object is implemented The definitions of the object (the data structure and the processes) are “wrapped” up in the object itself Message Passing The main idea behind OOP is that objects communicate to each other through messages This differs from the imperative approach where procedures call each other and pass parameters Example: Resizing a window -- send the “resize” message from the “mouse object” to the “window object” a message contains the specific function to be executed (the method) along with (optional) parameters OO Features

  33. Polymorphism One object might wish to pass a message to another and include a parameter But what if the first object does not know what type of parameter is expected? Polymorphism allows that the message be interpretable no matter what type of parameter is passed In a way, this is like operator overloading, here we are “message overloading” Inheritance One of the advantages of OOP is that a subclass is able to inherit from its parent class any or all of its parent’s data members and member functions This leads to code reusability and an easier way of defining objects In C++, Java -- what is inherited can be controlled In C++ and Common Lisp, multiple inheritance is allowable (a child may have more than 1 parent) More OO Features

  34. More on OOP • C++ and Java are OOP languages and you will learn about them in 2320, 2330, 3333 and 3336 • Here, we have just seen a brief overview • See the code on pages 266-267 • we see class definitions and creating subclasses • See the code on page 268 • Private: items that are hidden from view to promote information hiding • Public: items that are visible so that the programmer knows what the object can do

  35. Concurrency • Some languages provide concurrency • the ability to execute multiple procedures at the same time • parallel processing: procedures executed simultaneously on independent processors • concurrent processing: procedures executed in an overlapped fashion on the same processor • concurrent processes are referred to as threads • often useful in simulation, games and graphics • Languages that offer concurrency include Ada, Java, Concurrent Pascal, and Concurrent Lisp • We will skip this section, you will see concurrency in some detail 3336

  36. Declarative Programming • Based on logical deduction • modus ponens and modus tollens • All men are mortal, Socrates is a man, therefore Socrates is mortal • In Prolog: • mortal(x) :- man(x). man(Socrates). • mortal(?). Would return Socrates • Automatic problem solving just by providing declarations: facts and rules • Based on two principles, resolution and unification • Prolog (PROgramming LOGic) is one of the only declarative languages • We study Logic in 4350 and Prolog in 3336 and 6351

More Related