Converting Pascal to C 
Author Message
 Converting Pascal to C

I have an application written in Pascal using IPC message queues.
I would like to convert this one into C language.
Where can I find information about :
- guidelines to plan this application migration
- main differences between C and Pascal (general) such as variables
visibility differences, arguments ...

Thanks

PS: This post is written in English because I used multipost but French
langage is welcome !!

Sent via Deja.com
http://www.*-*-*.com/



Sun, 29 Jun 2003 16:52:44 GMT  
 Converting Pascal to C
There may be Pascal-to-C tools out there.

If not, you might consider using the DMS Reengineering Toolkit,
which is generalized compiler technology used to
automate custom modifications to large software sources.
DMS can parse and prettyprint many languages,
including C and Pascal, and can carry out user-defined
transformations.
See http://www.semdesigns.com/Products/DMS/DMSToolkit.html.

--

Semantic Designs, Inc.              web: http://www.semdesigns.com
12636 Research Blvd. C-214    voice: (512) 250-1018 x140
Austin, TX 78759-2200             fax: (512) 250-1191


Quote:
> I have an application written in Pascal using IPC message queues.
> I would like to convert this one into C language.
> Where can I find information about :
> - guidelines to plan this application migration
> - main differences between C and Pascal (general) such as variables
> visibility differences, arguments ...

> Thanks

> PS: This post is written in English because I used multipost but French
> langage is welcome !!

> Sent via Deja.com
> http://www.deja.com/



Sun, 29 Jun 2003 23:06:26 GMT  
 Converting Pascal to C

Quote:

> I have an application written in Pascal using IPC message queues.
> I would like to convert this one into C language.
> Where can I find information about :
> - guidelines to plan this application migration
> - main differences between C and Pascal (general) such as variables
> visibility differences, arguments ...

Try almost any elementary book on Pascal.  I used to recommend
Grogonos "Programming in Pascal" for beginners.  Don't know if it
is still in print, but he is at Concordia in Montreal.

However, neither ISO Pascal nor C know anything about IPC message
queues.

ISO Pascal is a very straightforward language, with virtually no
gotchas.  So if your original is well written, the translation
should be very easy and significanly less buggy than if had had
been first written in C.  If it is not well written, or if it
depends heavily on non-standard things, it might be easier to
start from scratch.

Two areas may give you trouble.  First, Pascal has nested
procedures and functions, i.e. a function may be local to an
encompassing procedure.  The local function inherits access to all
the outer functions variables.  This is very useful in hiding and
localizing operations.  To get the equivalent in C you will have
to isolate the outer function to a file, declare all the local
functions as static, and you won't really be able to
satisfactorily simulate the local variable access.  Globals won't
do because of re-entrancy, and C locals in the outer aren't
visible in the now non-local local procedures.  So the parameter
lists and overhead have to expand.

Second, Pascal file i/o depends on a strict model, and the
primitives are get, put, and f^ (apart from reset and rewrite).
read and write are built on these.  readln and writeln are
primitives for text files.  Versions of read, write, readln,
writeln with excess parameters are shorthand for sequences of
read, write, readln, writeln etc, i.e. overloaded.  You can build
equivalents of the primitives in C, but beware mixing up semantics
by using more than one system.

Some alleged Pascal systems don't supply the necessary primitives,
and foul the semantics of the derived functions. Turbo/Borland
comes to mind, so beware.

Lesser things:

Strings in Pascal are fixed length arrays of char.  Some systems
create new non-standard basic types to handle strings of varying
length.  String handling is a basic Pascal weakness.  Another is
generic arrays, unless conformant arrays are implemented, which
also covers most of the string nuisances.

C has no direct equivalent of a Pascal RECORD.  You have to build
the result up with a set of typedefs, structures, and unions.  The
C nomenclature for field access can get very involved, compared to
Pascal.  Understand the Pascal WITH statement in this area.

NOTE THAT I AM COMPARING Standard C and Standard Pascal.  Not
particular implementations of either.

