Translation from Z to C

 

Declarations                                

Expressions

Assignment                                                         

While loop                           

For loop

If statment

Reading                    

Writing                               

Action

Function                                     

Standard functions

            Algorithm

 

 

            Implementation in C

 

 

 Variable declaration

 

a variable declaration in C is made by

      Type <Li>;

where  <Li> denotes an identifier list.

 

     Simple objects

   

Equivalents  Z --> C

 

INTEGER is translated to int.

CHAR  is translated to char.

To type STRING we associate the built type String defined as follow:

       typedef  char Chaine[256]

 

Ic, Boolean type does not exist.

To continue working with this type, add at the beginning of the program

 

      typdef int Boolean

 

Define also values True et False as follow :

 

      #define true 1

      #define false 0

 

Structure objects

 

To define a structure S in C we must choose an implementation.

Implementing consists in choosing a memory representation ( static or dynamic ) and translate the operations of the abstract machine in this representation.

 

It suffices to replace the structure S by Pointer and add at the level of the header of the program C the desired implementation where the Pointer type will be defined.

Expressions

 

 

Z-expression grammar is included in C grammar.

 

 

Assignment

 

Same syntaxe with  '=' instead of  ':='.

 

 

 While loop 

 

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

  WHILE  Cond :               

        Statements

  ENDWHILE

 

is translated to :

 

  -------------C-----------

  WHILE ( Cond )

    {

      Statements

    }

 

 For loop

 

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

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

       Statements

  ENDFOR

 

is translated to :      

 

  ---------------C---------------

  for (V=Exp1; V<=Exp2; V=V+Exp3)

    {

       Statements

    }

 

 If statement

 

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

  IF Cond :                                                  

       Statements                                             

  [ELSE                                                     

       Statements  ]            

  ENSIF                           

 

is translated to :

 

----------------C--------------

IF ( Cond )

 {

   Statements

 }

[ELSE

 {Statements } ]

 

 

Reading

 

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

  READ(V1, V2, ...)    

 

 

is translated to :

 

  -----------C---------------

  scanf("...", &V1, &V2, ...)

 

Writing

 

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

  WRITE (E1, E2, ...)  

 

is translated to :

  --------C---------

  printf(E1, E2, ...)

 

 

Actions 

 

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

  ACTION Nom ( P1, P2, ...);

  LET

        Declaration of local objects and parameters

  BEGIN

        Statements

  END

 

is translated to :

 

  -----------------C----------------

  void Nom ( typ1 P1 , typ2 P2, ...)

    {

         Declaration of local objects and parameters

         Statements

    }

 

Functions

 

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

  FUNCTION  Nom (  P1, P2, ...) : type;

  LET

        Declaration of local objects and parameters 

  BEGIN

        Statements

  END

 

is translated to :

  ------------------C------------------

  type Nom ( typ1 P1, typ2 P2, ...)

    {        

        Declaration of local objects and parameters

        Statements

    }

 

 

 Standard functions 

 

  MOD (a, b)

     int Mod( int a, int b)
        { return ( a % b ); }

 

  MIN (a, b)

 

     int Min (int a, int b)
       {
          if (a < b) return(a);
         else return(b);
       }
 

  MAX ( a, b )

 

      int Max (int a, int b)
        {
           if (a > b) return(a);
          else return(b);
        } 

 

  EXP ( a, b )

 

      int Exp (int a, int b)
        {
           int i; int Ex ;
           Ex = 1;
           for (i= 1; i<=b; i++)
              Ex = Ex * a ;
           return (Ex);
        } 

 

  RANDNUMBER ( N )

 

      int Randnumber( int N )
        { return ( rand() % N ); }


  RANDSTRING ( N )

 

       char *Randstring ( int N )
         {
           int k;
           char * St = malloc(N);

           char Chr1[26] = "abcdefghijklmnopqrstuvwxyz";
           char Chr2[26] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

           for (k=0;k<N; k++)
              switch ( rand() % 2 ){
                  case 0 : *(St+k) = Chr1[rand() % 26] ; break ;
                  case 1 : *(St+k) = Chr2[rand() % 26] ; break ;
              }
           St[k] = '\0' ;

           return (St);
         }

 

  STRINGLENGTH ( C )

 

     int Stringlength ( string255 Ch )
        {
          return strlen(Ch);
        }

 

 CHARACT(Ch, I)

 

     char *Charact ( string255 Ch , int I )
       {
         char *s = malloc(2);
         s[0] = Ch[I-1];
         s[1] = '\0';
        return s;
       }

 

Algorithm

 

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

  LET

 

      Local and static objects

      Announcement of the modules

 

   BEGIN

 

      Statements

 

    END

 

    Module 1

    Module 2

    ...

    Modules n

 

is translated to :

 

   -------------------C----------------------

    Local and static objects

    // Definition of modules (Functions)

    Module 1

    Module 2

    ...

    Module n

 

    main()

      {

         Statements

      }