Imperative -- two types of imperative constructs:
Compiled -- no separate compilation.
Block-structured -- constructs may be nested.
E.g.,
|
||||||||||||||||||||||||||
Declarations are explicit.
Supports recursion.
Provides dynamic arrays.
Run-time binding.
Stack is main run-time data structure.
Lexics -- free format.
for i := 1 step 1 until N do
sum := sum + Data[i];
average := sum / N
Algol is defined on three levels with common syntax, but different lexics:
Algol uses keywords.
In general, special words can be denoted in three ways:
Reserved words -- Special words cannot be used as identifiers by the programmer.
E.g., Pascal words if and then.
Keywords -- Special words are distinguished by marking them in an unambiguous way, e.g.,
preceding each keyword with #: #IF
surrounding each keyword with quotes: 'if'
typing keywords in upper case: IF i < 5
printing keywords in a different font: if i < 5
| - | difficult to type |
| - | not easily readable (except for different font) |
Keywords in context -- Compiler finds special words according to the context
in which they are expected (as in FORTRAN and PL/I).
E.g., PL/I:
IF IF THEN
THEN = 0;
ELSE
ELSE = 0;
Most modern languages use reserved words.
Note that the use of the word "keyword" as defined above is specific to textbook; many language references use "keyword" as synonymous with "reserved word".
Algol follows the Zero-One-Infinity Principle:
The latter example illustrates Algol's conditional expression (like that in C).
Dangling else problem -- There are two possible meanings for the statement
if <cond1> then
if <cond2> then
<stmt1>
else
<stmt2>
if <cond1> then
if <cond2> then
<stmt1>
else
<stmt2>
Algol's simplistic solution: A conditional statement (if) is not allowed as the statement in a conditional statement. Instead, the intended structure must be explicitly shown using a block (begin-end pair):
if <cond1> then
begin
if <cond2> then
<stmt1>
else
<stmt2>
end
if <cond1> then
begin
if <cond2> then
<stmt1>
end
else
<stmt2>
Other languages solve the problem by:
Adopting a convention, usually that an else goes with the nearest preceding unmatched if (e.g., PL/I, C).
Using fully bracketed structures (e.g., Modula-2, Ada).
IF <cond1> THEN
IF <cond2> THEN
<stmt1>
ELSE
<stmt2>
END
END
IF <cond1> THEN
IF <cond2> THEN
<stmt1>
END
ELSE
<stmt2>
END
Perhaps the ultimate example of fully bracketed structures is Algol 68 which uses the backward spelling of a structure's starting "keyword" to close the structure: if ... fi, case ... esac. The Unix Bourne shell follows Algol 68's bracketing principle, but uses do ... done for for, while and until loops instead of Algol 68's do ... od.