--

http://www.qwikpages.com/backstreets/cbfalconer
   (Remove "NOSPAM." from reply address. my-deja works unmodified)



Mon, 30 Jun 2003 01:33:00 GMT  
 Converting Pascal to C

Quote:

>Two areas may give you trouble.  First, Pascal has nested
>procedures and functions, i.e. a function may be local to an
>encompassing procedure.  The local function inherits access to all
>the outer functions variables.  This is very useful in hiding and
>localizing operations.  To get the equivalent in C you will have
>to isolate the outer function to a file, declare all the local
>functions as static, and you won't really be able to
>satisfactorily simulate the local variable access.  Globals won't
>do because of re-entrancy, and C locals in the outer aren't
>visible in the now non-local local procedures.  So the parameter
>lists and overhead have to expand.

You do not have to do it this way at all, and in fact, you probably
should not do it exactly this way.

Here is a sample nested procedure and a C-ized version of the same:

        function sillyexample(x : integer) : integer;
        var i, sum : integer;
                procedure incr;
                begin sum := sum + i end;
        begin
                sum := 0;
                for i := 1 to x do
                        incr(sum, i);
                sillyexample := sum
        end;

Here is a direct transliteration into C:

        struct context {
                int i, sum;
        };
        static void incr(struct context *c) {
                c->sum += c->i;
        }
        int sillyexample(int x) {
                struct context ctx;

                ctx.sum = 0;
                for (ctx.i = 1; ctx.i <= x; ctx.i++)
                        incr(&ctx);
                return ctx.sum;
        }

In other words, you simply "package up" all the non-local variables
in a "struct context", and pass a pointer to the caller's context.
(This is, with about 95% certainty, how your Pascal compiler did
it in the first place, so the new code will not have any extra
overhead.)

In my experience, in many cases, Pascal code that uses nested
procedures and functions can be rewritten using simple C macros or
existing C library functions, avoiding the need for anything like
this anyway.
--
In-Real-Life: Chris Torek, Berkeley Software Design Inc




Mon, 30 Jun 2003 01:49:54 GMT  
 Converting Pascal to C

Quote:
>Here is a sample nested procedure and a C-ized version of the same:

>    function sillyexample(x : integer) : integer;
>    var i, sum : integer;
>            procedure incr;
>            begin sum := sum + i end;
>    begin
>            sum := 0;
>            for i := 1 to x do
>                    incr(sum, i);

Oops, this should just be "incr;".

(I accidentally started out writing this "properly", and forgot to
remove the parameters to build the silly example. :-) )

Quote:
>            sillyexample := sum
>    end;

--
In-Real-Life: Chris Torek, Berkeley Software Design Inc




Mon, 30 Jun 2003 01:51:42 GMT  
 Converting Pascal to C


Quote:

> In other words, you simply "package up" all the non-local variables
> in a "struct context", and pass a pointer to the caller's context.

This is certainly the best way of dealing with this situation.  But
note that pointers to the parameters of the outer function may also
need to be passed, or pointers to them must be copied into the context,
since a Pascal nested function would have access to the formal
parameters of the function it is nested in.

Quote:
> (This is, with about 95% certainty, how your Pascal compiler did
> it in the first place, so the new code will not have any extra
> overhead.)

Really?  I would have expected more than 5% to use a display
mechanism (which is equivalent to putting the pointer into a
global variable, while caching some of those pointers along the
way), although perhaps that is only true if the target machine
makes such a mechanism particularly attractive, which may be
5% or less.

It's also not clear what you intend to have happen with several
levels of nesting--do you package up the outer function's context
pointer in the inner function's context (when calling a function
nested in the inner function which is nested in the outer function),
or do you pass a pointer to each?  Most Pascal compilers where I
know what they do, do the former, creating a linked list of contexts
that has to be traversed if functions are deeply nested, but the
latter would result in less overhead (unless it pushes the number
of arguments past some breakpoint).

--
MJSR

