PL/0

Last updated

PL/0 is a programming language, intended as an educational programming language, that is similar to but much simpler than Pascal, a general-purpose programming language. It serves as an example of how to construct a compiler. It was originally introduced in the book, Algorithms + Data Structures = Programs , by Niklaus Wirth in 1976. It features quite limited language constructs: there are no real numbers, very few basic arithmetic operations and no control-flow constructs other than "if" and "while" blocks. While these limitations make writing real applications in this language impractical, it helps the compiler remain compact and simple.

Contents

Features

All constants and variables used must be declared explicitly.

The only data types are integers. The only operators are arithmetic and comparison operators. There is an odd function that tests whether the argument is odd.

In the original implementation presented by Wirth, there are no input and output routines. The new value of each variable is printed compiler prints as it changes. So the program:

vari,s;begini:=0;s:=0;whilei<5dobegini:=i+1;s:=s+i*iendend.

gives the output:

          0           0           1           1           2           5           3          14           4          30           5          55 

However, most implementations have single input and single output routines.

Flow control structures are if-then and while-do constructs and user-defined procedures. Procedures cannot accept parameters.

Grammar

The following is the syntax rules of the model language defined in EBNF:

program =block ".";block =["const"ident "="number {","ident "="number}";"]["var"ident {","ident}";"]{"procedure"ident ";"block ";"}statement ;statement =[ident ":="expression |"call"ident |"?"ident |"!"expression |"begin"statement {";"statement }"end"|"if"condition "then"statement |"while"condition "do"statement ];condition ="odd"expression |expression ("="|"#"|"<"|"<="|">"|">=")expression ;expression =["+"|"-"]term {("+"|"-")term};term =factor {("*"|"/")factor};factor =ident |number |"("expression ")";

It is rather easy for students to write a recursive descent parser for such a simple syntax. Therefore, the PL/0 compiler is still widely used in courses on compiler construction throughout the world. Due to the lack of features in the original specification, students usually spend most of their time with extending the language and their compiler. They usually start with introducing REPEAT .. UNTIL and continue with more advanced features like parameter passing to procedures or data structures like arrays, strings or floating point numbers.

Use in education

The main article on compilers honours PL/0[ citation needed ] for introducing several influential concepts (stepwise refinement, recursive descent parsing, EBNF, P-code, T-diagrams) to the field by educating students to use these concepts. Over the last 3 decades, most university courses on compiler construction that used PL/0 have followed Wirth strictly in employing these techniques (see references below). Some years ago university courses deviated from the course set by Wirth with the replacement of the classical recursive descent parsing technique by a (nonetheless classical) Unix-like approach of employing lex and yacc. Only recently an implementation (PL/0 Language Tools) along this way has also combined modern concepts like object-orientation and design patterns with a modern scripting language (Python), allowing students to consume the source text of the implementation in a contemporary programming style.

Compiler construction

In December 1976, Wirth wrote a small booklet about compiler construction, containing the full source code of the PL/0 compiler. The syntax rules above were taken from this first edition of Wirth's book Compilerbau. [1] In later editions of this book (under the influence of his ongoing research) Wirth changed the syntax of PL/0. He changed the spelling of keywords like const and procedure to uppercase. This change made PL/0 resemble Modula-2 more closely. At the same time, Wirth's friend and collaborator C. A. R. Hoare was working on his influential communicating sequential processes concept, which used the exclamation mark ! and the question mark ? to denote communication primitives. Wirth added both symbols to the PL/0 language, but he did not mention their semantics in the book.

Examples

The following example is taken from such an extended language called PL/0E. [2] This program outputs the squares of numbers from 1 to 10. Most courses in compiler construction today have replaced the exclamation mark with the WriteLn procedure.

VARx,squ;PROCEDUREsquare;BEGINsqu:=x*xEND;BEGINx:=1;WHILEx<=10DOBEGINCALLsquare;!squ;x:=x+1ENDEND.

This following program prints the prime numbers from 1 to 100. The write statement corresponds to '!' statement in the EBNF syntax above.

constmax=100;vararg,ret;procedureisprime;vari;beginret:=1;i:=2;whilei<argdobeginifarg/i*i=argthenbeginret:=0;i:=argend;i:=i+1endend;procedureprimes;beginarg:=2;whilearg<maxdobegincallisprime;ifret=1thenwritearg;arg:=arg+1endend;callprimes.

