Programming
Languages
(04/20/2008)
Machine Code/Assembler/High Level Languages/Natural Languages: Computer
languages exist on a spectrum from machine oriented languages (machine code) to
human oriented languages (high-level languages)
- Machine Code (tightly coupled with hardware)
- in binary
- allows only numeric
addresses for data and instructions
- difficult to change
(since all memory addresses are fixed binary numbers)
- difficult to create
data (consider the binary representation of a floating point number)
- reflects the underlying
architecture of the computer
- Assembler (one step removed from hardware but still machine
oriented)
- uses symbolic code;
i.e. assembler language is mnemonics for machine code.
- uses symbolic memory
addresses
- provides utility
features that allows programmer to change program and/or create data
- reflects underlying
architecture of the computer
- programmer must
(micro-) manage movement of data between memory and memory, between
registers and memory, and between registers and registers
- High-Level Languages (human oriented)
- syntax is based on
human languages (e.g. COBOL) or mathematical constructs (e.g. FORTRAN)
- programmer does not
need to manage details of movement of data items
- allows programming to
take macroscopic view of task
- program is portable -
can be executed on many different machines
- Natural Languages
- human languages are
"context" sensitive - meaning is often inferred from context.
- statements in human
languages can be ambiguous, having multiple meanings
More
Information
Code Translation: Programs in languages other than machine code must
be translated into machine code. Translations can be done one of two ways:
Interpretation is the translation technique
where each individual statement in a program is first translated, then executed
(Python and the original BASIC use interpretation). With compilation the entire program is first translated
into machine code then executed (most other languages use this). With
compilation, execution is faster since the "price" of translation is
paid "up-front". However interpreted languages are more “flexible”.
There are also hybrid translation systems
where the source code it translated into an intermediate code which is later
interpreted (e.g. Java).
·
Assemblers
o
one to one translation: each assembler statement
generates one machine code instruction (since assembler is mnemonic code for
machine code)
o
translation is done by table look up where
symbolic names are cross-listed with their numeric values
o
translating assembler into machine code usually
requires two passes through the code.
§
during pass one, a user-defined symbol table is
constructed consisting of the numeric addresses for all symbols used by the
programmer
§
during pass two, table look-up is used to
translate the assembler code into machine code
·
High-Level Language Compilers & Interpreters
o
many to one translation
o
there are four stages to high-level language
translation
§
lexical analysis - identification of syntactic
elements ("tokens")
§
parsing - checking that the program is
syntactically correct
§
code generation - translating
syntactically correct high-level code into machine code
§
optimization - refining the machine code to make
it more efficient (compilers only)
"The
structure of language defines the boundaries of thought"
: Sapir-Whorf hypothesis
High-Level Language Paradigms (The Tower of Babel) - Over time many
high-level computer languages were created. Some languages were created to
"fix" certain perceived problems in early languages. Others were
created to enhance a particular way to solve a problem. Some languages were
created to specifically teach programming. Others were created to force the
programming to write robust code (i.e. code that would execute correctly). Some
languages were "small"; others were "large". Sufficient to
say, there is no "perfect" language; every language has its good
points and its bad points.
- Procedural Languages (a.k.a. Imperative Languages) - programs written in a
procedural language consist of statements that "change the contents
of memory cells". There is heavy reliance on assignment statements.
These languages tend to reflect the underlying structure of von-Neumann
computer architecture. Examples include
- FORTRAN
- COBOL
- C
- BASIC
- Pascal
- Ada
- Python
- Functional Languages (a.k.a. Applicative Languages) - functional language programs
are constructed as a nested sequence of function calls but the functions
used are more general than those found in procedural languages like Pascal
or C++. For example, an operation like addition treated as a function that
takes two arguments and returns their sum. Functional programming makes
heavy use of recursion (where a function calls itself). Examples of
functional languages include
- LISP (LISt Processing)
- Scheme
- APL
- Logic Oriented Languages (a.k.a. Declarative
Languages) - in these languages various "facts" are asserted to
be true and the program deduces other "facts" from them.
Statements in Logic Oriented languages are based on predicate logic; there
are no procedure-like statements like a standard assignment statement that
evaluates an expression and assigns the result to a variable. Examples
include
- Prolog (Programming in
Logic)
- Object Oriented Languages - In one sense OOP
languages are extensions of procedural language to include objects (an
object is a variable that has both values and functions). There are three
terms associated with OOP languages: encapsulation, inheritance,
and polymorphism. Objects encapsulate values and the functions that
operate on those values. Objects can be "derived" from other
objects inheriting their "ancestor's" capabilities. Objects and
identifiers may have similar meanings but differ in details
(polymorphism). OOP programs can be viewed as a collection of
"semi-independent" objects which interact with each other.
Examples of OOP languages or languages which support objects include
- Simula 67 (the original
OOP language)
- C++
- Java
- Python
- Parallel Processing Languages - Like OOP languages,
these languages are for the most part extensions to procedural languages
which provide multiprocessing (i.e. parallel processing) capabilities. For
best results programs written in these language should be run on special
computers which support parallel processing in hardware..
More
Information
Examples of Imperative, Functional and Logical Programs
Fortran-I Code to detect small primes < 100 (imperative)
Note XMODF(N,M) is the Fortran I modulo function
returning n % m
READ
1, N
1 FORMAT (I3)
IF
(N - 2) 10, 100, 10
10 IF (XMODF(N, 2)) 20, 50, 20
20
IF (N - 3) 30, 100, 30
30 IF (XMODF(N, 3)) 40, 100, 40
40
IF (N - 5) 50 ,100, 50
50 IF (XMODF(N, 5)) 60, 100, 60
60
IF (N - 7) 70, 100, 70
70 IF (XMODF(N, 7)) 80, 100 80
80 PRINT 2, N
2 FORMAT (I3,9H IS PRIME)
STOP
100 PRINT 3, N
3 FORMAT(I3, 13H IS NOT PRIME)
STOP
Fortran I program to compute n factorial (imperative)
READ 1, N
1 FORMAT (I3)
NFACT = 1
DO 100 I=1,
N
NFACT =
NFACT * I
100 CONTINUE
PRINT 2,
NFACT
2 FORMAT (I6)
STOP
BASIC Program to compute n factorial (imperative)
10 INPUT "Enter an integer ", N
20 F = 1
30
FOR I = 1 TO N
40 F = F * I
50 NEXT I
60 PRINT F
70 END
Scheme Program to compute n factorial (functional)
(define (factorial n)
( if ( = n 0)
1
( * n (factorial ( - n 1)))
)
)
Scheme Code to detect a small prime < 100 (functional)
( define (smallprime n)
(cond
(( = 2 n) #t)
(( = 3 n) #t)
(( = 5 n) #t)
(( = 7 n) #t)
(( = (remainder n 2) 0) #f)
(( = (remainder n 3) 0) #f)
(( = (remainder n 5) 0) #f)
(( = (remainder n 7) 0) #f)
(else #t)
)
)
Prolog Program to detect Who is Mortal (logic oriented)
predicates
man(symbol)
mortal(symbol)
clauses
man(socrates).
man(plato).
man(aristotle).
mortal(X) if
man(X).
Prolog Code to Detect Small Primes < 100 (logic oriented)
predicates
smallprime(integer)
prime(integer)
clauses
smallprime(2).
smallprime(3).
smallprime(5).
smallprime(7).
prime(X) :- smallprime(X) or
(X mod 2 <> 0) and
(X mod 3 <> 0) and
(X mod 5 <> 0) and
(X mod 7 <> 0).
Special Purpose Languages: The languages listed above are all general
purpose in that each is capable of solving a wide range of problems.
However there is a class of special purpose languages that are more narrowly
aimed at solving specific problems.
- SQL (Structured Query Language) is a specialized
language used to query databases
- PERL (Practical Extraction and Report Language) is a
specialized language that can scan large text files, extract various kinds
of information, and generate reports from it
- HTML (Hyper-Text Markup Language) is a specialized language
used to create web page documents.
A Brief History of Some Important High-Level Procedural Languages
- FORTRAN - Formula Translation: The first high-level language,
it was developed in the mid 50's by John Backus at IBM. Its importance
(aside from being the first high-level language) was that it demonstrated
that a high-level language when compiled could generate efficient
machine code. Thus a FORTRAN programmer in much less time could write
a program that was almost as good as a program written by an Assembler
language programmer. In addition, FORTRAN was easier to learn and easier
to use which opened up programming to a larger segment of the population.
Since 1957 FORTRAN has "evolved" into FORTRAN II, FORTRAN IV,
FORTRAN 77, FORTRAN 90 etc. and it generates very efficient code (read
fast and small) it remains a favorite with scientists and engineers.
- COBOL: - Common Business Oriented Language: COBOL was developed
between 1958-1960 by the CODASYL committee headed by Dr.
Grace Hopper as a language oriented to business users. Very
"English-like" in that the Python statement sum
= a+b would be written as ADD A TO B GIVING SUM in COBOL. Very
good for manipulating files and generating reports (very good formatting
capabilities) but excessively "wordy". It is estimated that most
of the existing code today is COBOL.
- Pascal - named in honor of Blaise
Pascal, a 17th century mathematician, philosopher, theologian and inventor
of the first mechanical calculator called the Pascaline.
(Note: One Wilhelm Schickard actually invented a
mechanical calculator a few years before Pascal but his only model was
destroyed in a fire. Knowledge of Schickard's
calculator is fairly recent when notes describing his calculator were
found). The Pascal language was developed by Niklaus
Wirth in the late 60's early 70's at the Federal Institute of Technology
(ETH) at Zurich (Switzerland) as a language to teach programming. The
language is "small" and designed to force a programmer to write
good code. (Since developing Pascal, Wirth has gone on to develop other
languages like Modula-II and Oberon.).
- BASIC - Beginners All-Purpose Symbolic Instruction Code -
Developed by John Kemeny at Dartmouth College in
the late 60's to teach liberal arts majors how to program. Because it was
such a small language and interpreted (not compiled), it was implemented
on many of the early micro-computers (Bill Gates got his start writing a
BASIC interpreter for the Altair 8800, the world's first microcomputer).
BASIC was run on one of the first time-sharing computer systems allowing
many users to write and execute BASIC programs at the same time.
- C
- Developed by Dennis Ritchie in the early 70's at AT&T Bell Labs, it
was created for systems programming, in this case the UNIX operating
system. Since UNIX was very popular and C was "bundled" with it,
the language took off. One advantage of C (?) is its many
"low-level" language constructs so it has sometimes been called "high-level
assembler". For example, C has the capability to access and
manipulate memory addresses (i.e. the numeric addresses). C++ was developed by Bjarne Stroustrup in the
early 80's to be a better C with object oriented capabilities. (Note - The
'C' language was derived from a language called 'B' (Ken Thompson at Bell
Labs developed 'B' in the last 60's) which in turn was derived from a
language call BCPL (Martin Richards designed BCPL in 1967) which in turn
was developed from a language called CPL developed at Cambridge University
in the 60's (David Hartley & D.W. Barron at Cambridge worked on CPL -
Cambridge Programming Language?) Thus if this scheme of naming
languages were to be continued, 'P' would come next!).
- Ada - Named for Ada Lovelace a 19th century figure who was a popularizer of Charles Babbage's Analytic Engine and
considered to be the world's first "programmer". This language
was developed in the 70' and 80' under the auspices of the Department of
Defense (the DoD "owns" the Ada trademark). Ada is
definitely an "industrial-strength" language with many powerful
constructs (if you're familiar with the military, they tend to over
design). Ada has multi-processing capabilities.
- Python – developed by Guido van Rossum
in the 1980’s (released in 1991) and named after the British comedy act
Monty Python’s Flying Circus. The core of the language is minimal (the
addition of libraries enhances its power), the syntax is simple and the
language itself is interpreted, not compiled making it very flexible. Because
it’s easy to learn and use it makes for a good language to teach
programming; thus it’s sometimes hailed as the new Pascal (see above).
Return
to Comp 150 HomePage