course no-101319 programming languages theory & practice first semester 2008 - 2009 ittihad...

24
Course No-101319 ramming Languages Theory & Pract First Semester 2008 - 2009 ITTIHAD UNIVERSITY Computer Science Department Instructor: Dr. Rafiqul Zaman Khan , Ph.D

Upload: audra-fay-bailey

Post on 18-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Course No-101319 Programming Languages Theory & Practice First Semester 2008 - 2009 ITTIHAD UNIVERSITY Computer Science Department Instructor: Dr. Rafiqul

Course No-101319Programming Languages Theory & Practice

First Semester 2008 - 2009

ITTIHAD UNIVERSITY Computer Science DepartmentInstructor: Dr. Rafiqul Zaman Khan , Ph.D

First Semester 2008 - 2009

ITTIHAD UNIVERSITY Computer Science DepartmentInstructor: Dr. Rafiqul Zaman Khan , Ph.D

Page 2: Course No-101319 Programming Languages Theory & Practice First Semester 2008 - 2009 ITTIHAD UNIVERSITY Computer Science Department Instructor: Dr. Rafiqul

Handout-2 Preliminaries

Page 3: Course No-101319 Programming Languages Theory & Practice First Semester 2008 - 2009 ITTIHAD UNIVERSITY Computer Science Department Instructor: Dr. Rafiqul

Reasons to study concepts of PLs

• Increased capacity to express programming concepts

• Improved background for choosing appropriate languages

• Increased ability to learn new languages• Understanding the significance of

implementation• Increased ability to design new languages• Overall advancement of computing

Page 4: Course No-101319 Programming Languages Theory & Practice First Semester 2008 - 2009 ITTIHAD UNIVERSITY Computer Science Department Instructor: Dr. Rafiqul

Programming Domains

• Scientific applications

• Business applications

• Artificial intelligence

• Systems programming

• Scripting languages

• Special purpose languages

Page 5: Course No-101319 Programming Languages Theory & Practice First Semester 2008 - 2009 ITTIHAD UNIVERSITY Computer Science Department Instructor: Dr. Rafiqul

Programming Language Definition

• Language Syntax– Grammar and punctuation.– Superset of legal languages.

• Language Semantics– What the syntax means.– Restricts syntactically correct programs.

Page 6: Course No-101319 Programming Languages Theory & Practice First Semester 2008 - 2009 ITTIHAD UNIVERSITY Computer Science Department Instructor: Dr. Rafiqul

Programming Paradigms

• Imperative – Program = Algorithms + Data

• Functional– Program = Function o Function

• Object-Oriented– Program = Objects + Messages

• Logic– Program = Facts + Rules

• Special-purpose

Page 7: Course No-101319 Programming Languages Theory & Practice First Semester 2008 - 2009 ITTIHAD UNIVERSITY Computer Science Department Instructor: Dr. Rafiqul

Imperative Programming

• Oldest style of Programming

• Algorithm is expressed as a sequence of instructions, e.g. assignment, looping, …

• For example, assembly, Fortran, Algol, Pascal, and C.

Page 8: Course No-101319 Programming Languages Theory & Practice First Semester 2008 - 2009 ITTIHAD UNIVERSITY Computer Science Department Instructor: Dr. Rafiqul

Example in C

• Finding the greatest common divisor between two elements#include<stdio.h> void main() { int x,y; printf("enter any two numbers"); scanf("%d %d",&x,&y); if(x<0)x=-x; if(y<0)y=-y; while(x!=y) x>y?(x=x-y):(y=y-x); printf("gcd of the numbers is %d",x); }

Page 9: Course No-101319 Programming Languages Theory & Practice First Semester 2008 - 2009 ITTIHAD UNIVERSITY Computer Science Department Instructor: Dr. Rafiqul

Functional Programming

• Comes from traditional Mathematics.• Description of computation is based on the

evaluation of functions or the application of functions to known values.

• No notion of variables or assignment to variables!

• Loops are replaced by recursion!• For example Lisp, Scheme, ML, Haskell.

Page 10: Course No-101319 Programming Languages Theory & Practice First Semester 2008 - 2009 ITTIHAD UNIVERSITY Computer Science Department Instructor: Dr. Rafiqul

Example in Scheme

• Finding the greatest common divisor between two elements

(define (gcd u v) (if (= v 0) u (gcd v (remainder u v))))

Page 11: Course No-101319 Programming Languages Theory & Practice First Semester 2008 - 2009 ITTIHAD UNIVERSITY Computer Science Department Instructor: Dr. Rafiqul

Object Oriented Programming

• Based on class and inheritance

• Instead of applying global functions to variables, “methods” associated with instances are invoked (message passing)

• For example, C++, Java, Smalltalk, …

Page 12: Course No-101319 Programming Languages Theory & Practice First Semester 2008 - 2009 ITTIHAD UNIVERSITY Computer Science Department Instructor: Dr. Rafiqul

Example in C++

• // gcd.cpp - long integer greatest common divisor

• class BadGcdArgumentsException{ };

• long gcd(long a,long b) // Euclidean algorithm - returns 0 // if a and b are both 0. • { long r; // First make sure everything is >= 0. • if(a < 0) a = -a; • if(b < 0) b = -b; • // If both arguments are 0, throw an exception. • if(a == 0 && b == 0) • throw BadGcdArgumentsException(); • // Otherwise the gcd of 0 and x is x. • if(a == 0) return(b); if(b == 0) return(a); • // Keep dividing until we get a zero remainder. The last • // nonzero remainder is the gcd. • while(b > 0) {• r = a % b; • a = b; • b = r; • } • return(a); • } • #include<stdio.h>• void main()• {• int x,y,r;• printf("Please Input two numbers : ");• scanf("%d %d",&x,&y);• r=gcd(x,y);• printf("gcd = %d\n",r);• }