Sent via Deja.com
http://www.deja.com/



Mon, 30 Jun 2003 07:23:58 GMT  
 Converting Pascal to C

Quote:


>> (This is, with about 95% certainty, how your Pascal compiler did
>> it in the first place, so the new code will not have any extra
>> overhead.)

>Really?  I would have expected more than 5% to use a display
>mechanism (which is equivalent to putting the pointer into a
>global variable, while caching some of those pointers along the
>way), although perhaps that is only true if the target machine
>makes such a mechanism particularly attractive, which may be
>5% or less.

Displays are almost always a poor second to static links, almost
regardless of target machine.

Quote:
>It's also not clear what you intend to have happen with several
>levels of nesting--do you package up the outer function's context
>pointer in the inner function's context (when calling a function
>nested in the inner function which is nested in the outer function),
>or do you pass a pointer to each?  Most Pascal compilers where I
>know what they do, do the former, creating a linked list of contexts
>that has to be traversed if functions are deeply nested ...

This is the same as the static links. :-)

Quote:
>... but the latter would result in less overhead (unless it pushes
>the number of arguments past some breakpoint).

The latter also requires fancy compiler analysis.  The amount of
nesting tends to be small, so this is a lot of work for little gain.
--
In-Real-Life: Chris Torek, Berkeley Software Design Inc




Mon, 30 Jun 2003 08:03:14 GMT  
 Converting Pascal to C

Quote:



> >Two areas may give you trouble.  First, Pascal has nested
> >procedures and functions, i.e. a function may be local to an
> >encompassing procedure.  The local function inherits access to all
> >the outer functions variables.  This is very useful in hiding and
> >localizing operations.  To get the equivalent in C you will have
> >to isolate the outer function to a file, declare all the local
> >functions as static, and you won't really be able to
> >satisfactorily simulate the local variable access.  Globals won't
> >do because of re-entrancy, and C locals in the outer aren't
> >visible in the now non-local local procedures.  So the parameter
> >lists and overhead have to expand.

> You do not have to do it this way at all, and in fact, you probably
> should not do it exactly this way.

> Here is a sample nested procedure and a C-ized version of the same:

>         function sillyexample(x : integer) : integer;
>         var i, sum : integer;
>                 procedure incr;
>                 begin sum := sum + i end;
>         begin
>                 sum := 0;
>                 for i := 1 to x do
>                         incr(sum, i);
>                 sillyexample := sum
>         end;

> Here is a direct transliteration into C:

>         struct context {
>                 int i, sum;
>         };
>         static void incr(struct context *c) {
>                 c->sum += c->i;
>         }
>         int sillyexample(int x) {
>                 struct context ctx;

>                 ctx.sum = 0;
>                 for (ctx.i = 1; ctx.i <= x; ctx.i++)
>                         incr(&ctx);
>                 return ctx.sum;
>         }

> In other words, you simply "package up" all the non-local variables
> in a "struct context", and pass a pointer to the caller's context.
> (This is, with about 95% certainty, how your Pascal compiler did
> it in the first place, so the new code will not have any extra
> overhead.)

> In my experience, in many cases, Pascal code that uses nested
> procedures and functions can be rewritten using simple C macros or
> existing C library functions, avoiding the need for anything like
> this anyway.

I think this is well suited for machine translation, e.g. a
compiler, but not so useful for hand work.  It requires altering
all sorts of things within the nested procedures.  Passing
additional parameters requires altering the calls and function
header/prototype only.  Of course, tastes vary.

BTW, my compilers didn't do that - they used static and dynamic
links.  In a way you might consider that as passing up a context,
but the context only extended to the callers frame, not
everything.  When you get to:

     PROCEDURE a;
        PROCEDURE b;
           PROCEDURE c;
              PROCEDURE d;
                 PROCEDURE e;
                    PROCEDURE f;

or worse, with suitable variable declarations, that context can
become messy.  The other way you work from the inside out and with
trial compiles the compiler will tell you what is missing.

