Z Language  (Basic)                    Back to Summary

 

 

Overview

Structure of a Z algorithm

Control structures

Simple actions

Scalars

Pointers

Expressions

Comments

High level operations

Standard functions

Fonctions de génération aléatoire

Functions on strings

Definition of an action

Definition of a function

Example of a Z algorithm

 

 Overview

 

> A Z algorithm is a set of modules. The first one is the main module and the others are either actions (ACTION) or functions (FUNCTION).

> The Z language accepts recursion.

> Static objects are declared in the main module.

> The communication between modules is made via parameters and static variables.

> The language allows:

- Any type of parameters : scalars, structures, linked lists , queues, stacks, arrays, trees and also complex types.

- The dynamic allocation of arrays and structures

- The global assignment of any type

> Four standard types ( scalars) are allowed  : INTEGER, BOOLEAN, CHAR, STRING .

> Some usual functions exist :  MOD, MAX, MIN, RANDNUMBER, RANDSTRING,...

> The langage is the set of abstract algorithms written by using abstract machines.

> So, we considere abstract machines on structures, arrays of any dimension, queues, stacks, binary and m-ary search trees, linked lists and bidrectional linlked lists.

> We also consider an abstract machine on the files allowing their use and the construction of simple structures of files as well as the most complex structures.

> The language allows compound types such as STACK OF QUEUE OF LISTS OF ....of which the last one quoted is of scalar type or simple structure.

> The language has high-level operations to build lists, trees, queues, etc. from a set of values ( expressions ) or structures.

> the language offers two very useful functions to randomly generate strings (ALEACHAINE) and integers (ALEANUMBER).

> The language allows reading and  writing scalars, n-dimensional arrays of scalars and simple or complex structures.

 

Structure of a Z algorithm

 
    LET
        { Local and statitc objects }
        { announcement of the modules}
    BEGIN
        { Statements }
    END
    Module 1
    ....
    Module n

Each module can be either a function or an action.

 

 Control structures

 

While loop

 

    WHILE E [ : ]
        { Statements }
    ENDWHILE

For loop

 

    FOR V := E1, E2 [,E3]
        { Statements }
    ENDFOR                
  

E3, if present, designates the step.

 

Conditional 

    IF E [ : ]
        { Statements }
    [ ELSE
        { Statements } ]
    ENDSI
 

 Simple actions 

 

V denotes a variable, E an expression and Idf a module  identifier.

 

Assignment  : V := E

Reading : READ(V1, V2, .....)

Variables can be scalars, structures or arrays of any dimension.

Writing : WRITE(E1, E2, .....)

Expressions can be scalars, structures or n-dimensional arrays.

 

 

Scalars

 

Quatre types standards sont autorisés : INTEGER, CHAR, STRING, BOOLEAN.

 

Definition of scalars

 

  LET <li> Sep Type

 

<Li> : identifier list separated by coma.

Type is a standard type.

 

 

Examples

 

    A, B, Found : BOOLEANS ;

    A : INTEGER ;

    X, Y, Z : STRINGS ;

 

Pointers

 

We define pointer variables to use data structures.

 

  LET <li> :  POINTER TO Typec [ OF Typec OF Typec OF  ....] [ OF Types]

 

<Li> : Identifier list separated by coma.

Typec in  { ARRAY, STACK, list, QUEUE BST, BIlist, MST }

Types is a scalar or simple structure.

 

Note 

We can do not use the pointer type. Indeed, declarations

"LET L : list" and

"LET L : pointer TO list"

are equivalent.

 

Examples

 

  P1 : POINTER TO LIST OF STACKS;

  P2, P2 : POINTERS TO BST;

 

 Z Expressions 

 

As in the programming languages.

 

Arithmetical expressions  : + , - , / , *

Logical expressions  : AND, OR, NOT

Expressions on strings : +

