560 likes | 1.25k Views
Principles of Programming Languages. Introduction. Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department. Goal of This Course.
E N D
Principles of Programming Languages Introduction Asst. Prof. Dr. AhmetSayar Spring-2012 Kocaeli University Computer Engineering Department
Goal of This Course • Introducing major principles and concepts underlying all programming languages without concentrating on one particular language. • It is not necessary for you to be familiar with all the programming languages. Familiarity from one of them is enough. • It would be great if you have some general knowledge of data structures, algorithms and computational processes.
Different Names for the Same Course • Formal Languages • Programming Languages • The Principles of Programming Languages • Theory of Computing • Computational Theories • Fundamentals of Programming Languages
Related Studies in Literature • Computer Engineering • Computer Science • Linguistics • Scientific Study of Human Language • Morphology, syntax and phonology • Cognitive Science • Interdisciplinary scientific study of mind and its processes • Intelligence and behavior • How information is represented carried processed within nervous systems (humans and animals) and machines • Consists of multiple disciplines: psychology, artificial intelligence (computer eng and science), philosophy, linguistics, sociology, anthropology
Programming Language Programming Languages • What is a programming language?
What is a Programming Language • A notation for communicating with a computer what we want it to do. • A major advance in computer design occurred in 1940 • Instead of hard-wired jobs • A series of codes stored as data would determine the actions taken by a central processing unit. • Soon, programmers realized attaching symbols to the instruction codes as well as to the memory locations • And assembly language was born.
What is a Programming Language • Assembly language are machine dependent. Low-level languages. Hard to read and write • High-level languages? • Von Neumann model • An area of memory where both programs and data are stored and a separate central processing unit that sequentially executes instructions fetched from memory. • New Definition: Notational system for describing computation in machine-readable and human-readable form.
Computation? • Any process that can be carried out by a computer. • Not necessarily mean mathematical calculations • Data manipulation, text processing, information storage retrieval • Special purpose languages • Graphics, reports, database • General purpose languages • C, JAVA
The Number of Programming Languages • How many programming languages do you know? • This is a sample list… • http://dmoz.org/Computers/Programming/Languages • Why is the number of programming languages so large? • Evolution • Special Purpose • Personal Preference
int sum(int[] x) { int sum = 0; n = 0; while (n < x.length) { sum += x[n]; } return sum; } 00101010101010 10101011111010 11101010101110 00101010101010 ... Abstraction In Programming Languages
Abstraction In Programming Languages • Data Abstraction • Abstracts properties of data • Control Abstraction • Abstracts properties of the transfer of control • Abstractions fall into 3 levels • Basic • Structured • Unit
Data Abstraction: Basic • Abstracts the internal representation of common data values in a computer • Locations in computer memory that contain data values are abstracted by giving them names and are called variables. • Pascal • var x : integer; • C • int x;
Data Abstraction: Structured • Abstracting collections of data values that are related • Employee record may contain name, company, salary etc. Each diff type • Group of items having same type • In C – int a[10]; • In Fortran – INTEGER a(10)
Data Abstraction: Unit • Collecting related code into specific locations within a program, either as separate files or as separate language structures • Include access conventions and restrictions • Referred to as data encapsulation and information hiding. • Modules in ML and Haskell • Packages in JAVA • Unit data abstractions become the basis for language library mechanism • reusability
Control Abstraction: Basic • Typical basic control abstraction is the statements in a language that combine a few machine instructions into a more understandable abstract statement. • Assignment statement x = x + 3; • Another basic control statement is the GOTO statement • Example from Fortran
Control Abstraction: Structured • Divide a program into group of instructions that are nested in the code • In C: In Haskell:
Control Abstraction: Structured • Structured control – Procedure • ADA example for GCD (finding greatest common divisor)
Control Abstraction: Unit • It is similar to data unit abstraction. • The only difference is here the focus is on the operations rather than the data • But goals of the reusability and library building remain the same
Language Definition • Language Definition can be loosely divided into two parts • SYNTAX (structure): grammar of a language • An if-statement consists of the word “if” followed by an expression inside parentheses, followed by… • SEMANTICS (meaning) • An if=statement is executed by first evaluating its expression, which must have arithmetic or pointer type, including all side effects, and if it compares unequal to 0, ….
Syntax • The description of language syntax is one of the areas where formal definitions have gained acceptance, and the syntax of almost all languages is now using context-free grammar • Example context-free grammar
Quicksort in Java • A programming language is a way of thinking • Different people think in a different way
Quicksort in Haskell qsort [] = [] qsort (x:xs) = qsort lt_x ++ [x] ++ qsort ge_x where lt_x = [y | y <- xs, y < x] mid = [y | y <- xs, y = x] ++ [x] ge_x = [y | y <- xs, y > x]
Semantics • Much more complex than syntax. • Meaning can be defined in many ways. • No generally accepted method • Several notational systems for formal definitions have been developed and are increasingly in use. • Operational Semantics • Denotational Semantics • Axiomatic semantics
Language Translation • Translator • Interpreter • Compiler • Interpretation is a one-step- process, in which both the program and the input are provided to the interpreter, and the output is obtained
Examples • Some examples of interpreted programs are BASIC, QBASIC, and Visual Basic (version 5 of which has both a compiler and interpreter) • The Practical Extraction and Reporting Language, or Perl, is a script-based programming language whose syntax parallels that of the C language but is an interpreted language; Perl can optionally be compiled prior to execution into either C code or cross-platform bytecode • JavaScript is another example of an interpreted script-based programming language.
Compiler - 1 • Compilation is two-step process • Original program – source program is the input and new program – target program is output. • Target program may then be executed. • More commonly target language is assembly language • Target program must be translated by an assembler into an object-program • Then, linked with other object programs • and then, loaded into appropriate memory locations before it can be executed
Linking • Libraries of subroutines
Compilation From Source Code to Executable Code program gcd(input, output); var i, j: integer; begin read(i, j); while i <> j do if i > j then i := i – j; else j := j – i; writeln(i) end.
Hybrid Implementation System • JAVA uses both interpretation and compilation • Source code is compiled into byte-code • JVM (Java Virtual Machine) runs it as if it is an interpreter
Compiler vs. Interpretation • Compilation • Translate high-level program to machine code • Slow translation • Fast execution • Compiler vs. Interpretation • Compiler are better in terms of time efficiency • Interpreters are better in terms of memory usage • Interpreters are better in terms of exception handling