• Please Input two numbers : 4 5• gcd = 1• Press any key to continue

• Finding the greatest common divisor between two elements

Page 13: Course No-101319 Programming Languages Theory & Practice First Semester 2008 - 2009 ITTIHAD UNIVERSITY Computer Science Department Instructor: Dr. Rafiqul

Logic Programming

• Like functional programming, but through formal logic.

• A program is a set of predicates, i.e. rules governing the problem.

• Either the truthfulness of a given formula or the deduction of new formulae results from running a logic program.

• For example, prolog.

Page 14: Course No-101319 Programming Languages Theory & Practice First Semester 2008 - 2009 ITTIHAD UNIVERSITY Computer Science Department Instructor: Dr. Rafiqul

Example in Prolog

• Finding the greatest common divisor between two elements

gcd(U,V,U) :- V=0.gcd(U,V,X) :- V>0, Y is U mod V, gcd(V,Y,X).

Page 15: Course No-101319 Programming Languages Theory & Practice First Semester 2008 - 2009 ITTIHAD UNIVERSITY Computer Science Department Instructor: Dr. Rafiqul

Special-Purpose Languages

• Shell, Awk, Perl, Python– System administration– Program configuration

• Postscript, Tex, RTF– Text and document setting

• HTML, XML– Markup Language

Page 16: Course No-101319 Programming Languages Theory & Practice First Semester 2008 - 2009 ITTIHAD UNIVERSITY Computer Science Department Instructor: Dr. Rafiqul

Example in Perl• Make up one pdf file from separate postscript files.

# This script takes as input the desired output filename (without the extension) and the # number of input files. It is assumed that the input filenames are of the form 1.ps 2.ps ... # 99.ps and so on and so forth. It then combines all postscript files into one file with a .ps # extension. Finally, it will generate the pdf version of the postscript file.

$Docs_Dir = 'C:\\Program Files\\ScannerU\\';$OutputFile = $ARGV[0];$NumOfInputs = $ARGV[1];

$FileList = "";for ($i = 1; $i <= $NumOfInputs; $i = $i + 1) { $FileList = $FileList . $i . ".ps ";}chdir($Docs_Dir);system("gswin32c -dNOPAUSE -sDEVICE=pswrite -dBATCH -sOutputFile=" . $OutputFile . ".ps " . $FileList);system("ps2pdf -r720 " . $OutputFile . ".ps " . $OutputFile . ".pdf ");

Page 17: Course No-101319 Programming Languages Theory & Practice First Semester 2008 - 2009 ITTIHAD UNIVERSITY Computer Science Department Instructor: Dr. Rafiqul

Language Evaluation Criteria• Readability

– The most important criteria– Factors

• Overall simplicity– Too many features is bad– Multiplicity of features is bad

• Orthogonality– Makes the language easy to learn and read– Meaning is context independent

• Control statements• Data type and structures• Syntax considerations

• Writability– Factors

• Simplicity and orthogonality• Support for abstraction• Expressivity

Page 18: Course No-101319 Programming Languages Theory & Practice First Semester 2008 - 2009 ITTIHAD UNIVERSITY Computer Science Department Instructor: Dr. Rafiqul

Language Evaluation Criteria (cont.)• Reliability

– Factors• Type checking• Exception handling• Aliasing• Readability and writability

• Cost– Categories

• Programmer training• Software creation• Compilation• Execution• Compiler cost• Poor reliability• Maintenance

• Others: portability, generality, well-definedness

Page 19: Course No-101319 Programming Languages Theory & Practice First Semester 2008 - 2009 ITTIHAD UNIVERSITY Computer Science Department Instructor: Dr. Rafiqul

Layered View of a Computer

Page 20: Course No-101319 Programming Languages Theory & Practice First Semester 2008 - 2009 ITTIHAD UNIVERSITY Computer Science Department Instructor: Dr. Rafiqul

Implementation Methods

• Compilation• Interpretation• Hybrid Implementation Systems

Page 21: Course No-101319 Programming Languages Theory & Practice First Semester 2008 - 2009 ITTIHAD UNIVERSITY Computer Science Department Instructor: Dr. Rafiqul

Compilation

• Translate high-level program to machine code

• Slow translation• Fast execution

Page 22: Course No-101319 Programming Languages Theory & Practice First Semester 2008 - 2009 ITTIHAD UNIVERSITY Computer Science Department Instructor: Dr. Rafiqul

Interpretation

• No translation• Slow execution• Usually found in

functional and logic-based programming languages.

Page 23: Course No-101319 Programming Languages Theory & Practice First Semester 2008 - 2009 ITTIHAD UNIVERSITY Computer Science Department Instructor: Dr. Rafiqul

Hybrid Implementation Systems

• Small translation cost• Medium execution speed

Page 24: Course No-101319 Programming Languages Theory & Practice First Semester 2008 - 2009 ITTIHAD UNIVERSITY Computer Science Department Instructor: Dr. Rafiqul

Programming Environments• The collection of tools used in software

development

• UNIX– An old operating system and tool collection

• Borland JBuilder– A PC environment for Java

• Smalltalk– A language processor/environment

• Microsoft Visual C++– A large, complex visual environment