The following example was taken from the second edition of Wirth's book Compilerbau, [1] which appeared in 1986 in Germany.

VARx,y,z,q,r,n,f;PROCEDUREmultiply;VARa,b;BEGINa:=x;b:=y;z:=0;WHILEb>0DOBEGINIFODDbTHENz:=z+a;a:=2*a;b:=b/2ENDEND;PROCEDUREdivide;VARw;BEGINr:=x;q:=0;w:=y;WHILEw<=rDOw:=2*w;WHILEw>yDOBEGINq:=2*q;w:=w/2;IFw<=rTHENBEGINr:=r-w;q:=q+1ENDENDEND;PROCEDUREgcd;VARf,g;BEGINf:=x;g:=y;WHILEf#gDOBEGINIFf<gTHENg:=g-f;IFg<fTHENf:=f-gEND;z:=fEND;PROCEDUREfact;BEGINIFn>1THENBEGINf:=n*f;n:=n-1;CALLfactENDEND;BEGIN?x;?y;CALLmultiply;!z;?x;?y;CALLdivide;!q;!r;?x;?y;CALLgcd;!z;?n;f:=1;CALLfact;!fEND.

Oberon-0

In the third and last edition of his book on compiler construction, Wirth replaced PL/0 with Oberon-0. The language Oberon-0 is much more complex than PL/0. For example, Oberon-0 offers arrays, records, type declarations and procedure parameters. The publisher of Wirth's books (Addison-Wesley) has decided to phase out all his books, but Wirth has published revised editions of his book beginning in 2004. [3] As of August 2017, the most recent revision available is from May 2017. [4] [5]

See also

Notes

  1. 1 2 Wirth, 1986
  2. PL/0E Archived 2012-02-21 at the Wayback Machine
  3. The revised third edition Archived 2017-02-17 at the Wayback Machine (2005) of Compiler Construction, Niklaus Wirth, 1996, ISBN   0-201-40353-6 has never seen the printing press, but it is available online.
  4. Wirth, Niklaus (2017-05-28). "Compiler Construction" . Retrieved 2017-08-25.
  5. Wirth, Niklaus (2017-08-18). "news.txt". Archived from the original on 2017-08-25. Retrieved 2017-08-25.

Related Research Articles

<span class="mw-page-title-main">Oberon (programming language)</span>

Oberon is a general-purpose programming language first published in 1987 by Niklaus Wirth and the latest member of the Wirthian family of ALGOL-like languages. Oberon was the result of a concentrated effort to increase the power of Modula-2, the direct successor of Pascal, and simultaneously to reduce its complexity. Its principal new feature is the concept of type extension of record types. It permits constructing new data types on the basis of existing ones and to relate them, deviating from the dogma of strictly static typing of data. Type extension is Wirth's way of inheritance reflecting the viewpoint of the parent site. Oberon was developed as part of the implementation of an operating system, also named Oberon at ETH Zurich in Switzerland. The name is from the moon of the planet Uranus, named Oberon.

<span class="mw-page-title-main">Pascal (programming language)</span> Programming language

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.

In computer programming, a p-code machine is a virtual machine designed to execute p-code. This term is applied both generically to all such machines, and to specific implementations, the most famous being the p-Machine of the Pascal-P system, particularly the UCSD Pascal implementation, among whose developers, the p in p-code was construed to mean pseudo more often than portable, thus pseudo-code meaning instructions for a pseudo-machine.

In computer science, a recursive descent parser is a kind of top-down parser built from a set of mutually recursive procedures where each such procedure implements one of the nonterminals of the grammar. Thus the structure of the resulting program closely mirrors that of the grammar it recognizes.

In computer science, extended Backus–Naur form (EBNF) is a family of metasyntax notations, any of which can be used to express a context-free grammar. EBNF is used to make a formal description of a formal language such as a computer programming language. They are extensions of the basic Backus–Naur form (BNF) metasyntax notation.

ALGOL W is a programming language. It is based on a proposal for ALGOL X by Niklaus Wirth and Tony Hoare as a successor to ALGOL 60. ALGOL W is a relatively simple upgrade of the original ALGOL 60, adding string, bitstring, complex number and reference to record data types and call-by-result passing of parameters, introducing the while statement, replacing switch with the case statement, and generally tightening up the language.

In computer programming, an assignment statement sets and/or re-sets the value stored in the storage location(s) denoted by a variable name; in other words, it copies a value into the variable. In most imperative programming languages, the assignment statement is a fundamental construct.

