|
The WHILE language is a very simple imperative language
that has very much in common with Pascal.
It contains procedures and a main program, each of which consists of
a sequence of simple statements plus two control structures, namely
an if-then-else conditional (where the else part is optional) and a while loop.
This implementation of the WHILE syntax differs slightly
from the original syntax used in
Principles of Program Analysis
by Nielson/Nielson/Hankin.
What we present here is the preferred syntax for PAG/WWW,
but the original syntax is also supported by the PAG/WWW parser,
so almost every example from the book should work.
There are some limitations though: procedures must have exactly one value parameter
and result parameters are not supported.
This may change in a future implementation of PAG/WWW.
The following table gives the concrete WHILE syntax,
followed by some remarks about the differences to Pascal.
Keywords are printed in bold underlined face,
and square brackets [ ] denote optional parts.
The notation A* means a (possibly empty) list of As,
while A+ denotes a non-empty list of As.
Each line represents an alternative production,
so e.g. a block is either a single statement or a non-empty list of statements
grouped by parentheses (this is more like C than like Pascal).
|
program |
program [identifier] declaration* begin statements end
|
| | |
| declaration |
proc identifier ( identifier ) begin statements end
|
| | |
| block |
statement
( statements )
|
| | |
| statements |
statement+
|
| | |
| statement |
skip ;
identifier := aexpression ;
if bexpression then block [else block]
while bexpression do block
call identifier ( aexpression ) ;
|
| | |
| aexpression |
identifier
constant
aexpression + aexpression
aexpression - aexpression
aexpression * aexpression
aexpression / aexpression
- aexpression
( aexpression )
|
| | |
| bexpression |
true
false
aexpression < aexpression
aexpression <= aexpression
aexpression > aexpression
aexpression >= aexpression
aexpression = aexpression
aexpression <> aexpression
not bexpression
( bexpression )
|
Values and types
WHILE only knows variables and constants of type integer, except for the boolean constants true and false, so there is no type information for variables. All aexpressions are of type integer and all bexpressions are of type boolean.
Constants may be any integer that can be represented with 32bit. Identifiers may contain digits and underscores but must start with a letter.
Variables
Almost all variables in the WHILE language are global variables, meaning that they can be accessed from any procedure and the main program. There are no variable declarations, every variable you use is automatically defined and visible in the whole program (pretty much like the good old basic).
The only exception to this are parameters of procedures. Since every procedure has exactly one parameter, every procedure has exactly one local variable.
A local variable shadows global variables, that means if a procedure has a parameter with name x you cannot access a global variable x in this procedure.
Procedures
In contrast to Pascal, procedures may not be nested, that means all procedures must be defined one after another before the main program. Procedures must have exactly one parameter and do not have a return value. To return a value from a procedure you can use global variables.
Procedures can be mutually recursive, so the order in which you define procedures is irrelevant. Every procedure is visible from any other procedure no matter which one is defined first.
Remember though, that the resulting control flow graphs for recursive procedure calls may be quite ugly and hard to understand because of the crossing call and return edges.
Search
|