--

http://www.qwikpages.com/backstreets/cbfalconer
   (Remove "NOSPAM." from reply address. my-deja works unmodified)



Mon, 30 Jun 2003 12:27:23 GMT  
 Converting Pascal to C

Quote:

> I have an application written in Pascal using IPC message queues.
> I would like to convert this one into C language.
> Where can I find information about :
> - guidelines to plan this application migration
> - main differences between C and Pascal (general) such as variables
> visibility differences, arguments ...

You might check p2c.  Here are some links for several platforms:

http://mirror.ox.ac.uk/rpm2html/p2c.html
http://ftp.digital.com/pub/BSD/NetBSD/NetBSD-current/pkgsrc/lang/p2c/
http://donna.asknet.de/ftp/hpux/Languages/p2c-1.20/
ftp://cs.caltech.edu/pub/
http://www.simtel.net/pub/simtelnet/gnu/djgpp/v2gnu/



Mon, 30 Jun 2003 16:51:28 GMT  
 Converting Pascal to C

Quote:

>> (This is, with about 95% certainty, how your Pascal compiler did
>> it in the first place, so the new code will not have any extra
>> overhead.)

No, it doesn't really repackage, it simply also pushes the parent BP
as implicit parameter.

Quote:
>It's also not clear what you intend to have happen with several
>levels of nesting--do you package up the outer function's context
>pointer in the inner function's context (when calling a function
>nested in the inner function which is nested in the outer function),
>or do you pass a pointer to each?  

The parent's context that away also contains the pointer to the
parent's parent context. No problem :-)

Quote:
>Most Pascal compilers where I
>know what they do, do the former, creating a linked list of contexts
>that has to be traversed if functions are deeply nested, but the
>latter would result in less overhead (unless it pushes the number
>of arguments past some breakpoint).

The linked list usually already exists on the stack. Only if you use e.g.
register parameters, you will have to do things differently.


Mon, 30 Jun 2003 22:31:48 GMT  
 Converting Pascal to C
Alma is a general translator for programming language. It can parse
Pascal and generates C, even if it prefers object-oriented stuff. So
maybe it can help you. (and it speaks french ;-)

Sent via Deja.com
http://www.deja.com/



Wed, 02 Jul 2003 02:20:19 GMT  
 Converting Pascal to C

Quote:
> I have an application written in Pascal using IPC message queues.
> I would like to convert this one into C language.
> Where can I find information about :
> - guidelines to plan this application migration
> - main differences between C and Pascal (general) such as variables
> visibility differences, arguments ...

Is there a special reason you want to translate into C, instead
of a more recent language containing the Pascal semantics ?
If you want reliability and a low cost/time you have better to
choose Ada95; the translation is straightforward.

HTH
__________________________________________
Gautier  --  http://www.diax.ch/users/gdm/

Sent via Deja.com
http://www.deja.com/



Wed, 02 Jul 2003 17:55:52 GMT  
 Converting Pascal to C

Quote:
>> Alma is a general translator for programming language. It can
>> parse Pascal and generates C.



Quote:
> You have a URL that this can be downloaded from?

http://www.memoire.com/guillaume-desnoix/alma/

Sent via Deja.com
http://www.deja.com/



Fri, 04 Jul 2003 18:37:22 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. Converting Turbo Pascal to ANSI Pascal

2. Delphi CS 1.02 to Sybase Help

3. Accessing oracle sequences from Delphi 1.0 CS

4. CS Apps : TTable or TQuery ?

5. Delphi CS, Oracle7.2 & SQLNetV2.2

6. Documents in tables in CS ?

7. help needed please for CS 110 class

8. Delphi 2.0 CS Update & BLOB Problem

9. kcip@cs.cuhk.hk

10. Problem with Delphi3 CS and Sybase 11 - HELP please

11. 3 Questions on Delphi 3.0 cs

12. CS Queries than can be Cancelled

 

 
Powered by phpBB® Forum Software