Sei sulla pagina 1di 62

Preliminaries

"I think there is a world market for maybe five computers."


Thomas Watson, Chairman of IBM, 1943

7/25/2011

Lecture Outline
Reasons for studying concepts of programming languages Programming domains Language evaluation criteria Influences on language design Language categories Design trade-offs Implementation methods Introduction to Perl
7/25/2011

These lecture notes are based on Robert W. Sebestas Concepts of Programming Languages, 6th Edition

Reasons for Studying


Increased capacity to express ideas
Learning new data structures, algorithms, and other language features will allow a programmer to create more efficient programs
Example: after learning object-oriented programming in Java, a C programmer could simulate objects using structures and functions

7/25/2011

Reasons for Studying


Improved background for choosing appropriate languages
Without having studied several different languages, programmers will tend to stick with the first language they learned
Example: A C programmer might implement a binary tree with arrays, instead of using the objectoriented capabilities of Java

7/25/2011

Reasons for Studying


Increased ability to learn new language
Knowing the basic concepts of programming languages allows one to learn a new language easier
Example: C++ or Ada programmers, who already understand the concept of object-oriented programming, will have an easier time of learning Java then programmers have never used these concepts

7/25/2011

Reasons for Studying


Better understanding of the significance of implementation
Knowledge of the basic concepts of programming languages enables the programmer to make more efficient choices with implementation and to find bugs easier in a program
Example: A C programmer who understands pointers can create the binary tree using structures and pointers, instead of with arrays, and will have an easier time debugging programs that use pointers
7/25/2011 6

Reasons for Studying


Overall advancement of computing
Knowing the advantages & disadvantages of different languages helps those in charge to choose better languages over poorer ones
Example: In the early 1960s, Fortran was used much more than ALGOL 60, even though ALGOL 60 had much better control statements than Fortran

7/25/2011

Class Discussion
Do you believe our thinking capabilities are influenced by our language? Support your opinion.

7/25/2011

Programming Domains
Scientific applications
The first computers (1940s) created for scientific applications Used mostly arrays, matrices, counting loops, and selections Most efficient language for this is Fortran (1959) Also ALGOL 60 (1960) and its descendants designed mainly for this purpose
7/25/2011 9

Programming Domains
Business applications
Began to be used for business in 1950s First language for this was COBOL (1960) Used to create reports, storing decimal numbers & character data, and calculation decimal operations More recently, microcomputers are used for spreadsheet systems & database systems
7/25/2011 10

Programming Domains
Artificial intelligence
Uses symbolic rather than numeric computations Uses linked lists of names The functional language LISP was developed for AI applications in 1959 In 1970s, logic programming using Prolog appeared

7/25/2011

11

Programming Domains
Systems programming
Operating system + supporting tools = systems software Must be efficient & interface with external drivers In 1960s & 1970s, PL/S, BLISS, & Extended ALGOL were first used for this Currently, the UNIX operating system is written mostly in the C language
7/25/2011 12

Programming Domains
Scripting languages
Early scripting languages were simply a list of commands (called a script) in a file to be executed Scripting languages grew to include variables, control flow statements, functions, etc. Perl is a primitive programming language used for Common Gateway Interface (CGI) programming for the World Wide Web JavaScript & PHP are embedded in HTML documents for use on Web server systems
7/25/2011 13

Class Discussion
What arguments can you make for and against the idea of a single language for all programming domains?

7/25/2011

14

Language Evaluation Criteria


1. 2. 3. 4. Readability Writability Realiability Cost (Costability?)

7/25/2011

15

Language Evaluation Criteria


Readability
Ease at which a program can be read & understood Factors that affect readability

7/25/2011

Overall simplicity Orthogonality Control statements Data types & structures Syntax considerations
16

Overall Simplicity
A large number of features is difficult to learn
So programmers might learn two different subsets of features

Feature multiplicity (having more than one way to publish a particular operation) is not good
For example, in C++ or Java you can decrement a variable in four different ways: x = x - 1; x-=1; x--; --x

Operator overloading (a single operator symbol has more than one meeting) potentially can be bad Some languages, such as assembly language, can be too simple 7/25/2011 17

Orthogonality
A relatively small set of primitive constructs can be combined in the relatively small number of ways to build the control and data structures of the language
For example, if the language has three primitive data types, an integer, double, and character, and two type operators, array and pointer, then a large number of data structures can be defined Lack of orthogonality leads to exceptions to rules Too much orthogonality can also be bad, such as if you had the ability to add a pointer to another pointer in C
7/25/2011 18

Control Statements
In the 1970s, the use of goto statements was replaced by structured programming, which could be read from top to bottom
Most programming languages now contain sufficient control statements
if (x < y) x++; else y++; if (x < y) goto L1; y++; goto L2; L1: x++; L2:
19

7/25/2011

Data Types & Structures


Adequate data types and data structures also aids readability
A language with Boolean type is easier to read than one without
indicatorFlag = 0 is more difficult to read than indicatorFlag = false