Component Pascal is a programming language in the tradition of Niklaus Wirth's Pascal, Modula-2, Oberon and Oberon-2. It bears the name of the language Pascal and preserves its heritage, but is incompatible with Pascal. Instead, it is a minor variant and refinement of Oberon-2 with a more expressive type system and built-in string support. Component Pascal was originally named Oberon/L, and was designed and supported by a small ETH Zürich spin-off company named Oberon microsystems. They developed an integrated development environment (IDE) named BlackBox Component Builder. Since 2014, development and support has been taken over by a small group of volunteers. The first version of the IDE was released in 1994, as Oberon/F. At the time, it presented a novel approach to graphical user interface (GUI) construction based on editable forms, where fields and command buttons are linked to exported variables and executable procedures. This approach bears some similarity to the code-behind way used in Microsoft's .NET 3.0 to access code in Extensible Application Markup Language (XAML), which was released in 2008.

<span class="mw-page-title-main">Oberon-2</span>

Oberon-2 is an extension of the original Oberon programming language that adds limited reflection and object-oriented programming facilities, open arrays as pointer base types, read-only field export, and reintroduces the FOR loop from Modula-2.

A system programming language is a programming language used for system programming; such languages are designed for writing system software, which usually requires different development approaches when compared with application software. Edsger Dijkstra refers to these languages as machine oriented high order languages, or mohol.

Object Pascal is an extension to the programming language Pascal that provides object-oriented programming (OOP) features such as classes and methods.

<span class="mw-page-title-main">ALGOL 68</span> Programming language

ALGOL 68 is an imperative programming language that was conceived as a successor to the ALGOL 60 programming language, designed with the goal of a much wider scope of application and more rigorously defined syntax and semantics.

Harbour is a computer programming language, primarily used to create database/business programs. It is a modernized, open sourced and cross-platform version of the older Clipper system, which in turn developed from the dBase database market of the 1980s and 1990s.

The computer programming languages C and Pascal have similar times of origin, influences, and purposes. Both were used to design their own compilers early in their lifetimes. The original Pascal definition appeared in 1969 and a first compiler in 1970. The first version of C appeared in 1972.

IP Pascal is an implementation of the Pascal programming language using the IP portability platform, a multiple machine, operating system and language implementation system. It implements the language "Pascaline", and has passed the Pascal Validation Suite.

In computer programming, an anonymous function is a function definition that is not bound to an identifier. Anonymous functions are often arguments being passed to higher-order functions or used for constructing the result of a higher-order function that needs to return a function. If the function is only used once, or a limited number of times, an anonymous function may be syntactically lighter than using a named function. Anonymous functions are ubiquitous in functional programming languages and other languages with first-class functions, where they fulfil the same role for the function type as literals do for other data types.

Syntax diagrams are a way to represent a context-free grammar. They represent a graphical alternative to Backus–Naur form, EBNF, Augmented Backus–Naur form, and other text-based grammars as metalanguages. Early books using syntax diagrams include the "Pascal User Manual" written by Niklaus Wirth and the Burroughs CANDE Manual. In the compilation field, textual representations like BNF or its variants are usually preferred. BNF is text-based, and used by compiler writers and parser generators. Railroad diagrams are visual, and may be more readily understood by laypeople, sometimes incorporated into graphic design. The canonical source defining the JSON data interchange format provides yet another example of a popular modern usage of these diagrams.

PL360 is a system programming language designed by Niklaus Wirth and written by Wirth, Joseph W. Wells Jr., and Edwin Satterthwaite Jr. for the IBM System/360 computer at Stanford University. A description of PL360 was published in early 1968, although the implementation was probably completed before Wirth left Stanford in 1967.

SuperPascal is an imperative, concurrent computing programming language developed by Per Brinch Hansen. It was designed as a publication language: a thinking tool to enable the clear and concise expression of concepts in parallel programming. This is in contrast with implementation languages which are often complicated with machine details and historical conventions. It was created to address the need at the time for a parallel publication language. Arguably, few languages today are expressive and concise enough to be used as thinking tools.

<span class="mw-page-title-main">Rexx</span> Command/scripting/programming language

Rexx is a programming language that can be interpreted or compiled. It was developed at IBM by Mike Cowlishaw. It is a structured, high-level programming language designed for ease of learning and reading. Proprietary and open source Rexx interpreters exist for a wide range of computing platforms; compilers exist for IBM mainframe computers.

References