Chapter 6 Programming Languages Chapter 6 Programming Languages
- Slides: 69
Chapter 6 Programming Languages
Chapter 6: Programming Languages n n n n 6. 1 6. 2 6. 3 6. 4 6. 5 6. 6 6. 7 Historical Perspective Traditional Programming Concepts Procedural Units Language Implementation Object Oriented Programming Concurrent Activities Declarative Programming 2
Programming Languages n n 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. 3
Historical Perspective n n n Evolution of computer languages: �� Micro-instructions: Inside CPU, they implement each machine instruction. �� Machine language: binary form direct controls the hardware. �� E. g. , “ 4032”: R 3⇐ R 2, very hard to read & write the programs. �� Assembly language: mnemonic form of the machine language. �� E. g. , “MV R 3 R 2”is used to refer to “ 4032”. �� Descriptive name like “Price” is used to represent some memory address. �� “LD R 5, Price”(156 C) load the content in the address “Price” 0 X 6 C to R 5. �� Mnemonic ADDI & ADDF refer to integer & floating-point additions. �� 4
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. 5
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] 6
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 7
Figure 6. 1 Generations of programming languages 8
Second-generation: Assembly language n A mnemonic system for representing programs n n n Mnemonic names for op-codes Names for all registers Identifiers = descriptive names for memory locations, chosen by the programmer 9
Assembly language characteristics n One-to-one correspondence between machine instructions and assembly instructions n n n Programmer must think like the machine Inherently machine-dependent Converted to machine language by a program called an assembler 10
Assembly language example Machine language 156 C 166 D 5056 30 CE C 000 Assembly language LD R 5, Price LD R 6, Shipping. Charge ADDI R 0, R 5 R 6 ST R 0, Total. Cost HLT 11
Third generation language n Uses high-level primitives n n n 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 12
Machine Independence and Beyond n n n 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. 13
Figure 6. 2 The evolution of programming paradigms 14
Software Development Paradigms n n n n 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. �� 15
Software Development Paradigms �� n Declarative: emphasizes more on what the problem is than how to solve it. �� n Goal: discover/implement a general problem-solving algorithm, and develop/write a precise statement of the problem. [SQL, formal logic, logic programming] 16
Figure 6. 3 A function for checkbook balancing constructed from simpler functions 17
Software Development Paradigms �Which of the following is an example of a language that is based on the object-oriented paradigm? n n A. LISP B. PROLOG C. C D. C++ n Most machine languages are based on the n A. Imperative paradigm B. Declarative paradigm n 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? n n A. Imperative statement C. Declarative statement B. Exclamatory statement D. Comment statement 18
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. 19
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. �� 20
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 21
Traditional Programming Concepts � 22
Figure 6. 4 The composition of a typical imperative program or program unit 23
Constants and literals �Effective. Alt <- alitmeter + 645; In C const int Airaltitude = 645; In Java final int Airaltitude = 645; Effective. Alt <- alitmeter + Airaltitude; 24
Figure 6. 5 Variable declarations in C, C++, C#, and Java 25
Figure 6. 6 A two-dimensional array with two rows and nine columns 26
Figure 6. 7 Declaration of heterogeneous array 27
Figure 6. 8 Control structures and their representations in C, C++, C#, and Java 28
Figure 6. 9 The for loop structure and its representation in C++, C#, and Java 29
Traditional Programming Concepts Which of the following does not require a Boolean structure? A. If-then-else statement C. Assignment statement B. While loop statement D. For loop statement Which of the following is not a control statement? A. If-then-else statement C. Assignment statement B. While loop statement D. For loop statement 30
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. �� 31
Procedural Unit Imperative statements to describe the steps performed in the procedure. �� Invoking the procedure: �� FORTRAN: “CALL Get. Names”; in Ada/C/C++/Java/Pascall, “Get. Names; ”. 32
Figure 6. 10 The flow of control involving a procedure 33
Figure 6. 11 The procedure Project Population written in the programming language C 34
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. 35
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 36
Figure 6. 13 Executing the procedure Demo and passing parameters by reference 37
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”. 38
Figure 6. 14 The function Cylinder. Volume written in the programming language C 39
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. 40
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: § 41
Figure 6. 15 The translation process 42
Language Implementation 1. 2. 3. 4. 5. 6. 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++; )� 43
Language Implementation 1. 2. 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: 44
Figure 6. 16 A syntax diagram of our if-then-else pseudocode statement 45
Figure 6. 19 Two distinct parse trees for the statement if B 1 then if B 2 then S 1 else S 2 46
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 B 1 then if. B 2 then. S 1 else. S 2”reveals such flaw. § Avoidance: (uniqueness)�� Using parentheses to specify the precise join. [programmers]�� Adjusting the syntax rules for default join (nearest). [languagedesigners] § 47
Figure 6. 17 Syntax diagrams describing the structure of a simple algebraic expression 48
Figure 6. 18 The parse tree for the string x + y x z based on the syntax diagrams in Figure 6. 17 49
Code Generation To analyze imperative statements, the parser first processes the declarative statements and records each userdefined 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. �� � 50
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. 51
Figure 6. 20 An object-oriented approach to the translation process 52
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. 53
Figure 6. 21 The complete program preparation process 54
Objects and Classes n n 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. 55
Components of an object n n Instance variable = variable within an object Method = function or procedure within an object n Can manipulate the object’s instance variables 56
Figure 6. 22 The structure of a class describing a laser weapon in a computer game 57
Class & Object q Create an object of “type” and assign it to Laser 1. Laser. Class Laser 1 = new Laser. Class(); --declare variable Laser 1 to be of the type Laser. Class but also creates a new object using the Laser. Class template q Activate the approriate methods Laser 1. fire(); 58
Constructor = special method to initialize a new object instance Laser. Class Laser 1(50), Laser 2(100); 59
Encapsulation n Encapsulation = a way of restricting access to the internal components of an object n n Private—only object itself can access it Public—accessible from outside of objects 60
Figure 6. 24 Our Laser. Class definition using encapsulation as it would appear in a Java or C# program 61
Additional object-oriented concepts Inheritance: allows new classes to be defined in terms of previously defined classes n Class Rechargeable. Laser extends Laser. Class { void fire () } Rechargeable. Laser 3, Laser 4; n n Polymorphism: allows method calls to be interpreted by the object that receives the call Laser 3. fire(); 62
Programming concurrent activities n Parallel or concurrent processing = simultaneous execution of multiple processes n n 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. 63
Figure 6. 25 Spawning processes 64
Interaction between processes n n n 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 65
Declarative programming n n 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 n Example: (P OR Q) AND (R OR Q) resolves to (P OR R) n Resolvent = a new statement deduced by resolution n Clause form = statement whose elementary components are connected by the Boolean operation OR E. X. : P OR Q P->Q Q OR ~P 66
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 67 Be true
Figure 6. 27 Resolving the statements (P OR Q), (R OR ¬Q), ¬R, and ¬P 68
Prolog(Programming in Logic) n Fact = predicate. Name(arguments). n n Example: parent(bill, mary). Rule = conclusion : - premise. n n : - means “if” Example: wise(X) : - old(X). old(x)-> wise(x) n n Example: faster(X, Z) : - faster(X, Y), faster(Y, Z). n ((faster(x, y) and faster(Y, z))-> faster(x, z) All statements must be fact or rules. 69
- Real-time systems and programming languages
- Cs 421
- Multithreaded programming languages
- Programming languages levels
- Introduction to programming languages
- Plc
- Joey paquet
- Comparative programming languages
- Alternative programming languages
- Strongly typed vs weakly typed
- Transmission programming languages
- Cse 340 principles of programming languages
- Int vs short
- Xenia programming languages
- Advantages and disadvantages of system software
- Mainstream programming languages
- Vineeth kashyap
- Programing languages
- Programming languages
- Programming languages
- Programming languages
- Language
- Brief history of programming languages
- Lisp_q
- Real time programming language
- Eniac
- If programming languages were cars
- Reasons for studying concepts of programming languages
- Cornell programming languages
- Low level programming language
- Middle level programming languages
- The art of programming
- Cs 421 programming languages and compilers
- Multimedia programming languages
- Storage management in programming languages
- Perbedaan linear programming dan integer programming
- Greedy vs dynamic programming
- Components of system programming
- Linear vs integer programming
- Definisi integer
- How do individual use languages vary among places
- Assembly code example
- Jumlah level dalam gambar ini adalah . . . .
- Turing unrecognizable languages
- 5 love languages results
- Love language test results
- Love languages adults
- All love languages
- 11 official languages of south africa
- How many languages are there
- Ror programming language
- What languages come from latin
- Relational query languages in dbms
- Introduction to scripting languages
- Pumping lemma non regular languages examples
- Languages
- New zealand official languages english
- Flexive languages
- Modern languages for life and work
- Carol can speak three foreign languages
- Media and information languages
- Love languages list
- Machine language is a low level language
- Asian languages written
- Five love languages.com
- Sino-tibetan languages
- Decidable languages
- History of computer languages
- Dash phase is known as the back end of the compiler
- Minority language meaning