Pascal (Programming Language) : Jump To Navigationjump To Search

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Pascal (programming language)

From Wikipedia, the free encyclopedia


Jump to navigationJump to search
For other uses, see Pascal (disambiguation).

Pascal

• Imperative
Paradigm
• structured

Family Wirth Pascal

Designed by Niklaus Wirth

First appeared 1970; 52 years ago

Typing discipline • Static

• strong

• safe

Scope Lexical

Filename extensions .pp , .pas , .inc

Major implementations

• CDC 6000
• Embarcadero Delphi

• ICL 1900

• Pascal-P

• PDP-11

• PDP-10

• IBM System/370

• VSI Pascal

• Free Pascal

• GNU Pascal

Dialects

• Delphi

• Turbo Pascal

• UCSD Pascal

Influenced by

• ALGOL W

• Simula 67

Influenced

• Ada

• C/AL

• Component Pascal

• Go

• Java[1][2][3]

• Modula / -2 / -3

• Oberon / -2

• Object Pascal

• Oxygene

• Power Fx

• Seed7

• Standard ML

• VHDL

• Structured text

Pascal is an imperative and procedural programming language, designed


by Niklaus Wirth as a small, efficient language intended to encourage good
programming practices using structured programming and data structuring. It is
named in honour of the French mathematician, philosopher and physicist Blaise
Pascal.
Pascal was developed on the pattern of the ALGOL 60 language. Wirth was
involved in the process to improve the language as part of the ALGOL X efforts
and proposed a version named ALGOL W. This was not accepted, and the ALGOL
X process bogged down. In 1968, Wirth decided to abandon the ALGOL X process
and further improve ALGOL W, releasing this as Pascal in 1970.
On top of ALGOL's scalars and arrays, Pascal enables defining complex datatypes
and building dynamic and recursive data structures such as lists, trees and graphs.
Pascal has strong typing on all objects, which means that one type of data cannot
be converted to or interpreted as another without explicit conversions. Unlike C
(and most languages in the C-family), Pascal allows nested procedure definitions
to any level of depth, and also allows most kinds of definitions and declarations
inside subroutines (procedures and functions). A program is thus syntactically
similar to a single procedure or function. This is similar to the block structure of
ALGOL 60, but restricted from arbitrary block statements to just procedures and
functions.
Pascal became very successful in the 1970s, notably on the
burgeoning minicomputer market. Compilers were also available for
many microcomputers as the field emerged in the late 1970s. It was widely used as
a teaching language in university-level programming courses in the 1980s, and
also used in production settings for writing commercial software during the same
period. It was displaced by the C programming language during the late 1980s and
early 1990s as UNIX-based systems became popular, and especially with the
release of C++.
A derivative named Object Pascal designed for object-oriented programming was
developed in 1985. This was used by Apple Computer (for the Lisa and MacIntosh
machines) and Borland in the late 1980s and later developed into Delphi on
the Microsoft Windows platform. Extensions to the Pascal concepts led to the
languages Modula-2 and Oberon.

Contents

• 1History
o 1.1Earlier efforts
o 1.2Pascal
o 1.3Object Pascal
• 2Implementations
o 2.1Early Pascal compilers
o 2.2The Pascal-P system
o 2.3Object Pascal and Turbo Pascal
o 2.4Other variants
• 3Language constructs
o 3.1Data types
o 3.2Subrange types
o 3.3Set types
o 3.4Union types
o 3.5Type declarations
o 3.6File type
o 3.7Pointer types
o 3.8Control structures
o 3.9Procedures and functions
o 3.10Semicolons as statement separators
• 4Resources
o 4.1Compilers and interpreters
o 4.2IDEs
o 4.3Libraries
• 5Standards
o 5.1ISO/IEC 7185:1990 Pascal
o 5.2ISO/IEC 10206:1990 Extended Pascal
o 5.3Variations
▪ 5.3.1Borland-like Pascal compilers
o 5.4List of related standards
• 6Reception
o 6.1Early criticism
• 7See also
• 8References
• 9Further reading

