33
Principles of Programming Languages Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Introduction

Principles of Programming Languages Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Introduction

Embed Size (px)

Citation preview

Page 1: Principles of Programming Languages Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Introduction

Principles of Programming Languages

Asst. Prof. Dr. Ahmet SayarSpring-2012

Kocaeli University Computer Engineering Department

Introduction

Page 2: Principles of Programming Languages Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Introduction

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.

Page 3: Principles of Programming Languages Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Introduction

Different Names for the Same Course

• Formal Languages• Programming Languages• The Principles of Programming Languages• Theory of Computing• Computational Theories• Fundamentals of Programming Languages

Page 4: Principles of Programming Languages Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Introduction

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

Page 5: Principles of Programming Languages Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Introduction

• What is a programming language?

Programming Languages

Programming Language

Page 6: Principles of Programming Languages Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Introduction

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.

Page 7: Principles of Programming Languages Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Introduction

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.

Page 8: Principles of Programming Languages Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Introduction

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

Page 9: Principles of Programming Languages Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Introduction

• 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

The Number of Programming Languages

Page 10: Principles of Programming Languages Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Introduction
Page 11: Principles of Programming Languages Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Introduction

Abstraction In Programming Languages

int sum(int[] x) { int sum = 0; n = 0; while (n < x.length) { sum += x[n]; } return sum;}

00101010101010101010111110101110101010111000101010101010...

Page 12: Principles of Programming Languages Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Introduction

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

Page 13: Principles of Programming Languages Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Introduction

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;

Page 14: Principles of Programming Languages Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Introduction

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)

Page 15: Principles of Programming Languages Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Introduction

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

Page 16: Principles of Programming Languages Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Introduction

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

Page 17: Principles of Programming Languages Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Introduction

Control Abstraction: Structured

• Divide a program into group of instructions that are nested in the code

• In C:In Haskell:

Page 18: Principles of Programming Languages Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Introduction

Control Abstraction: Structured

• Structured control – Procedure• ADA example for GCD (finding greatest common divisor)

Page 19: Principles of Programming Languages Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Introduction

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

Page 20: Principles of Programming Languages Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Introduction

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, ….

Page 21: Principles of Programming Languages Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Introduction

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

Page 22: Principles of Programming Languages Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Introduction

Quicksort in JavaA programming language is a way of thinking

Different people think in a different way

Page 23: Principles of Programming Languages Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Introduction

qsort [] = []qsort (x:xs) = qsort lt_x ++ [x] ++ qsort ge_x

wherelt_x = [y | y <- xs, y < x]mid = [y | y <- xs, y = x] ++ [x]ge_x = [y | y <- xs, y > x]

Quicksort in Haskell

Page 24: Principles of Programming Languages Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Introduction

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

Page 25: Principles of Programming Languages Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Introduction

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

Page 26: Principles of Programming Languages Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Introduction

Interpreter

Page 27: Principles of Programming Languages Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Introduction

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.

Page 28: Principles of Programming Languages Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Introduction

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

Page 29: Principles of Programming Languages Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Introduction

Compiler - 2

Page 30: Principles of Programming Languages Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Introduction

Linking

• Libraries of subroutines

Page 31: Principles of Programming Languages Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Introduction

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.

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.

Compilation

Page 32: Principles of Programming Languages Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Introduction

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

Page 33: Principles of Programming Languages Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Introduction

1-33

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