Category:PL/0: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added a description.)
 
No edit summary
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{language|PL/0
{{language|PL/0
|tags=pl0}}
|tags=pl0}}
'''PL/0''' is an educational programming language. It was originally introduced in the book, ''Algorithms + Data Structures = Programs'', by [[Niklaus Wirth]] in 1976. Wirth uses PL/0 as an example of how to construct a compiler.
'''PL/0''' is an educational programming language. It was originally introduced in the book, ''Algorithms + Data Structures = Programs'', by [[Niklaus Wirth]] in 1976. Wirth uses PL/0 as an example of how to construct a compiler. This language has simple constructs. Writing real applications in PL/0 is not practical, but the compiler can remain compact and simple.


== Syntax ==
== Features ==
One must explicitly declare all used constants and variables.
The syntax rules of PL/0 can be specified in [[extended Backus–Naur form|EBNF]] as follows:


The only data type is integer. The only operators are arithmetical and comparisons. There is a function <code>odd</code> that examines if its integer argument is odd.
<syntaxhighlight lang="ebnf">
program = block "." ;


In the original implementation presented by Wirth, there are no input and output routines. The interpreter prints the new value of each variable when it changes. So, the program:
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 ")";
</syntaxhighlight>

Wirth in his book presents the version without <code>?</code> ("receive an integer value and assign it to the variable") and <code>!</code> ("display a value of the expression") statements. Instead, this version displays values of the variables after changing them. So, the program:
<syntaxhighlight lang="pascal">
<syntaxhighlight lang="pascal">
var a, b;
var a, b;

begin
begin
a := 0; b := 10;
a := 0; b := 10;
Line 42: Line 20:
end.
end.
</syntaxhighlight>
</syntaxhighlight>
gives the output:
gives this output:
<pre>
<pre>
0
0
Line 57: Line 35:
5
5
</pre>
</pre>
However, most implementations have single input and single output routines.
Some versions use other statements for receiving and displaying data, usually <code>read</code> and <code>write</code>.

The flow control structures are ''if-then'' and ''while-do'' constructs, and procedures are defined by the user. Procedures cannot accept any parameters.

== Syntax ==
The syntax rules of PL/0 can be specified in [[extended Backus–Naur form|EBNF]] as follows:

<syntaxhighlight lang="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 ")";
</syntaxhighlight>

Wirth in his book presents the implementation without <code>?</code> ("receive an integer value and assign it to the variable") and <code>!</code> ("display a value of the expression") routines. Some implementations use other routines for receiving and displaying data, usually <code>read</code> and <code>write</code>.


Due to typograhic conventions, Wirth uses non-ASCII symbols <code>≠</code>, <code>≤</code>, and <code>≥</code>. Some versions use <code>#</code> for "not equal", <code>[</code> for "less or equal", and <code>]</code> for "greater or equal" respectively.
Due to typographic conventions, Wirth uses non-ASCII symbols <code>≠</code>, <code>≤</code>, and <code>≥</code>. Some implementations use <code>#</code> for "not equal", <code>[</code> for "less or equal", and <code>]</code> for "greater or equal".


Some versions accept only upper-case letters or only lower-case letters in keywords and identifiers.
Some implementations accept only uppercase letters or only lowercase letters in keywords and identifiers.


== External links==
== External links==

Latest revision as of 16:51, 3 October 2023

Language
PL/0
This programming language may be used to instruct a computer to perform a task.
Lang tag(s): pl0
See Also:


Listed below are all of the tasks on Rosetta Code which have been solved using PL/0.

PL/0 is an educational programming language. It was originally introduced in the book, Algorithms + Data Structures = Programs, by Niklaus Wirth in 1976. Wirth uses PL/0 as an example of how to construct a compiler. This language has simple constructs. Writing real applications in PL/0 is not practical, but the compiler can remain compact and simple.

Features

One must explicitly declare all used constants and variables.

The only data type is integer. The only operators are arithmetical and comparisons. There is a function odd that examines if its integer argument is odd.

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

var a, b;
begin
  a := 0; b := 10;
  while a < 5 do 
  begin
    a := a + 1;
    b := b - 1;
  end
end.

gives this output:

         0
        10
         1
         9
         2
         8
         3
         7
         4
         6
         5
         5

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

The flow control structures are if-then and while-do constructs, and procedures are defined by the user. Procedures cannot accept any parameters.

Syntax

The syntax rules of PL/0 can be specified in EBNF as follows:

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 ")";

Wirth in his book presents the implementation without ? ("receive an integer value and assign it to the variable") and ! ("display a value of the expression") routines. Some implementations use other routines for receiving and displaying data, usually read and write.

Due to typographic conventions, Wirth uses non-ASCII symbols , , and . Some implementations use # for "not equal", [ for "less or equal", and ] for "greater or equal".

Some implementations accept only uppercase letters or only lowercase letters in keywords and identifiers.

External links