History[edit]
Earlier efforts[edit]
Much of the history of computer language design during the 1960s can be traced to
the ALGOL 60 language. ALGOL was developed during the 1950s with the explicit
goal of being able to clearly describe algorithms. It included a number of features
for structured programming that remain common in languages to this day.
Shortly after its introduction, in 1962 Wirth began working on his dissertation with
Helmut Weber on the Euler programming language. Euler was based on ALGOL's
syntax and many concepts but was not a derivative. Its primary goal was to add
dynamic lists and types, allowing it to be used in roles similar to Lisp. The language
was published in 1965.
By this time, a number of problems in ALGOL had been identified, notably the lack
of a standardized string system. The group tasked with maintaining the language
had begun the ALGOL X process to identify improvements, calling for submissions.
Wirth and Tony Hoare submitted a conservative set of modifications to add strings
and clean up some of the syntax. These were considered too minor to be worth
using as the new standard ALGOL, so Wirth wrote a compiler for the language,
which became named ALGOL W.
The ALGOL X efforts would go on to choose a much more complex
language, ALGOL 68. The complexity of this language led to considerable difficulty
producing high-performance compilers, and it was not widely used in the industry.
This left an opening for newer languages.
Pascal[edit]
Pascal was influenced by the ALGOL W efforts, with the explicit goals of teaching
programming in a structured fashion and for the development of system
software.[4] A generation of students used Pascal as an introductory language in
undergraduate courses.
One of the early successes for the language was the introduction of UCSD Pascal,
a version that ran on a custom operating system that could be ported to different
platforms. A key platform was the Apple II, where it saw widespread use as Apple
Pascal. This led to Pascal becoming the primary high-level language used for
development in the Apple Lisa, and later, the Macintosh. Parts of the
original Macintosh operating system were hand-translated into Motorola
68000 assembly language from the Pascal source code.[5]
The typesetting system TeX by Donald E. Knuth was written in WEB, the
original literate programming system, based on DEC PDP-10 Pascal. Successful
commercial applications like Adobe Photoshop[6] were written in Macintosh
Programmer's Workshop Pascal, while applications like Total
Commander, Skype[7] and Macromedia Captivate were written in Delphi (Object
Pascal). Apollo Computer used Pascal as the systems programming language for
its operating systems beginning in 1980.
Variants of Pascal have also been used for everything from research projects
to PC games and embedded systems. Newer Pascal compilers exist which are
widely used.[8]
Object Pascal[edit]
During work on the Lisa, Larry Tesler began corresponding with Wirth on the idea
of adding object-oriented extensions to the language. This led initially to Clascal,
introduced in 1983. As the Lisa program faded and was replaced by the Macintosh,
a further version was created and named Object Pascal. This was introduced on
the Mac in 1985 as part of the MacApp application framework, and became Apple's
main development language into the early 1990s.
The Object Pascal extensions were added to Turbo Pascal with the release of
version 5.5 in 1989.[9] Over the years, Object Pascal became the basis of
the Delphi system for Microsoft Windows, which is still used for developing
Windows applications, and can cross-compile code to other systems. Free
Pascal is an open source, cross-platform alternative with its own graphical IDE
called Lazarus.