Relational expressions : <, <=, >, >=, =, <> (ou #)

 

Logical constants : TRUE, FALSE

Pointer constant : NULL

 

Examples

 

  B+C / F

  NOT Found

  (X # 5) AND NOT Found

  F(X) <> 5

  P = Null

Comments

 

Comments are inserted into any place where we can insert a space.

Comments are beteween { and } or beteween   /*  and  */.

 

 

High level operations 

 

The Z language has high-level operations for filling or initializing a data structure from a set of values.

CREATE_ARB, CREATE_LISTE, CREATE_LISTEBI, CREATE_ARM, CREATE_PILE, CREATE_FILE, INIT_VECT (or INIT_ARRAY), INIT_STRUCT

Syntax :

CREATE_LIST ( L, [Exp1, Exp2, ....] )

CREATE_BILIST ( LB, [Exp1, Exp2, ....] )

CREATE_BST ( A, [Exp1, Exp2, ....] )

CREATE_MST ( M, [Exp1, Exp2, ....] )

CREATE_QUEUE ( F, [Exp1, Exp2, ....] )

CREATE_STACK ( P, [Exp1, Exp2, ....] )

INIT_STRUCT(S, [Exp1, Exp2, ....])

INIT_ARRAY ( T, [Exp1, Exp2, ....] )

Exp1, Exp2, .... are scalar expressions or structures.

Examples

CREATE-LIST (L, [12, 23, 67, I, I+J] )

creates the linked list L with values  with the values in square brackets in the order shown.

If L1 and L2 are 2 integer linked list and Ll a linked list of a linked list of integers, we can then write :

CREATE_LIST ( L1 , [ 2 , 4 , 67 , 778 ] ) ;

CREATE_LIST ( L2 , [ 12 , 14 , 167 , 1778 ] ) ;

CREATE_LIST ( Ll , [ L1 , L2 ] ) ;

 

 

 Standard functions 

     

MOD (A, B)  : Give the remainder of the integer division of A by B.

MAX(A, B)  :  Give the maximum between A and B.

MIN(A, B) :    Give the minimum between A and B.

EXP(A, B) :     A exposant B

 

 Fonctions de génération aléatoire

 

RANDSTRING(N)  : Generate an alphanumeric string of N characters.

RANDNUMBER (N) : Generate an integer between 0 and N.

Functions on strings

 

CHARACT(S, I) : give the i-th character of the string S.

 

LENTHSTRING(S) : give the length of string S

 

Definition of an action  

    ACTION Name (P1, P2, ..., Pn)
            { Local objects and parameters }
   
BEGIN
        { Statements }
   
END

Calling an action is made by the operation CALL followed by the name of the action and its parameters.

Parameters are not protected by the action.

 

Definition of a function

 

   FUNCTION Name (P1, P2, ...,Pn) : Type
            { Local objects and parameters }
   
BEGIN
        { Statements }
   
END

Type can be any.

Functions are used in expressions.

Parameters are not protected by the function.

 

Example of a Z algorithm

 

LET
L1, L2 : LISTS;
Rech, Tous : FUNCTION(BOOLEAN);
BEGIN
CREATE_LIST(L1, [2, 5, 9, 8, 3, 6 ]);
CREATE_LIST(L2, [12, 5, 19, 8, 3, 6, 2,9]);
WRITE( Tous(L1, L2) )
END

FUNCTION Rech ( L, Val ) : BOOLEAN
LET
L : LIST; Val : INTEGER;
BEGIN
IF L = NULL : Rech := FALSE
ELSE
IF CELL_VALUE(L) = Val :
Rech := TRUE
ELSE
Rech := Rech(NEXT(L), Val )
ENDIF
ENDIF
END
FUNCTION Tous ( L1, L2 ) : BOOLEAN
LET
L1, L2 : LISTS;
BEGIN
IF L1 = NULL : Tous := TRUE
ELSE
IF NOT Rech(L2, CELL_VALUE(L1) )
Tous := FALSE
ELSE
Tous := Tous(NEXT(L1), L2)
ENDIF
ENDIF
END