7/25/2011

20

Syntax Considerations
Syntax = the way in which linguistic elements (as words) are put together to form constituents (as phrases or clauses)
Identifier forms
If too short, reduces readibility

Special words
Ada uses end if & end loop, while Java just uses } In Fortran 95, Do & End can also be variable names

Form & meaning


7/25/2011

In C, static changes meaning depending on position

21

Class Discussion
What common programming language statement, in your opinion, is most detrimental to readability?

7/25/2011

22

Language Evaluation Criteria


Writability
Ease at which a language can be used to create programs for a specific problem domain Factors that affect writability
Simplicity & orthogonality Support for abstraction Expressivity

7/25/2011

23

Simplicity & Orthogonality


It is best to have a smaller number of primitive constructs and a consistent set of rules for combining them
Having a few constructs with few exceptions is easier to learn for a beginner programmer On the other hand, a language can also be too orthogonal, so the inexperienced programmer could add two pointers together
7/25/2011 24

Support for Abstraction


Abstraction = the ability to define and then use complicated structures or operations in ways that allow many of the details to be ignored
Using the same function several times in the program To implement a binary tree in Fortran 77 requires arrays, while C++ uses nodes with two pointers and an integer
7/25/2011 25

Expressivity
A language has relatively convenient, rather than cumbersome, ways for specifying computations
For example, in C x++ is shorter & more convenient than x=x+1

7/25/2011

26

Language Evaluation Criteria


Reliability
A reliable program performs to its specifications under all conditions Factors that affect reliability

7/25/2011

Type checking Exception handling Aliasing Readability & writability


27

Reliability
Type checking
Testing for type errors in a given program
For example, can have errors if a function that is expecting an integer receives a float instead

Exception handling
Used in Ada C++ and Java, but not in C and Fortran
For example, the try & catch blocks of C++ catch runtime errors, fix the problem, and then continue the program without an abnormal end 28

7/25/2011

Reliability
Aliasing
Referencing the same memory cell with more than one name
For example, in this C code both x & y can be used to the same memory cell int x = 5; int *y = &x;

Readability & writability


If a program is difficult to read or write, its easy 7/25/2011 to make mistakes and difficult to find them 29

Language Evaluation Criteria


Cost
The total cost of a programming language is based on many of its characteristics Factors that affect cost

7/25/2011

Training programmers Writing programs Compiling programs Executing programs Language implementation system Poor reliability Maintaining programs
30

Cost
Most important contributors to language costs
Program development Maintenance Reliability

Most important evaluation criteria in respect to language costs


Writability Readability
7/25/2011 31

Other Evaluation Criteria


Portability
Ease with which programs can be moved from one implementation to another Influenced by a degree of standardization
Time-consuming and difficult

Generality
Applicability to a wide range of applications

Well-definedness
Completeness and precision of the languages official defining document
7/25/2011 32

Class Discussion
C vs. Java: Evaluate the C & Java languages using the criteria just described. What kind of problem-solving domain would C be useful for? What about Java?
1. 2. 3. 4.
7/25/2011

Readability Writability Realiability Costability


33

Influences on Language Design


Computer architecture
von Neumann (von Noyman) architecture

7/25/2011

Data & programs are stored in memory CPU executes instructions Instructions & data are piped between memory & CPU Variables model memory cells Assignment statements model piping operation Iterative form is most efficient form of iteration
34

Imperative languages designed around this

Von Neumann Architecture

Note: All diagrams in this lecture are from Robert W. Sebestas Concepts of Programming Languages, 6th Edition 7/25/2011

35

Influences on Language Design


von Neumann bottleneck
Fetch-execution cycle
Fetch the instruction pointed to by program counter Increment program counter Decode the instruction Execute the instruction

Instructions can be executed faster than they can be moved to the CPU for execution
7/25/2011 36

Influences on Language Design


Programming methodologies
Type checking & control statements were developed in early 1970s
Used procedure-oriented programming

Abstract data types were developed in late 1970s Object-oriented programming developed in the early 1980s

7/25/2011

37

Language Categories
1. Imperative
Uses variables, assignments statements, & iteration Uses functions, parameters, & recursion Uses rules, written in no specific order, with a language system which produces the desired result Uses data abstraction, inheritance, & dynamic method binding
38

2. Functional 3. Logic 4. Object oriented

7/25/2011

Language Design Trade-offs

Some criteria conflict with one another


For example, reliability & cost of execution In C, the index ranges of arrays are not checked
So executes fast, but it not so reliable

On the other hand, Java checks all references to array elements


So executes slower, but is more reliable
39

7/25/2011

Implementation Methods
Compilation
A compiler is a program that reads a program written in one language (the source language) and translates it into an equivalent program in another language (the target language). A part of this process is to report to its user the presence of errors in the source program. source program => compiler => target program | error messages
7/25/2011 40

8 Phases of a Compiler
6 main phases
1. 2. 3. 4. 5. 6. Lexical analyzer Syntax analyzer Semantic analyzer Intermediate code generator Code optimizer Code generator