Implementations[edit]
Early Pascal compilers[edit]
The first Pascal compiler was designed in Zürich for the CDC 6000
series mainframe computer family. Niklaus Wirth reports that a first attempt to
implement it in FORTRAN 66 in 1969 was unsuccessful due to FORTRAN 66's
inadequacy to express complex data structures. The second attempt was
implemented in a C-like language (Scallop by Max Engeli) and then translated by
hand (by R. Schild) to Pascal itself for boot-strapping.[10] It was operational by mid-
1970. Many Pascal compilers since have been similarly self-hosting, that is, the
compiler is itself written in Pascal, and the compiler is usually capable of
recompiling itself when new features are added to the language, or when the
compiler is to be ported to a new environment. The GNU Pascal compiler is one
notable exception, being written in C.
The first successful port of the CDC Pascal compiler to another mainframe was
completed by Welsh and Quinn at the Queen's University of Belfast (QUB) in 1972.
The target was the International Computers Limited (ICL) 1900 series. This
compiler, in turn, was the parent of the Pascal compiler for the Information
Computer Systems (ICS) Multum minicomputer. The Multum port was developed –
with a view to using Pascal as a systems programming language – by Findlay,
Cupples, Cavouras and Davis, working at the Department of Computing Science
in Glasgow University. It is thought that Multum Pascal, which was completed in
the summer of 1973, may have been the first 16-bit implementation.
A completely new compiler was completed by Welsh et al. at QUB in 1977. It
offered a source-language diagnostic feature (incorporating profiling, tracing and
type-aware formatted postmortem dumps) that was implemented by Findlay and
Watt at Glasgow University. This implementation was ported in 1980 to the ICL
2900 series by a team based at Southampton University and Glasgow University.
The Standard Pascal Model Implementation was also based on this compiler,
having been adapted, by Welsh and Hay at Manchester University in 1984, to
check rigorously for conformity to the BSI 6192/ISO 7185 Standard and to
generate code for a portable abstract machine.
The first Pascal compiler written in North America was constructed at
the University of Illinois under Donald B. Gillies for the PDP-11 and generated
native machine code.
The Pascal-P system[edit]
To propagate the language rapidly, a compiler porting kit was created in Zürich that
included a compiler that generated so called p-code for a virtual stack machine,
i.e., code that lends itself to reasonably efficient interpretation, along with an
interpreter for that code – the Pascal-P system. The P-system compilers were
named Pascal-P1, Pascal-P2, Pascal-P3, and Pascal-P4. Pascal-P1 was the first
version, and Pascal-P4 was the last to come from Zürich. The version termed
Pascal-P1 was coined after the fact for the many different sources for Pascal-P
that existed. The compiler was redesigned to enhance portability, and issued as
Pascal-P2. This code was later enhanced to become Pascal-P3, with an
intermediate code backward compatible with Pascal-P2, and Pascal-P4, which was
not backward compatible.
The Pascal-P4 compiler–interpreter can still be run and compiled on systems
compatible with original Pascal. However, it only accepts a subset of the Pascal
language.
Pascal-P5, created outside the Zürich group, accepts the full Pascal language and
includes ISO 7185 compatibility.
UCSD Pascal branched off Pascal-P2, where Kenneth Bowles used it to create
the interpretive UCSD p-System. It was one of three operating systems available at
the launch of the original IBM Personal Computer.[11] UCSD Pascal used an
intermediate code based on byte values, and thus was one of the
earliest bytecode compilers. Apple Pascal was released in 1979 for the Apple 2
and Apple 3 computer systems. It was an implementation of, or largely based on,
UCSD Pascal. Pascal-P1 through Pascal-P4 was not, but rather based on the CDC
6600 60-bit word length.
A compiler based on the Pascal-P5 compiler, which created native binary object
files, was released for the IBM System/370 mainframe computer by the Australian
Atomic Energy Commission; it was named the AAEC Pascal 8000 Compiler after
the abbreviation of the name of the commission.[12]
Object Pascal and Turbo Pascal[edit]
Apple Computer created its own Lisa Pascal for the Lisa Workshop in 1982, and
ported the compiler to the Apple Macintosh and MPW in 1985. In 1985 Larry
Tesler, in consultation with Niklaus Wirth, defined Object Pascal and these
extensions were incorporated in both the Lisa Pascal and Mac Pascal compilers.
In the 1980s, Anders Hejlsberg wrote the Blue Label Pascal compiler for
the Nascom-2. A reimplementation of this compiler for the IBM PC was marketed
under the names Compas Pascal and PolyPascal before it was acquired
by Borland and renamed Turbo Pascal.
Turbo Pascal became hugely popular, thanks to an aggressive pricing strategy,
having one of the first full-screen IDEs, and very fast turnaround time (just seconds
to compile, link, and run). It was written and highly optimized entirely in assembly
language, making it smaller and faster than much of the competition.
In 1986, Anders ported Turbo Pascal to the Macintosh and incorporated Apple's
Object Pascal extensions into Turbo Pascal. These extensions were then added
back into the PC version of Turbo Pascal for version 5.5. At the same
time Microsoft also implemented the Object Pascal compiler.[13][14] Turbo Pascal 5.5
had a large influence on the Pascal community, which began concentrating mainly
on the IBM PC in the late 1980s. Many PC hobbyists in search of a structured
replacement for BASIC used this product. It also began to be adopted by
professional developers. Around the same time a number of concepts were
imported from C to let Pascal programmers use the C-based application
programming interface (API) of Microsoft Windows directly. These extensions
included null-terminated strings, pointer arithmetic, function pointers, an address-of
operator, and unsafe typecasts.
Turbo Pascal and other derivatives with unit or module structures are modular
programming languages. However, it does not provide a nested module concept or
qualified import and export of specific symbols.
Other variants[edit]
Super Pascal is a variant that added non-numeric labels, a return statement and
expressions as names of types.
TMT Pascal was the first Borland-compatible compiler for 32-bit DOS protected
mode, OS/2, and Win32 operating systems. The TMT Pascal language was the
first one to allow function and operator overloading.
The universities of Wisconsin-Madison, Zürich, Karlsruhe,
and Wuppertal developed the Pascal-SC[15][16] and Pascal-XSC[17][18][19] (Extensions for
Scientific Computation) compilers, aimed at programming numerical computations.
Development for Pascal-SC started in 1978 supporting ISO 7185 Pascal level 0,
but level 2 support was added at a later stage.[20] Pascal-SC originally targeted
the Z80 processor, but was later rewritten for DOS (x86) and 68000. Pascal-XSC
has at various times been ported to Unix (Linux, SunOS, HP-UX, AIX) and
Microsoft/IBM (DOS with EMX, OS/2, Windows) operating systems. It operates by
generating intermediate C source code which is then compiled to a native
executable. Some of the Pascal-SC language extensions have been adopted
by GNU Pascal.
Pascal Sol was designed around 1983 by a French team to implement a Unix-
like system named Sol. It was standard Pascal level-1 (with parameterized array
bounds) but the definition allowed alternative keywords and predefined identifiers
in French and the language included a few extensions to ease system
programming (e.g. an equivalent to lseek).[21] The Sol team later on moved to
the ChorusOS project to design a distributed operating system.[22]
IP Pascal was an implementation of the Pascal programming language
using Micropolis DOS, but was moved rapidly to CP/M-80 running on the Z80. It
was moved to the 80386 machine types in 1994, and exists today as Windows/XP
and Linux implementations. In 2008, the system was brought up to a new level and
the resulting language termed "Pascaline" (after Pascal's calculator). It includes
objects, namespace controls, dynamic arrays, and many other extensions, and
generally features the same functionality and type protection as C#. It is the only
such implementation that is also compatible with the original Pascal
implementation, which is standardized as ISO 7185.

Language constructs[edit]
Pascal, in its original form, is a purely procedural language and includes the
traditional array of ALGOL-like control structures with reserved words such
as if , then , else , while , for , and case , ranging on a single statement or
a begin - end statements block. Pascal also has data structuring constructs not
included in the original ALGOL 60 types, like records,
variants, pointers, enumerations, and sets and procedure pointers. Such constructs
were in part inherited or inspired from Simula 67, ALGOL 68, Niklaus Wirth's
own ALGOL W and suggestions by C. A. R. Hoare.
Pascal programs start with the program keyword with a list of external file
descriptors as parameters[23] (not required in Turbo Pascal etc.); then follows the
main block bracketed by
the begin and end keywords. Semicolons separate statements, and the full
stop (i.e., a period) ends the whole program (or unit). Letter case is ignored in
Pascal source.
Here is an example of the source code in use for a very simple "Hello, World!"
program:

program HelloWorld(output);
begin
Write('Hello, World!')
{No ";" is required after the last statement of a block -
adding one adds a "null statement" to the program, which is
ignored by the compiler.}
end.

Data types[edit]
A Type Declaration in Pascal is used to define a range of values which a
variable of that type is capable of storing. It also defines a set of operations that
are permissible to be performed on variables of that type. The predefined types
are:

Data
Type of values which the variable is capable of storing
type

integer integer (whole) numbers

real floating-point numbers

boolean the values True or False

char a single character from an ordered character set

set equivalent to an array of boolean values


array a countable group of any of the preceding data types or records

record A collection of any of the preceding data types

a sequence or "string" of characters is declared as a "packed array of char" with a


string starting index of 1. These can be assigned string constants and individual characters
can be accessed as elements of the array.

The range of values allowed for the basic types (except boolean) is
implementation defined. Functions are provided for some data conversions. For
conversion of real to integer , the following functions are
available: round (which rounds to integer using banker's rounding)
and trunc (rounds towards zero).
The programmer has the freedom to define other commonly used data types
(e.g. byte, string, etc.) in terms of the predefined types using Pascal's type
declaration facility, for example

type
byte = 0..255;
signed_byte = -128..127;
string = packed array[1..255] of char;

Often-used types like byte and string are already defined in many
implementations.
Normally the system will use a word to store the data. For instance,
the byte type may be stored in a machine integer - 32 bits perhaps - rather
than an 8-bit value. Pascal does not contain language elements that allow
the basic storage types to be defined more granularly. This capability was
included in a number of Pascal extensions and follow-on languages, while
others, like Modula-2, expanded the built-in set to cover most machine data
types like 16-bit integers.
The packed keyword tells the compiler to use the most efficient method of
storage for the structured data types: sets, arrays and records, rather than
using one word for each element. Packing may slow access on machines
that do not offer easy access to parts of a word.
Subrange types[edit]
Subranges of any ordinal data type (any simple type except real) can also
be made:

var
x : 1..10;
y : 'a'..'z';

Set types[edit]
In contrast with other programming languages from its time, Pascal
supports a set type:[24]

var
Set1 : set of 1..10;
Set2 : set of 'a'..'z';

A set is a fundamental concept for modern mathematics, and they


may be used in many algorithms. Such a feature is useful and may
be faster than an equivalent construct in a language that does not
support sets. For example, for many Pascal compilers:

if i in [5..10] then ...

executes faster than:

if (i > 4) and (i < 11) then ...

Sets of non-contiguous values can be particularly useful, in


terms of both performance and readability:

if i in [0..3, 7, 9, 12..15] then ...

For these examples, which involve sets over small


domains, the improved performance is usually achieved
by the compiler representing set variables as bit vectors.
The set operators can then be implemented efficiently as
bitwise machine code operations.
Union types[edit]
This section is an excerpt from Union type § Pascal.[edit]
In Pascal, there are two ways to create unions. One is the
standard way through a variant record. The second is a
nonstandard means of declaring a variable as absolute,
meaning it is placed at the same memory location as
another variable or at an absolute address. While all
Pascal compilers support variant records, only some
support absolute variables.
For the purposes of this example, the following are all
integer types: a byte is 8-bits, a word is 16-bits, and an
integer is 32-bits.
The following example shows the non-standard absolute
form:

VAR
A: Integer;
B: Array[1..4] of Byte absolute A;
C: Integer absolute 0;

In the first example, each of the elements of the array B


maps to one of the specific bytes of the variable A. In the
second example, the variable C is assigned to the exact
machine address 0.
In the following example, a record has variants, some of
which share the same location as others:

TYPE
TSystemTime = record
Year, Month, DayOfWeek, Day : word;
Hour, Minute, Second, MilliSecond: word;
end;
TPerson = RECORD
FirstName, Lastname: String;
Birthdate: TSystemTime;
Case isPregnant: Boolean of
true: (DateDue:TSystemTime);
false: (isPlanningPregnancy: Boolean);
END;

Type declarations[edit]
Types can be defined from other types using type
declarations:

type
x = integer;
y = x;
...

Further, complex types can be constructed from


simple types:

type
a = array[1..10] of integer;
b = record
x : integer;
y : char {extra semicolon not strictly required}
end;
c = file of a;

File type[edit]
As shown in the example above, Pascal files are
sequences of components. Every file has a buffer
variable which is denoted by f^. The
procedures get (for reading) and put (for writing)
move the buffer variable to the next element.
Read is introduced such that read(f, x) is the same
as x := f^; get(f);. Write is introduced such
that write(f, x) is the same as f^ := x; put(f); The
type text is predefined as file of char. While the
buffer variable could be used for inspecting the
next character to be used (check for a digit before
reading an integer), this leads to serious problems
with interactive programs in early
implementations, but was solved later with the
"lazy I/O" concept.
In Jensen & Wirth Pascal, strings are represented
as packed arrays of chars; they therefore have
fixed length and are usually space-padded.
Pointer types[edit]
Pascal supports the use of pointers:

type
pNode = ^Node;
Node = record
a : integer;
b : char;
c : pNode
end;
var
NodePtr : pNode;
IntPtr : ^integer;

Here the variable NodePtr is a pointer to the


data type Node, a record. Pointers can be
used before they are declared. This is
a forward declaration, an exception to the rule
that things must be declared before they are
used.
To create a new record and assign the
value 10 and character A to the
fields a and b in the record, and to initialise the
pointer c to the null pointer ("NIL" in Pascal),
the statements would be:

New(NodePtr);
...
NodePtr^.a := 10;
NodePtr^.b := 'A';
NodePtr^.c := NIL;
...

This could also be done using


the with statement, as follows:

New(NodePtr);
...
with NodePtr^ do
begin
a := 10;
b := 'A';
c := NIL
end;
...

Inside of the scope of


the with statement, a and b refer to the
subfields of the record
pointer NodePtr and not to the record
Node or the pointer type pNode.
Linked lists, stacks and queues can be
created by including a pointer type field
(c) in the record.
Unlike many languages that feature
pointers, Pascal only allows pointers to
reference dynamically created variables
that are anonymous, and does not
allow them to reference standard static
or local variables. Pointers also must
have an associated type, and a pointer
to one type is not compatible with a
pointer to another type (e.g. a pointer to
a char is not compatible with a pointer
to an integer). This helps eliminate the
type security issues inherent with other
pointer implementations, particularly
those used for PL/I or C. It also
removes some risks caused
by dangling pointers, but the ability to
dynamically deallocate referenced
space by using the dispose function
(which has the same effect as
the free library function found in C)
means that the risk of dangling pointers
has not been eliminated[25] as it has in
languages such as Java and C#, which
provide automatic garbage
collection (but which do not eliminate
the related problem of memory leaks).
Some of these restrictions can be lifted
in newer dialects.
Control structures[edit]
Pascal is a structured
programming language, meaning that
the flow of control is structured into
standard statements, usually without
'goto' commands.

while a <> b do WriteLn('Waiting');

if a > b then WriteLn('Condition met') {no semicolon allowed


before else}
else WriteLn('Condition not met');

for i := 1 to 10 do {no semicolon here as it would detach the next


statement}
WriteLn('Iteration: ', i);

repeat
a := a + 1
until a = 10;

case i of
0 : Write('zero');
1 : Write('one');
2 : Write('two');
3,4,5,6,7,8,9,10: Write('?')
end;

Procedures and
functions[edit]
Pascal structures programs into
procedures and functions.
Generally, a procedure is used for
its side effects, whereas a function
is used for its return value.

program Printing;

var i : integer;

procedure PrintAnInteger(j : integer);


begin
...
end;

function triple(const x: integer): integer;


begin
triple := x * 3
end;

begin { main program }


...
PrintAnInteger(i);
PrintAnInteger(triple(i))
end.

Procedures and functions can


be nested to any depth, and the
'program' construct is the logical
outermost block.
By default, parameters are
passed by value. If 'var'
precedes a parameter's name, it
is passed by reference.
Each procedure or function can
have its own declarations of
goto labels, constants, types,
variables, and other procedures
and functions, which must all be
in that order. This ordering
requirement was originally
intended to allow
efficient single-pass compilation.
However, in some dialects (such
as Delphi) the strict ordering
requirement of declaration
sections has been relaxed.

You might also like