Translation from Z to C
Back
to Summary
Implementation of Z machines in C
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.
Z-expression grammar is included in C grammar.
Same syntaxe with '=' instead of ':='.
-----------Z----------
WHILE Cond :
Statements
ENDWHILE
is translated to :
-------------C-----------
WHILE ( Cond )
{
Statements
}
--------------Z-----------------
FOR V:= Exp1, Exp2 [, Exp3] [:]
Statements
ENDFOR
is translated to :
---------------C---------------
for (V=Exp1; V<=Exp2; V=V+Exp3)
{
Statements
}
-----------Z------------
IF Cond :
Statements
[ELSE
Statements ]
ENSIF
is translated to :
----------------C--------------
IF ( Cond )
{
Statements
}
[ELSE
{Statements } ]
--------Z--------
READ(V1, V2, ...)
is translated to :
-----------C---------------
scanf("...", &V1, &V2, ...)
--------Z----------
WRITE (E1, E2, ...)
is translated to :
--------C---------
printf(E1, E2, ...)
------------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
}
------------------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
}
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;
}
--------------------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
}