Seed7 
 FAQ 
 Manual 
 Screenshots 
 Examples 
 Algorithms 
 Download 
 Links 

 Manual 
 Introduction 
 Tutorial 
 Syntax 
 Statements 
 Types 
 Parameters 
 Objects 
 File System 
 Declarations 
 Tokens 
 Expressions 
 OS access 
 Actions 
 Errors 

 Declarations 
 System 
 Syntax 
 Semantic 

9. DECLARATIONS

Seed7 has three kinds of declarations:

  • System declarations
  • Syntax declarations
  • Semantic declarations

which are described in detail in the following subchapters.

9.1 System declarations

With system declarations the analyzer and the interpreter are informed about which objects should be used for various system internal purposes. An example of a system declaration is

    $ system "integer" is integer;

This defines that the type of all integer literals is 'integer'. Additionally 'integer' is used as type for all integers generated by primitive actions. There are different objects which are defined by a system declaration

  • The types of literals and simple expressions for example: 'string' for strings and 'integer' for integers
  • Which objects should be used as result values for primitive actions for example:
            TRUE, FALSE and empty
  • The EXCEPTIONS which are to be raised by primitive actions for example:
            NUMERIC_ERROR and MEMORY_ERROR
  • Which objects should be used for several implicit actions for example:
            := ::= 'destroy' 'write' and 'flush'

The following system declarations exist

    $ system "type" is type;
    $ system "expr" is expr;
    $ system "integer" is integer;
    $ system "char" is char;
    $ system "string" is string;
    $ system "proc" is proc;
    $ system "float" is float;

    $ system "true" is TRUE;
    $ system "false" is FALSE;
    $ system "empty" is empty;

    $ system "memory_error" is MEMORY_ERROR;
    $ system "numeric_error" is NUMERIC_ERROR;
    $ system "range_error" is RANGE_ERROR;
    $ system "io_error" is IO_ERROR;
    $ system "illegal_action" is ILLEGAL_ACTION;

    $ system "assign" is := ;
    $ system "create" is ::= ;
    $ system "destroy" is destroy;
    $ system "ord" is ord;
    $ system "in" is in;
    $ system "prot_outfile" is PROT_OUTFILE;
    $ system "flush" is flush;
    $ system "write" is write;
    $ system "writeln" is writeln;
    $ system "main" is main;

9.2 Syntax declarations

In many languages exist predefined constructs like statements and operators. This constructs have fixed syntax and semantics defined in a natural language or in some meta language. Further more no programmer is allowed to define new such constructs. But sometimes a full description of the syntax and semantics of a construct written in the programming language itself is helpful. This avoids the need to use ambiguous natural language descriptions and to learn an additional meta language. Also this gives the opportunity to define new constructs. Note that a compiler-compiler does not offer this opportunity and has also a meta language.

When a syntax construct has parameters before the first symbol or after the last symbol the priority and the associativity of the construct are significant. Constructs with stronger priority bind their parameters earlier than constructs with weaker priority. The priority is described by a natural number (inclusive 0). The strongest priority is 0. Weaker priorities are described by larger numbers. What bind means is can be declared with an example:

                                       =
    A + B = C * D                    /   \
                                    /     \
    * priority 4                   +       *
    + priority 5                 /   \   /   \
    = priority 8                A     B C     D

First the * takes its parameters, then the + and at last the = follows.

The associativity describes, in which order constructs with equal priority bind their parameters. For example

    A - B - C

can be interpreted in two ways:

    (A - B) - C    or   A - (B - C)

There are four associativities possible:

                                                   Symbol

    Binding from left to right                       ->

    Binding from right to left                       <-

    Neither the left nor the right parameter
    are allowed to have the same priority            <->

    At the left side there is a binding from
    left to right and at the right side there
    is a binding from right to left                 -><-

The last two possibilities give no legal interpretation in the subtraction example. The third kind of assiciativity ( <-> ) is used by the equal operator ( = ) of Pascal because there a expression like

    A = B = C

is not legal.

There is a second way to describe the associativity. The associativity describes if an operand must have a stronger priority than the priority of the operator. For example:

                             -                     3
    A - B - C              /   \                 /   \
                          /     \           <=3 /     \ <3
    - priority 3 ->      /       \             /       \
                        -         C           3         0
                      /   \                 /   \
                     /     \           <=3 /     \ <3
                    /       \             /       \
                   A         B           0         0

The numbers in the nodes of the right tree show the priority of each sub expression (sub tree). With < and <= the required condition for the priority of an operand is described. An interpretation is legal if all this conditions are met. If there are more than one legal interpretations or no legal interpretation the expression is illegal.

Table for the possibilities of associativity:

    +---------------+------------------------------+
    | associativity |     The priority of the      |
    +---------------+--------------+---------------+
    |               | left operand | right operand |
    |               |    must be   |    must be    |
    +---------------+--------------+---------------+
    |      ->       |      <=      |      <        |
    |      <-       |      <       |      <=       |
    |      <->      |      <       |      <        |
    |     -><-      |      <=      |      <=       |
    +---------------+--------------+---------------+
    |               |  than that of the operator   |
    +---------------+------------------------------+

The parameter before the operator symbol is called left operand. The parameter after the last symbol of a construct is called right operand. In case of normal operators the last symbol of a construct and the operator symbol are identical. If this is not the case there is a third kind of operand. Between the operator symbol and the last symbol of a construct are the middle operands. Middle operands can have any priority.

9.3 Semantic declarations

A semantic declaration declares an object in the database. For example

    const integer: ONE is 1;

declares the 'integer' constant ONE which is initialized with the value 1. Variable declarations are also possible. For example

    var integer: NUMBER is 0;

declares the 'integer' variable NUMBER which is initialized with the value 0. With each declaration the new declared object obtains an initial value. Note that it is not possible to declare an object without an initial value. Declarations with initialisation expressions are also possible. For example

    var string: FILE_NAME is NAME & ".txt";

The expression is evaluated and the result is assigned to the new object using the creation operation ( ::= ). For example the expression

    ONE . ::= . 1

is executed to assign 1 to the object ONE. There are two reasons to use ::= instead of := to assign the initialisation value.

  1. The assignment ( := ) can only be used to assign a value to a variable and initialisation is also needed for constants.
  2. Sometimes some initialisations are needed for the new object in addition to the pure assignment.

For all predefined types the creation operator ( ::= ) is already defined. To allow the declaration of objects of a new user defined type the constructor operation for this type must be defined.