Pascal (programming language)
|Paradigms :||imperative , structured|
|Typing :||strong , static|
|Dialects:||UCSD- Pascal, Borland Pascal Turbo Pascal|
|Affected:||Modula-2 , Ada , Oberon , Object Pascal , WEB , Seed7|
Pascal is an imperative programming language developed in the early 1970s .
Pascal is a further development of Algol 60 . In its syntax it is based very much on English grammar. This is supposed to improve readability for newcomers to programming; Pascal is therefore particularly suitable as a teaching language. It found its widespread use in professional programming as Borland / Turbo Pascal (later Object Pascal ) - significantly expanded and improved versions compared to the original Pascal.
An important concept that Wirth used is strong typing : Variables are already assigned to a certain data type at compilation time, and this cannot be changed afterwards. Type strictness means that value assignments are only allowed under variables of the same type. In Pascal there are only a few exceptions to this strict constraint:
- Value assignments of the form [variable of type
:=[value of type
- In Extended Pascal, value assignments of integer or real numbers to variables of the type
- Value assignments of restricted value ranges of an ordinal type. Example:
type int10 = 1 .. 10;Values of this type can then be assigned to integer variables.
- Value assignments of quantities of a restricted value range of an ordinal type. Example:
type set10 = set of 1 .. 10; set20 = set of 1 .. 20;Values of the type
set10can then be
set20assigned to variables of the type .
- Value assignments to restricted value ranges of an ordinal type or a set. In this case, values or quantity elements outside the target range cause a runtime error.
- Value assignments between different string types, including
Charcharacter arrays according to Standard Pascal and the different
Stringtypes in Extended Pascal, Borland Pascal, etc. If the length of the value is not within the capacity of the target, a runtime error results here too .
Today Pascal is used in the university sector (development / training) and in safety-critical areas (e.g. traffic engineering, energy supply, medical technology, space travel, the military, partly in banking and insurance). This is mainly due to the good testability and maintainability of the code and the clear assignment of the variables. This is how the operations control system IV introduced in 2005 for the Transrapid test facility in Emsland in Pascal is programmed. A Pascal-like notation has always been used in computer science and mathematics to represent algorithms. For didactic reasons, such as the strict type, high level of error security and freely available portable Pascal compilers ( Free Pascal , GNU Pascal ), Pascal is also frequently used in current computer science classes . In the hobby area, Pascal achieved a very widespread use at times, but this decreased again with newer Microsoft Windows versions.
program Hallo(output); begin writeln('Hallo Welt') end.
or in newer Pascal versions the program looks like this:
program Hallo; begin writeln('Hallo Welt') end.
A side detail: In some old examples you can find a
WRITELNcommand. This was only necessary because the I / O architecture of the CDC Cyber 6000, on which Wirth developed the first Pascal compiler, required it to empty the internal buffers - otherwise no output would have been seen in interactive mode. IDEs ( Integrated Development Environments) could be forced with this READLN not to switch back to editor mode immediately at the end of program execution, which would otherwise have made the output with some IDEs (e.g. Turbo Pascal) disappear. In any case, this was never necessary in batch operation .
Standard data types
Whole numbers , for example
Floating point numbers , such as
|char||a single character, such as
A data type must be assigned to each variable in a variable declaration . The data type determines which possible values the variable can save while the program is running. In Pascal there are four simple standard types, namely
The length of the various data types except boolean depend on the compiler. Many implementations also have other predefined simple data types.
Self-defined data types
In Pascal it is possible to define your own simple data types in addition to the four standard types. These are either enumerated types or section types. In order to define a new enumeration type, all the values that a variable of this type should be able to store are written down in sequence. In the following example, a new type with the name is
tBesteckdefined, whereby the optional
tat the beginning should make it clear for the further course of the program that it is a name of a type:
tBesteck = (Gabel, Messer, Loeffel)
A section type is defined when the range of values of a more comprehensive type is not fully exhausted. If, for example, a variable should only be able to store numbers between 0 and 255, it is possible to
integerrestrict the data type to this subset. The following example
tBytedefines a new type named :
tByte = 0 .. 255;
Structured data types
Pascal offers programmers four structured data types that allow multiple values to be stored in a variable. A fixed number of values that cannot be changed during runtime can be stored in a field ( array ), all of which must be of the same data type. In the following example, a field is defined in which a maximum of 100 whole numbers can be stored:
tBeispielFeld = array [1..100] of integer;
Values of the same data type can also be stored in a set ( set ), but it is only recorded which values are contained in the set, whereby it does not matter how often an individual value was mentioned or in which order the values were mentioned. An example of the definition of a set:
tBeispielMenge = set of 13..55
In a composite ( record ) a fixed number of values can be stored, and the values may be of different data type. An example of a definition:
tPerson = record Alter : 0..150; Haarfarbe : (braun, blond, schwarz); Groesse : real; kurzsichtig : boolean end;
An arbitrarily long sequence of values of the same type can in Pascal using the data type of file ( file ) can be represented. Example of a definition:
tBeispielDatei = file of integer
The main purpose of using pointers in Pascal is to create different data structures, depending on the programmer's ideas. With the help of pointers, for example, lists and trees can be created, which offer advantages over fields, sets, groups and files when storing data. Another advantage of pointers is that pointer variables can request the space they need at runtime. The programmer does not have to define in advance how much space the variables of his program will require during execution and can basically start from the minimum, beyond which, if necessary, can be expanded.
The concept of nesting procedures and functions is characteristic of Pascal. In the declaration part of a procedure or function, other procedures and functions can be defined that are not visible outside. Example:
program HalloWelt; procedure foo; procedure bar; begin writeln('Hallo Welt'); end; begin bar end; begin foo; (* kein Compilerfehler *) bar (* Compilerfehler, da bar nicht sichtbar *) end.
Conditional statement and branch
The if and case statements are available as conditional statements and branches in Pascal. An example of an if branch:
if 1 = 2 then writeln ('wird nicht ausgeführt') else if 1 = 1 then writeln ('wird ausgeführt') else writeln ('wird nicht ausgeführt');
Pascal offers three types of loops . If a termination condition is to be checked before each loop run, the while statement is used, and the repeat statement is used to check after each run. The for statement is used for a specified number of runs.
while wert < 100 do wert := wert + 1;
for i:= 1 to 100 do wert := wert + 1;
repeat wert := wert + 1 until wert > 100;
The WITH statement is not a loop; it only simplifies access to components of a record.
Jump instructions can
gotoalso be used with, but its use is controversial (see article on jump instructions).
The first Pascal compiler itself was created on the CDC Cyber 6000 at ETH Zurich . This then resulted in Pascal 6000, which can be seen as the first operational version of a compiler of the language.
A second Pascal compiler - the P4 "Portable Pascal Compiler" by Urs Ammann , Kesav Nori and Christian Jacobi - also came from ETH Zurich. The P4 generated a platform-independent intermediate language designed as an assembly language , the P-Code , which is interpreted by a virtual machine (a program). Compilers based on the P4 emerged later. The most common version became known as UCSD Pascal , which was implemented on many systems, including a. on Apple II and Texas Instruments TI-99 / 4A and contributed significantly to the spread of the language both (first) in the United States and later in Europe. In May 2006 the source texts of UCSD Pascal were released.
Most of the Pascal compilers were single-pass compilers ; H. the compiler only has to read and parse the source once . The language was designed to do just that. Given the low speed of the computers at the time, this was a great advantage.
Very high process reliability
Since Pascal and the quasi-standard Borland / Turbo-Pascal provide for a strict separation of different types and observe the assignment compatibility of expressions, implicit type conversions , unlike in C, practically do not occur. In particular when transferring data (e.g. from external files, but also within the program) to functions or procedures, the compiler can check the correctness of the types during compilation.
No zero-terminated strings
Standard Pascal had character string literals, but no type
string(see above). In most later implementations, strings were
chardefined as arrays with the first field (byte) used to store length. This resulted in a length limit of 255 characters. The strings used in the C standard library , however, are NUL-terminated strings. By storing the length in Pascal, various operations such as string concatenation can be carried out more efficiently (no searching through to the NUL character required). Larger strings had to be defined by yourself (e.g. as an array of char ), unless they were already implemented by the compiler manufacturer (e.g. in Borland Pascal 7). In the Extended Pascal standard, character strings were defined as a scheme type. Here, too, has the capacity specified, but it is written in parentheses, and there is no formal length limit:
Strict separation between program, functions and procedures
Pascal makes a strict distinction between a function (with a return value) and a procedure (no return value). A function may not be called as a procedure - i. H. the return value must always be received. However, since Turbo Pascal 4.0 (1987) it is possible to call functions without receiving the function result. Furthermore, an executable program in Pascal is identified by the keyword
PROGRAM, whereas in C the entry point for a program is the function
main, which apart from its name does not differ from other functions.
In contrast to C, programs, functions and procedures are divided into three parts: In addition to the header, which contains the name and signature , there is a separate declaration part in which types, variables and nested procedures / functions are declared and a definition part in which they are implemented . In C there is only one function head and function body that unites the declaration and definition part. In C, functions may not be defined nested.
In contrast to C, Pascal is case-insensitive with regard to keywords, identifiers of variables, functions or procedures .
The semicolon is not interpreted as a command termination as in C, but as a separator between statements. In front of one
UNTILcan be omitted. As a
ELSErule, it must not be placed in front of you, otherwise the
IFbranch would be regarded as closed. One case in which it comes (and must) come before the ELSE is at the end of a CASE listing.
The same delimiter, namely the apostrophe, is used for character string literals and character literals.
Assignments to variables are defined by the sequence
:=, the equals sign alone is used for equality comparison and the definition of constants. This means that "mathematically incorrect" expressions, e.g. B. i = i + 1 avoided. Abbreviated notations for assignments such as i ++ instead of i = i + 1 or i * = 2 instead of i = 2 * i do not exist in Pascal.
Pascal uses the operators
div(only for Integer , in real it is
<>instead of the C operators
!=. Niklaus Wirth explicitly ruled out the "short circuit" functionality of
|| in C (as soon as the result is certain, namely when the left part is
orTRUE, the right part is no longer evaluated), since internal compiler optimization processes change the order of the evaluation can. Later Pascal versions implemented a locally or globally switchable “short circuit” functionality so that the programmer can decide in each individual case which type of evaluation he wants. In standard Pascal, there is no bit operations for integer (such as
~C), but instead the type
set of(with the operators
-for intersection, union, and difference).
Strict type separation
Pascal operates a strict separation of types so that, among other things, the type is
Charnot interchangeable with
Integer. The conversion must be carried out with the functions
chr(). This avoids hidden (or unwanted) conversions; z. B. the instruction i = 7/2 (with i as
Integer) leads to an error message, because the operator / is an operator that has
Realas a result. Later Pascal versions (e.g. Turbo Pascal) also introduced “explicit” type conversions , so-called “type casts”, so that e.g. B.
longint(i)could be extended or restricted with the value range of the integer variable i. In standard Pascal, a detour via (the misuse of) variant records (
unionin C) had to be taken.
In Pascal there is no pointer arithmetic, iterations over arrays can only be done using the index.
Wirth's first publication of the new programming language appeared in 1971, a revised and expanded version of this report in 1973. Another year later, a report written with Kathleen Jensen followed, which Wirth described as the ultimate reference for programmers and implementers; he called the Pascal described in it Standard Pascal.
There are three standards related to Pascal:
- Standard Pascal: ANSI / IEEE770X3.97-1993 or ISO 7185: 1990;
- Extended Pascal: ANSI / IEEE770X3.160-1989 or ISO / IEC 10206: 1991;
- as well as a draft for "Object-Oriented Extensions to Pascal".
However, as with most other programming languages, very few compilers are fully compatible with these standards. This fact led Scott A. Moore to the biting remark "Pascal is, unfortunately, very much a great improvement on its successors" ("Pascal is unfortunately pretty much a big improvement on his successors" - at that time already a popular phrase).
Even large compilers like Delphi or Free Pascal are still missing some elements from Standard Pascal, while Extended Pascal is hardly supported by anyone. Only Prospero Pascal is fully compatible with Extended Pascal, while GNU Pascal also strives for full compatibility.
Pascal and Mac OS
Historically interesting is the fact that Apple relied on Pascal when developing the operating system and the applications running on it for the Apple II successor Macintosh . For this purpose, Apple developed its own Object Pascal variant in a team led by Larry Tesler in consultation with Niklaus Wirth, which was derived from an earlier object-oriented Pascal variant called Clascal, which was already available on Lisa . Object Pascal was required to support MacApp, a Macintosh application framework that would now come under the term class library. Object Pascal was completed in 1986. Apple ended the focus on Object Pascal with the conversion from Motorola 68K CPUs to PowerPC CPUs from IBM in 1994 with the operating system version Mac OS 7.5. For the purpose of backward compatibility, the transfer of parameters for operating system calls remained based on Pascal conventions until the last Mac OS version 9.2.2 of 2002.
Implementations (compilers, interpreters and IDEs)
Pascal had great success in the university area from the start, which u. a. This is also reflected in the Ada programming language , which is very much based on the Pascal syntax. It was also widely used, also in the commercial sector, with products from Borland Turbo Pascal , Borland Pascal and Delphi. These versions are characterized by strong extensions of the original language standard. The object orientation was introduced with version 5.5.
- UCSD Pascal
- A Pascal implementation from the University of California , San Diego (UCSD).
- Free Pascal
- Among the GNU Lesser General Public License standing open-source generating project, which has set itself the goal of a free 32/64-bit compiler that 100 percent should be compatible with Turbo Pascal and Delphi and now a slight Porting of Pascal programs to almost all common operating systems and hardware platforms enabled.
- GNU Pascal (GPC)
- A Pascal compiler designed as a front end to the GNU Compiler Collection . It is not part of the GCC itself, but is platform-independent through the use of the GCC. GPC is no longer being developed.
- Virtual Pascal
- This now free implementation of a Borland-compatible Pascal compiler for OS / 2 has also been ported to other platforms, but has not been further developed since 2005.
Integrated development environments
- A development environment for Free Pascal, which also provides various components. The IDE is designed very similar to Delphi and uses the GTK + as a graphics toolkit under Unix ; under Windows (win32 / win64 / wince) it is based on the native API , and on Apple operating systems, the native Carbon API or the X Window System can be used. In addition, Lazarus supports cross compiling so that software for other platforms such as Windows CE , OS / 2, Palm OS or Nintendo DS can be developed.
- An IDE running under Windows, which can be based on Free-Pascal or GNU-Pascal as a compiler. It will no longer be developed.
- Kyan Pascal
- A Pascal development environment that can be run on the Apple II home computer, published in 1986 by Kyan Software, including editor, assembler, macro assembler and programming course.
- Kyan Pascal 128
- A version further developed by Kyan Software in 1987 for the home computers Commodore 128 and Atari 800 .
- Pascal C128
- A Pascal development environment with compiler, editor and linker that was published by Markt + Technik Verlag and can run on the Commodore 128 in C128 mode.
- Professional Pascal Plus
- A Pascal development environment with compiler, editor and linker that was published by Data Becker in 1987 and can run on the Commodore 128 in C128 mode.
- Super Pascal 128
- A Pascal development environment including editor, assembler and utility package that was published by Abacus Software in 1986 and runs on the Commodore 128 in C128 mode.
Pascal was named after the French mathematician Blaise Pascal . The development of the programming language Algol (until 1958) as well as that of its successors was the product of development committees. Niklaus Wirth played a key role in the creation of the descendant Algol W, which was developed in Zurich and completed in 1966. He later reported on the disadvantages of joint development. Consensus within the committee had to be laboriously worked out, and the practical-thinking Wirth, who saw himself not only as a scientist but also as an engineer, found some of his colleagues' development goals to be exaggerated and unrealizable.
In 1968 Wirth received a professorship at the ETH Zurich, where, in addition to teaching, he found time to begin developing a new language. This time he worked alone and was able to finish Pascal in 1969. According to Wirth, one of the reasons for the new development was the lack of a language suitable for university teaching. Neither Fortran , which was too unscientific, nor Algol, which was unsuitable for practical engineering, could meet Wirth's demands. In addition to programming lessons, Pascal should also be used in business, research and the development of compilers and operating systems.
After Wirth had completed the language in 1969, the development of the first Pascal compiler followed a year later. This was written for computers of the CDC 6000 series and went into operation in 1970. In 1971 Wirth published the description of the language, which was only 28 pages long, and in 1972 teachers were able to use Pascal for the first time for introductory programming courses. In 1973 a revised and 54 page long revision of the specification followed.
“I was often asked how to 'invent' a programming language. There is no real answer to this, but it is certainly a matter of programming experience and careful consideration. Sometimes I say, 'The way you design an airplane. You have to identify a certain number of necessary components and then assemble them into a functioning whole. ' This answer may not be entirely satisfactory, but at least the same thing happens to the result in both cases, either it flies or it crashes. "
Since the language was designed as a teaching language, the standard Pascal had some features that made commercial use difficult: The concept for file access ("file I / O") was not powerful, the runtime library was not very extensive, and strings could only be detoured (packed array) can be used directly in the code. This meant that practically every commercial implementation offered its own solutions, which initially (similar to the diversification in C) led to its own Pascal dialects and thus to compatibility problems. With the monopoly dominance of Borland's Turbo Pascal products , these differences almost completely disappeared.
Another major disadvantage was that modularization in the sense of separate compilation was not planned - which was changed with the further development of Modula-2 . Platform-specific implementations provided their own solutions for this (e.g. the units of Turbo Pascal, continued in equivalent unit implementations in Freepascal and Delphi, or later the modules in ANSI / ISO Extended Pascal).
- Pascal-XSC Pascal dialect with "Language eXtensions for Scientific Computation"
- Oberon , Oberon-2
- POW! - based on Oberon and Pascal
- Component Pascal - further development of Oberon-2
- Embarcadero Delphi - Object-oriented Pascal as a further development of Borland Pascal .
- Free Pascal - a free implementation that is largely compatible with Delphi. There is also the Delphi-like IDE Lazarus .
- RemObjects Chrome, Oxygene, now licensed as Delphi Prism by CodeGear
- Pascal Script
- Concurrent Pascal - Pascal for concurrent programs
- HP PPL , a Pascal variant for computer algebra developed by HP
- Niklaus Wirth: The Programming Language Pascal. In: Acta Informatica. Volume 1, 1971, pp. 35-63. (on-line)
- Niklaus Wirth: The Programming Language Pascal. Revised Report. Swiss Federal Institute of Technology, Zurich 1973. (online)
- Niklaus Wirth: Algorithms and Data Structures. Stuttgart 1975, ISBN 3-519-02330-X .
- Niklaus Wirth: Pascal and its Successors. In: Manfred Broy, Ernst Denert (Ed.): Software Pioneers: Contributions to Software Engineering. Springer-Verlag, Berlin / Heidelberg 2002, ISBN 3-540-43081-4 , pp. 108-119. (on-line)
- Kathleen Jensen, Niklaus Wirth: Pascal. User Manual and Report. (= Lecture notes in computer science. 18). Springer, Berlin 1974.
- Kathleen Jensen, Niklaus Wirth: Pascal User Manual and Report. ISO Pascal standard. 4th edition. Springer-Verlag, 1991, ISBN 3-540-97649-3 .
- Steven Pemberton, Martin Daniels, Ellis Horwood: Pascal Implementation: The P4 Compiler and Interpreter. ISBN 0-13-653031-1 . ( online )
- ISO-Pascal overview page. Language reference, Pascal grammar, history, etc. (English)
- UCSD-Pascal source code (English)
- Brian W. Kernighan : Why Pascal is Not My Favorite Programming Language. AT&T Bell Laboratories, Murray Hill, New Jersey 1981. An article that explains the disadvantages of Pascal in detail using examples and compares Pascal with other programming languages such as C or Fortran.
- Niklaus Wirth: The Programming Language Pascal. In: Acta Informatica. Volume 1, 1971, pp. 35-63, doi: 10.1007 / BF00264291 .
- M. Jordan: "Day of Inventors" Today: Niklaus Wirth. www.land-der-erfinder.ch, February 15, 2011, accessed on May 21, 2012 .
- Andreas Schwill: Programming styles in the beginning class. (PDF) University of Paderborn , Department of Mathematics / Computer Science, accessed on May 21, 2012 .
- Programming styles in the beginning class. delphi-treff.de, accessed on May 21, 2012 .
- Kathleen Jensen, Niklaus Wirth: Pascal. User Manual and Report. Springer, Berlin 1974.
- Kathleen Jensen, Niklaus Wirth: Pascal. User Manual and Report. Springer, Berlin 1974, foreword
- N. Wirth, CAR Hoare: A Contribution to the development of ALGOL. In: Comm. ACM. 9, 6, June 1966, pp. 413-432.
- Niklaus Wirth: Pascal and its Successors. 2002, pp. 108-119.
- Niklaus Wirth: The Programming Language Pascal. 1971.
- Niklaus Wirth: The Programming Language Pascal. Revised Report. 1973.
- Niklaus Wirth: Pascal and its Successors. 2002, pp. 108-119: Many times I have been asked how one "invents" a programming language. One cannot really tell, but it certainly is a matter of experience with the subject of programming, and of careful deliberation. Sometimes I answered: "Like one designs an airplane. One must identify a number of necessary building blocks and materials, and then assemble and combine them properly to a functioning whole". This answer may not be entirely satisfactory, but at least in both cases the result either flies or crashes.