Two more activities (interact with all 6 phases)


1. Symbol table manager 2. Error handler
7/25/2011 41

Compilation Process

7/25/2011

42

Post-Compiler
Linking the program
To create the executable (.exe file) program from the object (.obj) file, the linker (linkeditor) links:
1. The object files from the program modules, and 2. Any additional library files

Usually this includes the use of relocatable addresses within the program
Allows the program to run in different memory locations

7/25/2011

43

Post-Compiler
Loading the executable (.exe file) program
Also called the load module or executable image The loader
Identifies a memory location in which the program can be loaded, and Alters the relocatable machine code addresses to run in the designated memory location

A program is loaded into memory each time it is run


7/25/2011 44

Implementation Methods
Pure interpretation
High-level language statements are immediately decoded into machine code & executed
Easy to debug Slow to decode (10 to 100 times slower) Requires more space for symbol table & source program

7/25/2011

45

Pure Interpretation

7/25/2011

46

Implementation Methods
Hybrid implementation systems
Cross between compilers & interpreters
High-level language programs are translated to an intermediate language, which is easily interpreted

Perl
Partially compiled to detect errors, then interpreted

Java
Intermediate byte code is created from a program, then it is interpreted
7/25/2011 47

Hybrid Implementation System

7/25/2011

48

Introduction to Perl
Created by Larry Wall in 1987
Secret project for National Security Agency
Had to find & print important information in text files

Practical Extraction and Report Language


or Pathologically Eclectic Rubbish Lister

Server-side Internet scripting language


Efficient at manipulating text Usability, speed, flexibility, portability
7/25/2011 49

Perl Program
See example.pl program
First line is path to Perl interpreter
UNIX command: which perl

Use pound sign (#) for single-line comments Output to screen


print "output statement";

Make the file executable & execute


chmod u+x example1.pl ./example.pl
7/25/2011 50

Scalar Variables
Scalar variables can be strings or numbers
For example $var1 = "aloha"; $var2 = 5; Can use the same operators as C or Java $var2++; Can also include them in a single print statement
print "$var1 and $var2"; #aloha and 6
7/25/2011 51

Scalar Variables
Have special Boolean operators to compare strings
Letters later in the alphabet have a greater value eq = equals ne = not equals gt = greater than lt = less than
7/25/2011 52

Input & Subroutine


The standard input usually from the keyboard
Waits for user to enter data $var1 = <STDIN>; Delete newline from end of string (\n) chomp($var1); Subroutine (function) format
subroutine1(); subroutine1(){ . . . }
7/25/2011 53

Control Structures
Selection structures
if, unless, if/else, if/elsif/else unless is the opposite of if

Repetition structures
while, until, do/while, do/until, for, foreach until is the opposite of while

Special variable $_
Default argument for control structures & functions
7/25/2011 54

Control Structures
last statement
Will immediately exit a while, until, for, or foreach loop

redo statement
Will execute from the first statement in the body of a while, until, for, or foreach loop, without executing the loop continuing test

7/25/2011

55

Arrays
Stores a list of scalars
@array = ("Apple", 'b', 3, 4.4);

Access each element of the array with $, [ ]


$array[0] is "Apple" $array[3] is 4.4

Negative subscripts start from end of array


$array[-1] is 4.4 $array[-2] is 3
7/25/2011 56

Arrays
Special variable $" is the string separator
$" = "-"; print "@array\n"; #Apple-b-3-4.4

Index of the last element of an array


$last = $#array;

Length of an array
$length = @array;

BUT this displays array without any spaces


7/25/2011

print @array;

57

Arrays
Insert & remove to end of array
push(@list, 5);

$last = pop(@list); Insert & remove to beginning of array (less efficient)


unshift(@list, "Apple"); $first = shift(@list);
7/25/2011 58

Arrays
Numeric sort
@list1 = sort{$a <=> $b} @list;

Alphabetical sort
@list2 = sort{$a cmp $b} @list;
$a is current element $b is next element <=> and cmp return 1, 0, or -1, for numbers or strings greater then, equal, or less than {$b <=> $a} sorts in descending order
7/25/2011 59

foreach Repetition Structure


Used to iterate through a list
Can access and change values
foreach $1(@integers){$1=$1*$1;}

Can also use special variable $_


Can also access and change values
foreach (@integers){$_++;}

7/25/2011

60

Subroutines
Argument stored in special array variable @_
subroutine7(1, 2, 3, 4, 5); In body of subroutine
@_ is array (1, 2, 3, 4, 5) $_[0] returns 1
$_[] is NOT special variable $_

shift returns 1 pop returns 5

Return value
7/25/2011

return $whatever; # @whatever;

61

Scope
Where the variable can be referenced
Default is global scope
Visible throughout the program

my variables have lexical scope


Visible only inside its block Use for recursion

local variables have dynamic scope


Visible inside its block Also visible to any subroutines called from its block
7/25/2011 62

Potrebbero piacerti anche