ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior...

40
ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer http://www.geocities.com /profMostafa

Transcript of ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior...

Page 1: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

ISBN 0-321-19362-8

Structure of Programming Languages

Prof. Dr. Mostafa Abdel Aziem Mostafa

Senior Lecturer http://www.geocities.com/profMostafa

Page 2: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

ISBN 0-321-19362-8

Module information …

1- Introduction&preliminaries

2- Describing Syntax and Semantics

3- Lexical and syntax analysis.

4- Names, binding, type checking, and Scopes

5- Data Types.

6-Expressions and Assignment Statements.

7- Statement- Level control Structures.

8- Subprograms.

9- Implementing Subprograms.

Page 3: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-3

Teaching Team

• Prof.Dr. Mostafa Abdel Aziem mostafa•

Teaching assistant

Mr. Mohamed Saleh

[email protected]

Office hours: sun 10:30- 12:30

different from

handout

Page 4: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-4

Module Structure

Lectures

Tutorials Help channels

Assignments& Quizzes

Examination

Page 5: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-5

~12 lectures(Mon 10:30-12:00)

15 tutorials (1 hr per week)

Office hours, email, appointment (by email)

4 Assignments & 1 or 2 Quizzes15% of final mark)

A written examination(40% of final mark)

Page 6: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-6

Concepts of Programming Languages.Sixth Edition, 2004Robert W. Sebesta

Additional ReadingPrinciple of Programming Languages

http://www.mhhe.com/engcs/compsci/tucker/

Page 7: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

ISBN 0-321-19362-8

Chapter 1

Preliminaries

Page 8: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-8

Chapter 1 Topics

• Motivation• Programming Domains• Language Evaluation Criteria• Influences on Language Design• Language Categories• Language Design Trade-Offs• Implementation Methods• Programming Environments

Page 9: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-9

Motivation: Why Study Programming Languages?

• Increased ability to express ideas• Improved background for choosing appropriate

languages• Greater ability to learn new languages• Understand significance of implementation• Ability to design new languages• Overall advancement of computing

Page 10: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-10

What makes programming languages an interesting subject? The amazing variety

