Translation from Z to PASCAL                     Back to Summary

 

Declarations                                

Expressions

Assignment                                                        

While loop

For loop

If statement

Reading                        

Writing                                 

Action

Function                                     

Standard functions

            Algorithm

 

            Implementation of Z machines in PASCAL

 

Variable declaration

 

a variable declaration in C is made by

      Type <Li>;

where  <Li> denotes an identifier list.

 

  LET is translated to VAR.

 

   Simple objects

   

Equivalents  Z --> PASCAL

      Z                      PASCAL

 

      INTEGER       INTEGER

      BOOLEAN     BOOLEAN

 

 

Expressions

 

Z-expression grammar is included in PASCAL grammar.

 

 

Assignment

 

Same syntax

WHILE loop

 

-------Z---------         

 WHILE  Cond :              

                                 

      Statements   

                                 

 ENDWHILE

 

is translated to:

 

-------PASCAL------

 WHILE ( Cond ) DO

   BEGIN

     Statements

   END

 

FOR loop 

 

  --------------Z---------------

  FOR V:= Exp1, Exp2 [, Exp3] :

       Statements

  ENDFOR

 

if Exp3 is absent or equal to 1

 

is translated to:

 

  --------PASCAL---------

  FOR V:= Exp1 TO Exp2 DO

    BEGIN

        Instructions

    END

 

If Exp3 # 1 :

 

  ----------PASCAL------

  V := Exp1;

  WHILE ( V <= Exp2 ) DO

    BEGIN

       Statements ;

      V := V + Exp3

    END;

 

If statement

 

  ----------Z----------        

  IF  Cond :                    

                                  

       Statements

 

  [ELSE                

                        

       Statements]   

  ENDIF                   

 

is translated to:

 

  -------PASCAL---------

 IF Cond

 THEN

   BEGIN

     Statements

   END

 [ELSE

    BEGIN

      Statements

    END]

 

Reading

  

 --------Z--------           

    READ(V1, V2, ...)    

 

is translated to:

 

    ------PASCAL------

    READLN(V1, V2, ...)

 

 

Writing

 

   -----------Z-----------         

   WRITE(Exp1, Exp2, ...)  

 

is translated to:

 

   --------PASCAL----------

   WRITELN(Exp1, Exp2, ...)

 

 

Actions

 

  ------------Z-----------------

  ACTION Name ( P1, P2, ...)

  LET

       Declaration of local objects and parameters

  BEGIN

        Statements

  END

 

is translated to:

 

  --------------------PASCAL--------------------

  PROCEDURE Name ( VAR P1: typ; VAR P2:typ, ...);

  VAR

         Declaration of local objects and parameters

  BEGIN

         Statements

  END

 

Functions

 

  ----------------Z-----------------

  FUNCTION Name ( P1, P2, ...) : Type

  LET

        Declaration of local objects and parameters

  BEGIN

        Statements

  END

 

is translated to:

 

  ---------------------PASCAL------------------------

  FUNCTION Name ( VAR P1: typ; VAR P2:typ, ...) : Type;

  VAR

        Declaration of local objects and parameters

  BEGIN

         Statements

  END

Standard functions

 

  MOD (a, b)

 

FUNCTION Mod (a, b : INTEGER) : INTEGER;

  BEGIN

    Mod := a Mod b

  END;

 

  MIN (a, b)

 

FUNCTION Min (a, b: INTEGER) : INTEGER;

  BEGIN

    Min := a; IF b < a THEN Min := b;

  END;

 

  MAX ( a, b )

 

FUNCTION Max (a, b: INTEGER) : INTEGER;

  BEGIN

    Max := a; IF b > a THEN Max := b;

  END;

 

  EXP ( a, b )

 

FUNCTION Exp (a, b: INTEGER) : INTEGER;

  VAR I : INTEGER;

  BEGIN

    Exp := 1;

    FOR I:= 1 TO b DO Exp := Exp * a

  END;

 

  RANDNUMBER ( N )

 

FUNCTION Randnumer (N: INTEGER) : INTEGER;

  BEGIN

    Randnumer := Random( N );

  END;

 

  RANDSTRING ( N )

 

FUNCTION Randstring(N: INTEGER) : STRING;

  VAR

    K : BYTE;

    Chaine : STRING;

  BEGIN

    Chaine := '';

    FOR K:=1 TO N DO

      CASE Random(2) OF

      0 : Chaine := Chaine + CHR(97+Random(26) ) ;

      1 : Chaine := Chaine + CHR(65+Random(26) )

      END;

    Randstring := Chaine;

  END;

 

  STRINGLENGTH ( C )

 

FUNCTION Stringlength(C : STRING): INTEGER;

  BEGIN

    Stringlength := length (C)

  END;

 

Algorithm

 

  --------------------Z-----------------

  LET

 

      Local and static objects

      Announcement of the modules

 

  BEGIN

 

      Statements

 

  END

 

  Module 1

  Module 2

  ...

  Modules n

 

is translated to:

 

 ------------------PASCAL---------------

 PROGRAM  Pascal;

   VAR

        

    Local and static objects

   { Definition of modules }

   Module 1

   Module 2

   ...

   Module n

 

   BEGIN

         Statements

   END.