converting c code to pascal code

I am looking for a good converter which translate "K&R C source code" to Pascal code
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.



That's definitely not a 20 points task :-)
Even 500 are a bit too low :-)

The best thing I can do for you is to try and introduce you to some
nasty tips&tricks to help you translating code.

This is a very small primer on the topic.

Let's see some procedure declarations:

void MyRoutine( Parameters );

This is a procedure. "Void" in C means it's "empty" thus no value.

void *MyRoutine( Parameters );

This is a function. "Void *" in C means "Pointer to empty", thus an
untyped pointer.

Generally speaking, when you see an * near an identifier, that is a

char * = PChar = lpsz

The former is a very common equation to make. The last form is
especially used in Windows APIs.

char []

This is an array, but beware: arrays in C are the same as pointers,
thus you can treat a pointer as an array and viceversa.
Keep this in mind when translating code.

  ( void * )( int Function )

This is nasty. This is a function pointer, much like:

   TFunc = function : Integer;

Well, these are the usual pitfalls.



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Try a search on Google with c2pas. One of the results leads to this

I have not tested the mentioned utility, but you could give it a try!

I saw one of these about 15 years back in a Borland magazine.  You might look at Borland's support database.

It's about 8 hours of work to write and deobug the parser if you're already familiar with parsers, and another 8 hours of work to take the parsed tree and write it back out in C instead of Pascal and debug that.

Simple string substitution will handle most of it ...

Pascal   C
begin    {
end      }
:=       =
=        ==
void     procedure
int       integer
double real
record  struct
(*       /*
{         /*
}         */
*)       */

and so on

What cannot be handled by simple string substitution is:
*   pointer dereferencing (always an issue in C programming)
*   "for" loop constructs
*   places where C syntax call for different order than Pascal sequence.

The latter two pieces are trivial in the sense that the compiler will let you know about them.  The former is a pain in the <insert descripter here>.

I would probably start by writing a set of general translator classes without implementations, then write the Pascal reader to build a parsed tree, and a C writer to print the parsed tree with C constructs.  At this point it becomes trivial to reverse the process and write a two way translator.

If I were point starved, I might sit down and do this for the 500 points suggested by DeerBear because it sounds fun.  I'd certainly be willing to help you build one by answering question sand offering a bit of advice just because it does sound fun, although you would have to do all of the work.  I'm not starved for points, so increasing the point count won't get much more from me than advice on this one.

Initially, these are the classes you need.  All are descended from TObject.


Once these general LANGUAGE INDEPENDENT classes are defined, you need to descend a TPascalCodeReader and a TCCodeWriter.  The TParsedTree will be language independent so it does not need descendents.  

The TTranslator should be and independent wrapper that has a TCodeReader, TParsedTree, and TCodeWriter component, so it should also need no descendents.  

The TParsedTree is actually the most complex piece, although it is primarily a data object.  It implements the recursive structure that is defines a "module" in the abstract sense in any programming language.

I would recommend downloading dUnit at thispoint in time from sourceforge, since you will need to get the parsed tree working before you can really get started on the specific code parsers or code writers.
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

Hi Swift99,

I saw at least a flaw in your table:

void can, yeah, be used for procedure, but also for pointer in the form void* .


Yep ... hence the need for something more robust than string substitution is this is not a one-shot conversion.
Swift99 was the first to give some samples, the only thing DeerBear has added was pointing at a minor flaw
So Swift99 should be accepted, I don't care about the points ;)
Hi DeNavigator,

Please check again *all* the messages :-)

I don't usually respond to these posts, but your inaccuracy this time yelded my reply.
Please *do* check again *all* the posts.


DeerBear explained the principles and provided examples of the manual process.  I provided a rough outline of automating the process, but put no work into it.

I concur that DeerBear deserves the points.

Please keep in mind that nobody is under accusation.
I replied simply because it was stated that I only provided a small correction and nothing else.
This was simply not true, and wanting correction.

As for the points, I think nobody of us is really concerned about earning them.
Speaking for me, my motivation is helping out other people and if I can get some points, the better :-)

I just replied because I felt DeNavigator message to be inaccurate to say the least.

Sorry DeerBear I overlooked your message. But then again ... whatever let it go, it is worse enough that the asker hasnot accepted an answer after a year.
No problem :-)

Well, I've left a few questions unaccepted too for a long time. It can happen, I guess :-)


It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.