There are very many, very different languages A list that used to be posted occasionally on comp.lang.misc had over 2800 published languages in 1999. Often grouped into four families:{ Imperative{ Object-oriented{ Functional{ Logic

Page 11: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-11

Imperative Languages

Example: Factorial function in C

int fact(int n) {

int sofar = 1;

while (n>0) sofar *= n--;

return sofar;

}

Hallmarks of imperative languages:

{ Assignment

{ Iteration

{ Order of execution is critical

Page 12: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-12

Functional Languages Example: Factorial function in Haskellfac 0 = 1fac n = n * fac (n-1) Hallmarks of functional languages:{ Single-valued variables{ closely tied to the mathematical concept of \function"{ Heavy use of recursion

Page 13: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-13

Logic Languages Example: Factorial function in Prologfact(X,1) :-X =:= 1.fact(X,Fact) :-X > 1,NewX is X - 1,fact(NewX,NF),Fact is X * NF. Hallmark of logic languages{ Program expressed as rules in formal logic

Page 14: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-14

Object-Oriented Languages Example: a Java denition for a kind of object that can store an integerand compute its factorialpublic class MyInt {private int value;public MyInt(int value) {this.value = value;}

Page 15: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-15

public int getValue() {return value;}public MyInt getFact() {return new MyInt(fact(value));}private int fact(int n) {int sofar = 1;while (n > 1) sofar *= n--;return sofar;} }

Page 16: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-16

Hallmarks of object-oriented languages:{ Usually imperative, plus . . .{ Constructs to help programmers use "objects\-little bundles ofdata that know how to do things to themselves.

Page 17: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-17

Widely Used and Not Widely Used Widely Used{ Java: Quick rise to popularity since 1995 release{ Java uses many ideas from C++, plus some from Mesa, Modula, andother languages{ C++ uses most of C and extends it with ideas from Simula 67, Ada,Clu, ML and Algol 68

Page 18: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-18

{ C was derived from B, which was derived from BCPL, which wasderived from CPL, which was derived from Algol 60Not Widely Used: Algol{ One of the earliest languages: Algol 58, Algol 60, Algol 68{ Never widely used{ Introduced many ideas that were used in later languages, including

Block structure and scope Recursive functions Parameter passing by value

Page 19: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-19

Programming Domains

• Scientific applications– Large number of floating point computations

• Business applications– Produce reports, use decimal numbers and characters

• Artificial intelligence– Symbols rather than numbers manipulated

• Systems programming– Need efficiency because of continuous use

• Scripting languages– Put a list of commands in a file to be executed

• Special-purpose languages

Page 20: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-20

Page 21: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-21

Page 22: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-22

Language Evaluation Criteria

• Readability– The most important criterium

– 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

– A relatively small set of primitive constructs can be combined in a relatively small number of ways

– Every possible combination is legal

– Lack of orthogonality leads to exceptions to rules

Page 23: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-23

Language Evaluation Criteria

– Readability factors (continued)• Control statements

• Defining data types and structures

• Syntax considerations– Identifier forms

– Special words

– Form and meaning

Page 24: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-24

Language Evaluation Criteria

• Writability– Factors:

• Simplicity and orthogonality

• Support for abstraction

• Expressivity

• Reliability– Factors:

• Type checking

• Exception handling

• Aliasing

• Readability and writability

Page 25: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-25

Language Evaluation Criteria

• Cost– Categories

• Training programmers to use language

• Writing programs

• Compiling programs

• Executing programs

• Language implementation system

• Reliability

• Maintaining programs

• Others: portability, generality, well-definedness

Page 26: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-26

Influences on Language Design

• Computer architecture: Von Neumann • We use imperative languages, at least in part,

because we use von Neumann machines– Data and programs stored in same memory– Memory is separate from CPU– Instructions and data are piped from memory to

CPU– Basis for imperative languages

• Variables model memory cells• Assignment statements model piping• Iteration is efficient

Page 27: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-27

Von Neumann Architecture

Page 28: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-28

Influences on Language Design

• Programming methodologies– 1950s and early 1960s: Simple applications; worry

about machine efficiency

– Late 1960s: People efficiency became important; readability, better control structures

• Structured programming

• Top-down design and step-wise refinement

– Late 1970s: Process-oriented to data-oriented• data abstraction

– Middle 1980s: Object-oriented programming

Page 29: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-29

Language Categories

• Imperative– Central features are variables, assignment

statements, and iteration

– C, Pascal

• Functional– Main means of making computations is by

applying functions to given parameters

– LISP, Scheme

Page 30: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-30

Language Categories

• Logic– Rule-based

– Rules are specified in no special order

– Prolog

• Object-oriented– Encapsulate data objects with processing

– Inheritance and dynamic type binding

– Grew out of imperative languages

– C++, Java

Page 31: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-31

Language Design Trade-Offs

• Reliability vs. cost of execution• Readability vs. writability• Flexibility vs. safety

Page 32: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-32

Layered View of Computer

Page 33: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-33

Levels of Language in Computing

Page 34: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-34

Implementation Methods

• Compilation– Translate high-level program to machine code

– Slow translation

– Fast execution

Page 35: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-35

Compilation Process

Page 36: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-36

Implementation Methods

• Pure interpretation– No translation

– Slow execution

– Becoming rare

Page 37: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-37

Pure Interpretation

Page 38: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-38

Implementation Methods

• Hybrid implementation systems– Small translation cost

– Medium execution speed

Page 39: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-39

Hybrid Implementation System

Page 40: ISBN 0-321-19362-8 Structure of Programming Languages Prof. Dr. Mostafa Abdel Aziem Mostafa Senior Lecturer ://.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-40

Programming Environments

• The collection of tools used in software development

• UNIX– An older operating system and tool collection

• Borland JBuilder– An integrated development environment for Java

• Microsoft Visual Studio.NET– A large, complex visual environment– Used to program in C#, Visual BASIC.NET,

Jscript, J#, or C++